polars_arrow/scalar/
binary.rs

1use super::Scalar;
2use crate::datatypes::ArrowDataType;
3use crate::offset::Offset;
4
5/// The [`Scalar`] implementation of binary ([`Option<Vec<u8>>`]).
6#[derive(Debug, Clone, PartialEq, Eq)]
7pub struct BinaryScalar<O: Offset> {
8    value: Option<Vec<u8>>,
9    phantom: std::marker::PhantomData<O>,
10}
11
12impl<O: Offset> BinaryScalar<O> {
13    /// Returns a new [`BinaryScalar`].
14    #[inline]
15    pub fn new<P: Into<Vec<u8>>>(value: Option<P>) -> Self {
16        Self {
17            value: value.map(|x| x.into()),
18            phantom: std::marker::PhantomData,
19        }
20    }
21
22    /// Its value
23    #[inline]
24    pub fn value(&self) -> Option<&[u8]> {
25        self.value.as_ref().map(|x| x.as_ref())
26    }
27}
28
29impl<O: Offset, P: Into<Vec<u8>>> From<Option<P>> for BinaryScalar<O> {
30    #[inline]
31    fn from(v: Option<P>) -> Self {
32        Self::new(v)
33    }
34}
35
36impl<O: Offset> Scalar for BinaryScalar<O> {
37    #[inline]
38    fn as_any(&self) -> &dyn std::any::Any {
39        self
40    }
41
42    #[inline]
43    fn is_valid(&self) -> bool {
44        self.value.is_some()
45    }
46
47    #[inline]
48    fn dtype(&self) -> &ArrowDataType {
49        if O::IS_LARGE {
50            &ArrowDataType::LargeBinary
51        } else {
52            &ArrowDataType::Binary
53        }
54    }
55}