Skip to main content

Null

Trait Null 

Source
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 value
  • T::null().is_null() should always return true
  • The null value should be a valid instance of the type

Required Methods§

Source

fn null() -> Self

Create a null/sentinel value of this type.

This should return a deterministic “null” value that satisfies is_null(). The exact meaning of “null” is type-specific.

§Examples
use multi_trait::Null;

let null_id = ResourceId::null();
assert!(null_id.is_null());
Source

fn is_null(&self) -> bool

Check if this value is the null value.

Returns true if this instance represents the null/sentinel value, false otherwise.

§Examples
use multi_trait::Null;

let zero = Counter::null();
assert!(zero.is_null());

let non_zero = Counter(5);
assert!(!non_zero.is_null());

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§