pub trait Null {
// Required methods
fn null() -> Self;
fn is_null(&self) -> bool;
}Expand description
Trait for types that have a null/sentinel value.
This trait allows multiformats types to define a “null” or sentinel value,
similar to Option::None but for types where a discriminated union isn’t
appropriate. Common examples include null CIDs, null signatures, or
default/uninitialized identifiers.
§Use Cases
- Defining “zero” values for custom ID types
- Creating null/empty multiformats objects
- Sentinel values in data structures
- Default initialization for resource handles
§Thread Safety
This trait is Send + Sync safe when implemented on thread-safe types.
§Examples
use multi_trait::Null;
#[derive(Debug, PartialEq)]
struct UserId(u64);
impl Null for UserId {
fn null() -> Self {
UserId(0)
}
fn is_null(&self) -> bool {
self.0 == 0
}
}
let null_user = UserId::null();
assert!(null_user.is_null());
let valid_user = UserId(42);
assert!(!valid_user.is_null());§Implementation Guidelines
The null value should be consistent and deterministic. For a given type:
T::null()should always return the same logical valueT::null().is_null()should always returntrue- The null value should be a valid instance of the type
Required Methods§
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".