Skip to main content

solana_nullable/
nullable.rs

1/// Trait for types that can be `None`.
2///
3/// This trait is used to indicate that a type can reserve a specific value to
4/// represent `None`.
5pub trait Nullable: PartialEq + Sized {
6    /// Value that represents `None` for the type.
7    const NONE: Self;
8
9    /// Indicates whether the value is `None` or not.
10    fn is_none(&self) -> bool {
11        self == &Self::NONE
12    }
13
14    /// Indicates whether the value is a `Some` value of type `Self` or not.
15    fn is_some(&self) -> bool {
16        !self.is_none()
17    }
18}
19
20macro_rules! nullable_integer {
21    ( $type:tt ) => {
22        #[doc = concat!("Implements `Nullable` for the `", stringify!($type), "` type, reserving `0` as the `NONE` value.")]
23        impl Nullable for $type {
24            const NONE: Self = 0;
25        }
26    };
27}
28
29nullable_integer!(u8);
30nullable_integer!(u16);
31nullable_integer!(u32);
32nullable_integer!(u64);
33#[cfg(not(target_arch = "bpf"))]
34nullable_integer!(u128);