Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/crates/polars-arrow/src/scalar/binview.rs
6939 views
1
use std::fmt::{Debug, Formatter};
2
3
use super::Scalar;
4
use crate::array::ViewType;
5
use crate::datatypes::ArrowDataType;
6
7
/// The implementation of [`Scalar`] for utf8, semantically equivalent to [`Option<String>`].
8
#[derive(PartialEq, Eq)]
9
pub struct BinaryViewScalar<T: ViewType + ?Sized> {
10
value: Option<T::Owned>,
11
phantom: std::marker::PhantomData<T>,
12
}
13
14
impl<T: ViewType + ?Sized> Debug for BinaryViewScalar<T> {
15
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
16
write!(f, "Scalar({:?})", self.value)
17
}
18
}
19
20
impl<T: ViewType + ?Sized> Clone for BinaryViewScalar<T> {
21
fn clone(&self) -> Self {
22
Self {
23
value: self.value.clone(),
24
phantom: Default::default(),
25
}
26
}
27
}
28
29
impl<T: ViewType + ?Sized> BinaryViewScalar<T> {
30
/// Returns a new [`BinaryViewScalar`]
31
#[inline]
32
pub fn new(value: Option<&T>) -> Self {
33
Self {
34
value: value.map(|x| x.into_owned()),
35
phantom: std::marker::PhantomData,
36
}
37
}
38
39
/// Returns the value irrespectively of the validity.
40
#[inline]
41
pub fn value(&self) -> Option<&T> {
42
self.value.as_ref().map(|x| x.as_ref())
43
}
44
}
45
46
impl<T: ViewType + ?Sized> From<Option<&T>> for BinaryViewScalar<T> {
47
#[inline]
48
fn from(v: Option<&T>) -> Self {
49
Self::new(v)
50
}
51
}
52
53
impl<T: ViewType + ?Sized> Scalar for BinaryViewScalar<T> {
54
#[inline]
55
fn as_any(&self) -> &dyn std::any::Any {
56
self
57
}
58
59
#[inline]
60
fn is_valid(&self) -> bool {
61
self.value.is_some()
62
}
63
64
#[inline]
65
fn dtype(&self) -> &ArrowDataType {
66
if T::IS_UTF8 {
67
&ArrowDataType::Utf8View
68
} else {
69
&ArrowDataType::BinaryView
70
}
71
}
72
}
73
74