polars_arrow/scalar/
utf8.rs

1use super::Scalar;
2use crate::datatypes::ArrowDataType;
3use crate::offset::Offset;
4
5/// The implementation of [`Scalar`] for utf8, semantically equivalent to [`Option<String>`].
6#[derive(Debug, Clone, PartialEq, Eq)]
7pub struct Utf8Scalar<O: Offset> {
8    value: Option<String>,
9    phantom: std::marker::PhantomData<O>,
10}
11
12impl<O: Offset> Utf8Scalar<O> {
13    /// Returns a new [`Utf8Scalar`]
14    #[inline]
15    pub fn new<P: Into<String>>(value: Option<P>) -> Self {
16        Self {
17            value: value.map(|x| x.into()),
18            phantom: std::marker::PhantomData,
19        }
20    }
21
22    /// Returns the value irrespectively of the validity.
23    #[inline]
24    pub fn value(&self) -> Option<&str> {
25        self.value.as_ref().map(|x| x.as_ref())
26    }
27}
28
29impl<O: Offset, P: Into<String>> From<Option<P>> for Utf8Scalar<O> {
30    #[inline]
31    fn from(v: Option<P>) -> Self {
32        Self::new(v)
33    }
34}
35
36impl<O: Offset> Scalar for Utf8Scalar<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::LargeUtf8
51        } else {
52            &ArrowDataType::Utf8
53        }
54    }
55}