polars_arrow/scalar/
fixed_size_list.rs

1use std::any::Any;
2
3use super::Scalar;
4use crate::array::*;
5use crate::datatypes::ArrowDataType;
6
7/// The scalar equivalent of [`FixedSizeListArray`]. Like [`FixedSizeListArray`], this struct holds a dynamically-typed
8/// [`Array`]. The only difference is that this has only one element.
9#[derive(Debug, Clone)]
10pub struct FixedSizeListScalar {
11    values: Option<Box<dyn Array>>,
12    dtype: ArrowDataType,
13}
14
15impl PartialEq for FixedSizeListScalar {
16    fn eq(&self, other: &Self) -> bool {
17        (self.dtype == other.dtype)
18            && (self.values.is_some() == other.values.is_some())
19            && ((self.values.is_none()) | (self.values.as_ref() == other.values.as_ref()))
20    }
21}
22
23impl FixedSizeListScalar {
24    /// returns a new [`FixedSizeListScalar`]
25    /// # Panics
26    /// iff
27    /// * the `dtype` is not `FixedSizeList`
28    /// * the child of the `dtype` is not equal to the `values`
29    /// * the size of child array is not equal
30    #[inline]
31    pub fn new(dtype: ArrowDataType, values: Option<Box<dyn Array>>) -> Self {
32        let (field, size) = FixedSizeListArray::get_child_and_size(&dtype);
33        let inner_dtype = field.dtype();
34        let values = values.inspect(|x| {
35            assert_eq!(inner_dtype, x.dtype());
36            assert_eq!(size, x.len());
37        });
38        Self { values, dtype }
39    }
40
41    /// The values of the [`FixedSizeListScalar`]
42    pub fn values(&self) -> Option<&Box<dyn Array>> {
43        self.values.as_ref()
44    }
45}
46
47impl Scalar for FixedSizeListScalar {
48    fn as_any(&self) -> &dyn Any {
49        self
50    }
51
52    fn is_valid(&self) -> bool {
53        self.values.is_some()
54    }
55
56    fn dtype(&self) -> &ArrowDataType {
57        &self.dtype
58    }
59}