Path: blob/main/crates/polars-arrow/src/scalar/binview.rs
6939 views
use std::fmt::{Debug, Formatter};12use super::Scalar;3use crate::array::ViewType;4use crate::datatypes::ArrowDataType;56/// The implementation of [`Scalar`] for utf8, semantically equivalent to [`Option<String>`].7#[derive(PartialEq, Eq)]8pub struct BinaryViewScalar<T: ViewType + ?Sized> {9value: Option<T::Owned>,10phantom: std::marker::PhantomData<T>,11}1213impl<T: ViewType + ?Sized> Debug for BinaryViewScalar<T> {14fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {15write!(f, "Scalar({:?})", self.value)16}17}1819impl<T: ViewType + ?Sized> Clone for BinaryViewScalar<T> {20fn clone(&self) -> Self {21Self {22value: self.value.clone(),23phantom: Default::default(),24}25}26}2728impl<T: ViewType + ?Sized> BinaryViewScalar<T> {29/// Returns a new [`BinaryViewScalar`]30#[inline]31pub fn new(value: Option<&T>) -> Self {32Self {33value: value.map(|x| x.into_owned()),34phantom: std::marker::PhantomData,35}36}3738/// Returns the value irrespectively of the validity.39#[inline]40pub fn value(&self) -> Option<&T> {41self.value.as_ref().map(|x| x.as_ref())42}43}4445impl<T: ViewType + ?Sized> From<Option<&T>> for BinaryViewScalar<T> {46#[inline]47fn from(v: Option<&T>) -> Self {48Self::new(v)49}50}5152impl<T: ViewType + ?Sized> Scalar for BinaryViewScalar<T> {53#[inline]54fn as_any(&self) -> &dyn std::any::Any {55self56}5758#[inline]59fn is_valid(&self) -> bool {60self.value.is_some()61}6263#[inline]64fn dtype(&self) -> &ArrowDataType {65if T::IS_UTF8 {66&ArrowDataType::Utf8View67} else {68&ArrowDataType::BinaryView69}70}71}727374