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
6939 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!(
27
dtype.to_logical_type(),
28
&ArrowDataType::FixedSizeBinary(x.len())
29
);
30
x.into_boxed_slice()
31
}),
32
dtype,
33
}
34
}
35
36
/// Its value
37
#[inline]
38
pub fn value(&self) -> Option<&[u8]> {
39
self.value.as_ref().map(|x| x.as_ref())
40
}
41
}
42
43
impl Scalar for FixedSizeBinaryScalar {
44
#[inline]
45
fn as_any(&self) -> &dyn std::any::Any {
46
self
47
}
48
49
#[inline]
50
fn is_valid(&self) -> bool {
51
self.value.is_some()
52
}
53
54
#[inline]
55
fn dtype(&self) -> &ArrowDataType {
56
&self.dtype
57
}
58
}
59
60