1#[derive(Clone, Copy, Debug, PartialEq, Eq)]
10pub enum Nullable<T> {
11 Null,
13 Value(T),
15}
16
17impl<T> Nullable<T> {
18 pub fn value(self) -> Option<T> {
20 match self {
21 Nullable::Null => None,
22 Nullable::Value(v) => Some(v),
23 }
24 }
25
26 #[must_use]
28 pub fn is_null(&self) -> bool {
29 matches!(self, Nullable::Null)
30 }
31}
32
33impl<T> From<Option<T>> for Nullable<T> {
34 fn from(o: Option<T>) -> Self {
35 match o {
36 Some(v) => Nullable::Value(v),
37 None => Nullable::Null,
38 }
39 }
40}
41
42#[cfg(test)]
43mod tests {
44 use super::*;
45
46 #[test]
47 fn value_and_is_null() {
48 assert_eq!(Nullable::Value(7u8).value(), Some(7));
49 assert!(Nullable::<u8>::Null.value().is_none());
50 assert!(Nullable::<u8>::Null.is_null());
51 assert!(!Nullable::Value(7u8).is_null());
52 }
53
54 #[test]
55 fn from_option() {
56 assert_eq!(Nullable::from(Some(3u8)), Nullable::Value(3));
57 assert_eq!(Nullable::<u8>::from(None), Nullable::Null);
58 }
59}