vortex_dtype/
nullability.rs

1use std::fmt::{Display, Formatter};
2use std::ops::BitOr;
3
4/// Whether an instance of a DType can be `null or not
5#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
6pub enum Nullability {
7    /// Instances of this DType are guaranteed to be non-nullable
8    #[default]
9    NonNullable,
10    /// Instances of this DType may contain a null value
11    Nullable,
12}
13
14impl Nullability {
15    /// A self-describing displayed form.
16    ///
17    /// The usual Display renders [Nullability::NonNullable] as the empty string.
18    pub fn verbose_display(&self) -> impl Display {
19        match self {
20            Nullability::NonNullable => "NonNullable",
21            Nullability::Nullable => "Nullable",
22        }
23    }
24}
25
26impl BitOr for Nullability {
27    type Output = Nullability;
28
29    fn bitor(self, rhs: Self) -> Self::Output {
30        match (self, rhs) {
31            (Self::NonNullable, Self::NonNullable) => Self::NonNullable,
32            _ => Self::Nullable,
33        }
34    }
35}
36
37impl From<bool> for Nullability {
38    fn from(value: bool) -> Self {
39        if value {
40            Self::Nullable
41        } else {
42            Self::NonNullable
43        }
44    }
45}
46
47impl From<Nullability> for bool {
48    fn from(value: Nullability) -> Self {
49        match value {
50            Nullability::NonNullable => false,
51            Nullability::Nullable => true,
52        }
53    }
54}
55
56impl Display for Nullability {
57    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
58        match self {
59            Self::NonNullable => write!(f, ""),
60            Self::Nullable => write!(f, "?"),
61        }
62    }
63}