Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/crates/polars-arrow/src/scalar/binary.rs
6939 views
1
use super::Scalar;
2
use crate::datatypes::ArrowDataType;
3
use crate::offset::Offset;
4
5
/// The [`Scalar`] implementation of binary ([`Option<Vec<u8>>`]).
6
#[derive(Debug, Clone, PartialEq, Eq)]
7
pub struct BinaryScalar<O: Offset> {
8
value: Option<Vec<u8>>,
9
phantom: std::marker::PhantomData<O>,
10
}
11
12
impl<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
29
impl<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
36
impl<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
}
56
57