Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/crates/polars-arrow/src/scalar/fixed_size_binary.rs
8415 views
1
use super::Scalar;
2
use crate::datatypes::ArrowDataType;
3
4
#[derive(Debug, Clone, PartialEq, Eq)]
5
/// The [`Scalar`] implementation of fixed size binary ([`Option<Box<[u8]>>`]).
6
pub struct FixedSizeBinaryScalar {
7
value: Option<Box<[u8]>>,
8
dtype: ArrowDataType,
9
}
10
11
impl FixedSizeBinaryScalar {
12
/// Returns a new [`FixedSizeBinaryScalar`].
13
/// # Panics
14
/// iff
15
/// * the `dtype` is not `FixedSizeBinary`
16
/// * the size of child binary is not equal
17
#[inline]
18
pub fn new<P: Into<Vec<u8>>>(dtype: ArrowDataType, value: Option<P>) -> Self {
19
assert_eq!(
20
dtype.to_physical_type(),
21
crate::datatypes::PhysicalType::FixedSizeBinary
22
);
23
Self {
24
value: value.map(|x| {
25
let x: Vec<u8> = x.into();
26
assert_eq!(dtype.to_storage(), &ArrowDataType::FixedSizeBinary(x.len()));
27
x.into_boxed_slice()
28
}),
29
dtype,
30
}
31
}
32
33
/// Its value
34
#[inline]
35
pub fn value(&self) -> Option<&[u8]> {
36
self.value.as_ref().map(|x| x.as_ref())
37
}
38
}
39
40
impl Scalar for FixedSizeBinaryScalar {
41
#[inline]
42
fn as_any(&self) -> &dyn std::any::Any {
43
self
44
}
45
46
#[inline]
47
fn is_valid(&self) -> bool {
48
self.value.is_some()
49
}
50
51
#[inline]
52
fn dtype(&self) -> &ArrowDataType {
53
&self.dtype
54
}
55
}
56
57