-
Notifications
You must be signed in to change notification settings - Fork 202
Expand file tree
/
Copy pathnullability.rs
More file actions
164 lines (142 loc) · 4.29 KB
/
Copy pathnullability.rs
File metadata and controls
164 lines (142 loc) · 4.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright the Vortex contributors
use std::fmt::Display;
use std::fmt::Formatter;
use std::ops::BitOr;
use std::ops::BitOrAssign;
/// Whether an instance of a DType can be `null or not
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
pub enum Nullability {
/// Instances of this DType are guaranteed to be non-nullable
#[default]
NonNullable,
/// Instances of this DType may contain a null value
Nullable,
}
impl Nullability {
/// Returns `true` if the nullability is [`Nullable`](Self::Nullable), otherwise returns
/// `false`.
///
/// # Examples
///
/// ```
/// use vortex_array::dtype::Nullability::*;
///
/// assert!(!NonNullable.is_nullable());
/// assert!(Nullable.is_nullable());
/// ```
pub fn is_nullable(&self) -> bool {
match self {
Nullability::NonNullable => false,
Nullability::Nullable => true,
}
}
}
impl BitOr for Nullability {
type Output = Nullability;
#[inline]
fn bitor(self, rhs: Self) -> Self::Output {
match (self, rhs) {
(Self::NonNullable, Self::NonNullable) => Self::NonNullable,
_ => Self::Nullable,
}
}
}
impl BitOrAssign for Nullability {
fn bitor_assign(&mut self, rhs: Self) {
*self = *self | rhs
}
}
impl From<bool> for Nullability {
#[inline]
fn from(value: bool) -> Self {
if value {
Self::Nullable
} else {
Self::NonNullable
}
}
}
impl From<Nullability> for bool {
#[inline]
fn from(value: Nullability) -> Self {
match value {
Nullability::NonNullable => false,
Nullability::Nullable => true,
}
}
}
impl Display for Nullability {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Self::NonNullable => {
if f.alternate() {
write!(f, "NonNullable")
} else {
write!(f, "")
}
}
Self::Nullable => {
if f.alternate() {
write!(f, "Nullable")
} else {
write!(f, "?")
}
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_nullability_default() {
let default = Nullability::default();
assert_eq!(default, Nullability::NonNullable);
}
#[test]
fn test_nullability_bitor() {
use Nullability::*;
// NonNullable | NonNullable = NonNullable
assert_eq!(NonNullable | NonNullable, NonNullable);
// NonNullable | Nullable = Nullable
assert_eq!(NonNullable | Nullable, Nullable);
// Nullable | NonNullable = Nullable
assert_eq!(Nullable | NonNullable, Nullable);
// Nullable | Nullable = Nullable
assert_eq!(Nullable | Nullable, Nullable);
}
#[test]
fn test_nullability_from_bool() {
assert_eq!(Nullability::from(false), Nullability::NonNullable);
assert_eq!(Nullability::from(true), Nullability::Nullable);
}
#[test]
fn test_bool_from_nullability() {
assert!(!bool::from(Nullability::NonNullable));
assert!(bool::from(Nullability::Nullable));
}
#[test]
fn test_nullability_roundtrip() {
// Test roundtrip conversion bool -> Nullability -> bool
assert!(!bool::from(Nullability::from(false)));
assert!(bool::from(Nullability::from(true)));
// Test roundtrip conversion Nullability -> bool -> Nullability
assert_eq!(
Nullability::from(bool::from(Nullability::NonNullable)),
Nullability::NonNullable
);
assert_eq!(
Nullability::from(bool::from(Nullability::Nullable)),
Nullability::Nullable
);
}
#[test]
fn test_nullability_chained_bitor() {
// Test chaining multiple BitOr operations
let result = Nullability::NonNullable | Nullability::NonNullable | Nullability::NonNullable;
assert_eq!(result, Nullability::NonNullable);
let result = Nullability::NonNullable | Nullability::Nullable | Nullability::NonNullable;
assert_eq!(result, Nullability::Nullable);
}
}