vortex_dtype/
nullability.rs

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