Struct UntaggedOption

Source
pub struct UntaggedOption<T: Zeroable> { /* private fields */ }
Expand description

A zero-optimized option type that uses no discriminant.

UntaggedOption<T> represents None by storing a zeroed value of T, and Some(value) by any non-zero T. This can save space, especially in FFI or low-level contexts.

§Safety

T must be valid in its all-zero bit pattern. Implementing Zeroable for a type that cannot safely be all-zero invokes undefined behavior.

§⚠️ Undefined Behavior example:

use core::num::NonZeroU32;
use std_fork::zeroable::Zeroable;

unsafe impl Zeroable for NonZeroU32 {} // ⚠️ Undefined Behavior!

§✅ Safe example:

use std_fork::option::UntaggedOption;
use std_fork::zeroable::Zeroable;

#[derive(Debug, PartialEq, Clone, Copy)]
#[repr(C)]
struct MyInt(u32);

unsafe impl Zeroable for MyInt {}

let opt = UntaggedOption::some(MyInt(123));
assert!(opt.is_some());

Implementations§

Source§

impl<T> UntaggedOption<T>
where T: Zeroable,

Source

pub const T_SIZE: usize

Size of the inner type T.

Source

pub const IS_ZST: bool

True if T is a zero-sized type (ZST).

Source

pub const NONE: Self

A constant representing None.

Source

pub const fn some(value: T) -> Self

Creates a new UntaggedOption containing a value.

§Example
use std_fork::option::UntaggedOption;
use std_fork::zeroable::Zeroable;

#[derive(Clone, Copy, Debug, PartialEq)]
#[repr(transparent)]
struct Wrapper(u32);

unsafe impl Zeroable for Wrapper {}

let opt = UntaggedOption::some(Wrapper(1));
assert!(opt.is_some());
Source

pub const fn none() -> Self

Creates a new UntaggedOption representing None.

§Example
use std_fork::option::UntaggedOption;

let none = UntaggedOption::<u32>::none();
assert!(none.is_none());
Source

pub fn is_none(&self) -> bool

Returns true if the option is None.

§Note

Zero-sized types (ZSTs) always return true.

§Example
use std_fork::option::UntaggedOption;

let none = UntaggedOption::<u32>::none();
assert!(none.is_none());

let some = UntaggedOption::some(123u32);
assert!(!some.is_none());
Source

pub fn is_some(&self) -> bool

Returns true if the option contains a value.

§Example
use std_fork::option::UntaggedOption;

let opt = UntaggedOption::some(7i32);
assert!(opt.is_some());

let none = UntaggedOption::<()>::none();
assert!(!none.is_some());
Source

pub fn take(&mut self) -> Option<T>

Takes the value out of the option, leaving a None in its place.

§Example
use std_fork::option::UntaggedOption;
use std_fork::zeroable::Zeroable;

#[derive(Clone, Copy, Debug, PartialEq)]
#[repr(C)]
struct MyInt(i32);

unsafe impl Zeroable for MyInt {}

let mut opt = UntaggedOption::some(MyInt(42));
let taken = opt.take();

assert_eq!(taken, Some(MyInt(42)));
assert!(opt.is_none());
Source

pub fn as_ref(&self) -> Option<&T>

Returns a reference to the value if Some, otherwise returns None.

§Example
use std_fork::option::UntaggedOption;

let opt = UntaggedOption::some(999u32);
assert_eq!(opt.as_ref(), Some(&999));

let none = UntaggedOption::<u32>::none();
assert_eq!(none.as_ref(), None);
Source

pub fn as_mut(&mut self) -> Option<&mut T>

Returns a mutable reference to the value if Some, otherwise returns None.

§Example
use std_fork::option::UntaggedOption;

let mut opt = UntaggedOption::some(100i32);

if let Some(val) = opt.as_mut() {
    *val += 1;
}

assert_eq!(opt.as_ref(), Some(&101));

Trait Implementations§

Source§

impl<T: Debug + Zeroable> Debug for UntaggedOption<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> From<Option<T>> for UntaggedOption<T>
where T: Zeroable,

Source§

fn from(value: Option<T>) -> Self

Converts a standard Option<T> into an UntaggedOption<T>.

§Example
use std_fork::option::UntaggedOption;

let opt = UntaggedOption::from(Some(255u8));
assert!(opt.is_some());

let none = UntaggedOption::from(None::<u8>);
assert!(none.is_none());
Source§

impl<T: PartialEq + Zeroable> PartialEq for UntaggedOption<T>

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.

Auto Trait Implementations§

§

impl<T> Freeze for UntaggedOption<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for UntaggedOption<T>
where T: RefUnwindSafe,

§

impl<T> Send for UntaggedOption<T>
where T: Send,

§

impl<T> Sync for UntaggedOption<T>
where T: Sync,

§

impl<T> Unpin for UntaggedOption<T>
where T: Unpin,

§

impl<T> UnwindSafe for UntaggedOption<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.