vortex_vector/bool/
scalar.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use crate::Scalar;
5use crate::ScalarOps;
6use crate::VectorMut;
7use crate::VectorMutOps;
8use crate::bool::BoolVectorMut;
9
10/// A scalar value for boolean types.
11#[derive(Clone, Debug)]
12pub struct BoolScalar(Option<bool>);
13
14impl BoolScalar {
15    /// Creates a new bool scalar with the given value.
16    pub fn new(value: Option<bool>) -> Self {
17        Self(value)
18    }
19
20    /// Returns the value of the bool scalar, or `None` if the scalar is null.
21    pub fn value(&self) -> Option<bool> {
22        self.0
23    }
24}
25
26impl ScalarOps for BoolScalar {
27    fn is_valid(&self) -> bool {
28        self.0.is_some()
29    }
30
31    fn mask_validity(&mut self, mask: bool) {
32        if !mask {
33            self.0 = None;
34        }
35    }
36
37    fn repeat(&self, n: usize) -> VectorMut {
38        let mut vec = BoolVectorMut::with_capacity(n);
39        match self.0 {
40            None => vec.append_nulls(n),
41            Some(value) => vec.append_values(value, n),
42        }
43        vec.into()
44    }
45}
46
47impl From<BoolScalar> for Scalar {
48    fn from(val: BoolScalar) -> Self {
49        Scalar::Bool(val)
50    }
51}
52
53impl From<bool> for Scalar {
54    fn from(value: bool) -> Self {
55        BoolScalar::new(Some(value)).into()
56    }
57}