pub struct Maybe<T, G = ()> { /* private fields */ }Expand description
Represents a type similar in spirit to the built-in Option type.
Unlike Option however, Maybe does have in-place initializer support.
(In-place initializer support is impossible to provide for Option due to its
enum nature, and because it is not marked with repr(transparent)).
Maybe is convertable to and from Option (via the From / Into traits),
however these conversions are not recommended when the wrapped value is large
which defeats the purpose of using Maybe in the first place.
The canonical way to use Maybe with large values is to initialize it in-place with
one of the provided init constructors, and then use one of the as_ref, as_mut,
as_deref and as_deref_mut methods to access the wrapped value.
Implementations§
Source§impl<T, G> Maybe<T, G>
impl<T, G> Maybe<T, G>
Sourcepub fn new(value: Option<T>) -> Maybe<T, G>
pub fn new(value: Option<T>) -> Maybe<T, G>
Create a new Maybe value from an Option.
Note that when the wrapped value is large, it is recommended instead to use
Maybe::init_none() and Maybe::init_some() to create the Maybe value in-place.
Sourcepub fn init_none() -> impl Init<Maybe<T, G>>
pub fn init_none() -> impl Init<Maybe<T, G>>
Create an in-place initializer for a Maybe value that is empty.
Sourcepub fn init_some<I, E>(value: I) -> impl Init<Maybe<T, G>, E>where
I: Init<T, E>,
pub fn init_some<I, E>(value: I) -> impl Init<Maybe<T, G>, E>where
I: Init<T, E>,
Create an in-place initializer for a Maybe value that is not empty
by initializing the wrapped value with the provided initializer.
Sourcepub fn init<I, E>(value: Option<I>) -> impl Init<Maybe<T, G>, E>where
I: Init<T, E>,
pub fn init<I, E>(value: Option<I>) -> impl Init<Maybe<T, G>, E>where
I: Init<T, E>,
Create an in-place initializer for a Maybe value that might or might
not be empty.
Sourcepub fn reinit<I>(&mut self, value: I)
pub fn reinit<I>(&mut self, value: I)
Re-initialize the Maybe value with a new in-place initializer.
Sourcepub fn try_reinit<I, E>(&mut self, value: I) -> Result<(), E>
pub fn try_reinit<I, E>(&mut self, value: I) -> Result<(), E>
Try to re-initialize the Maybe value with a new in-place initializer.
If the re-initialization fails, the Maybe value is left to none.
Sourcepub fn as_mut(&mut self) -> Maybe<&mut T, G>
pub fn as_mut(&mut self) -> Maybe<&mut T, G>
Return a mutable reference to the wrapped value, if it exists.
Sourcepub fn as_opt_mut(&mut self) -> Option<&mut T>
pub fn as_opt_mut(&mut self) -> Option<&mut T>
Return - as an Option - a mutable reference to the wrapped value, if it exists.
Sourcepub fn as_opt_ref(&self) -> Option<&T>
pub fn as_opt_ref(&self) -> Option<&T>
Return - as an Option - a reference to the wrapped value, if it exists.
Sourcepub fn as_deref(&self) -> Maybe<&<T as Deref>::Target, G>where
T: Deref,
pub fn as_deref(&self) -> Maybe<&<T as Deref>::Target, G>where
T: Deref,
Derefs the wrapped value, if it exists.
Sourcepub fn as_deref_mut(&mut self) -> Maybe<&mut <T as Deref>::Target, G>where
T: DerefMut,
pub fn as_deref_mut(&mut self) -> Maybe<&mut <T as Deref>::Target, G>where
T: DerefMut,
Derefs mutably the wrapped value, if it exists.
Sourcepub fn as_opt_deref(&self) -> Option<&<T as Deref>::Target>where
T: Deref,
pub fn as_opt_deref(&self) -> Option<&<T as Deref>::Target>where
T: Deref,
Derefs - as an Option - the wrapped value, if it exists.
Sourcepub fn as_opt_deref_mut(&mut self) -> Option<&mut <T as Deref>::Target>where
T: DerefMut,
pub fn as_opt_deref_mut(&mut self) -> Option<&mut <T as Deref>::Target>where
T: DerefMut,
Derefs - as an Option - mutably the wrapped value, if it exists.
Sourcepub fn into_option(self) -> Option<T>
pub fn into_option(self) -> Option<T>
Consume the Maybe value and return the wrapped value, if it exists.
Note that this method is not efficient when the wrapped value is large (might result in big stack memory usage due to moves), hence its usage is not recommended when the wrapped value is large.
Trait Implementations§
impl<T, G> Eq for Maybe<T, G>where
T: Eq,
Source§impl<'a, T> FromTLV<'a> for Maybe<T, AsNullable>where
T: FromTLV<'a>,
impl<'a, T> FromTLV<'a> for Maybe<T, AsNullable>where
T: FromTLV<'a>,
Source§fn from_tlv(element: &TLVElement<'a>) -> Result<Maybe<T, AsNullable>, Error>
fn from_tlv(element: &TLVElement<'a>) -> Result<Maybe<T, AsNullable>, Error>
Source§fn init_from_tlv(
element: TLVElement<'a>,
) -> impl Init<Maybe<T, AsNullable>, Error>
fn init_from_tlv( element: TLVElement<'a>, ) -> impl Init<Maybe<T, AsNullable>, Error>
Source§fn nullable_from_tlv(element: &TLVElement<'a>) -> Result<Self, Error>
fn nullable_from_tlv(element: &TLVElement<'a>) -> Result<Self, Error>
Source§fn init_nullable_from_tlv(element: TLVElement<'a>) -> impl Init<Self, Error>
fn init_nullable_from_tlv(element: TLVElement<'a>) -> impl Init<Self, Error>
Source§impl<'a, T> FromTLV<'a> for Maybe<T>where
T: FromTLV<'a> + 'a,
impl<'a, T> FromTLV<'a> for Maybe<T>where
T: FromTLV<'a> + 'a,
Source§fn from_tlv(element: &TLVElement<'a>) -> Result<Maybe<T>, Error>
fn from_tlv(element: &TLVElement<'a>) -> Result<Maybe<T>, Error>
Source§fn init_from_tlv(element: TLVElement<'a>) -> impl Init<Maybe<T>, Error>
fn init_from_tlv(element: TLVElement<'a>) -> impl Init<Maybe<T>, Error>
Source§fn nullable_from_tlv(element: &TLVElement<'a>) -> Result<Self, Error>
fn nullable_from_tlv(element: &TLVElement<'a>) -> Result<Self, Error>
Source§fn init_nullable_from_tlv(element: TLVElement<'a>) -> impl Init<Self, Error>
fn init_nullable_from_tlv(element: TLVElement<'a>) -> impl Init<Self, Error>
Source§impl<T> ToTLV for Maybe<T, AsNullable>where
T: ToTLV,
impl<T> ToTLV for Maybe<T, AsNullable>where
T: ToTLV,
Source§fn to_tlv<W>(&self, tag: &TLVTag, tw: W) -> Result<(), Error>where
W: TLVWrite,
fn to_tlv<W>(&self, tag: &TLVTag, tw: W) -> Result<(), Error>where
W: TLVWrite,
Source§fn tlv_iter(&self, tag: TLVTag) -> impl Iterator<Item = Result<TLV<'_>, Error>>
fn tlv_iter(&self, tag: TLVTag) -> impl Iterator<Item = Result<TLV<'_>, Error>>
TLV instances by potentially borrowing
data from the type.Source§impl<T> ToTLV for Maybe<T>where
T: ToTLV,
impl<T> ToTLV for Maybe<T>where
T: ToTLV,
Source§fn to_tlv<W>(&self, tag: &TLVTag, tw: W) -> Result<(), Error>where
W: TLVWrite,
fn to_tlv<W>(&self, tag: &TLVTag, tw: W) -> Result<(), Error>where
W: TLVWrite,
Source§fn tlv_iter(&self, tag: TLVTag) -> impl Iterator<Item = Result<TLV<'_>, Error>>
fn tlv_iter(&self, tag: TLVTag) -> impl Iterator<Item = Result<TLV<'_>, Error>>
TLV instances by potentially borrowing
data from the type.Auto Trait Implementations§
impl<T, G> Freeze for Maybe<T, G>where
T: Freeze,
impl<T, G> RefUnwindSafe for Maybe<T, G>where
G: RefUnwindSafe,
T: RefUnwindSafe,
impl<T, G> Send for Maybe<T, G>
impl<T, G> Sync for Maybe<T, G>
impl<T, G> Unpin for Maybe<T, G>
impl<T, G> UnsafeUnpin for Maybe<T, G>where
T: UnsafeUnpin,
impl<T, G> UnwindSafe for Maybe<T, G>where
G: UnwindSafe,
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more