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,
impl<T> UntaggedOption<T>where
T: Zeroable,
Sourcepub const fn some(value: T) -> Self
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());
Sourcepub const fn none() -> Self
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());
Sourcepub fn is_some(&self) -> bool
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());
Sourcepub fn take(&mut self) -> Option<T>
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());
Sourcepub fn as_ref(&self) -> Option<&T>
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);
Trait Implementations§
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more