polars_arrow/scalar/
binary.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
use super::Scalar;
use crate::datatypes::ArrowDataType;
use crate::offset::Offset;

/// The [`Scalar`] implementation of binary ([`Option<Vec<u8>>`]).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BinaryScalar<O: Offset> {
    value: Option<Vec<u8>>,
    phantom: std::marker::PhantomData<O>,
}

impl<O: Offset> BinaryScalar<O> {
    /// Returns a new [`BinaryScalar`].
    #[inline]
    pub fn new<P: Into<Vec<u8>>>(value: Option<P>) -> Self {
        Self {
            value: value.map(|x| x.into()),
            phantom: std::marker::PhantomData,
        }
    }

    /// Its value
    #[inline]
    pub fn value(&self) -> Option<&[u8]> {
        self.value.as_ref().map(|x| x.as_ref())
    }
}

impl<O: Offset, P: Into<Vec<u8>>> From<Option<P>> for BinaryScalar<O> {
    #[inline]
    fn from(v: Option<P>) -> Self {
        Self::new(v)
    }
}

impl<O: Offset> Scalar for BinaryScalar<O> {
    #[inline]
    fn as_any(&self) -> &dyn std::any::Any {
        self
    }

    #[inline]
    fn is_valid(&self) -> bool {
        self.value.is_some()
    }

    #[inline]
    fn dtype(&self) -> &ArrowDataType {
        if O::IS_LARGE {
            &ArrowDataType::LargeBinary
        } else {
            &ArrowDataType::Binary
        }
    }
}