Skip to main content

Maybe

Struct Maybe 

Source
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>

Source

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.

Source

pub const fn none() -> Maybe<T, G>

Create a new, empty Maybe value.

Source

pub const fn some(value: T) -> Maybe<T, G>

Create a new Maybe value with a wrapped value.

Source

pub fn init_none() -> impl Init<Maybe<T, G>>

Create an in-place initializer for a Maybe value that is empty.

Source

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.

Source

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.

Source

pub fn clear(&mut self)

Sets the Maybe value to “none”.

Source

pub fn reinit<I>(&mut self, value: I)
where I: Init<Maybe<T, G>>,

Re-initialize the Maybe value with a new in-place initializer.

Source

pub fn try_reinit<I, E>(&mut self, value: I) -> Result<(), E>
where I: Init<Maybe<T, G>, 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.

Source

pub fn as_mut(&mut self) -> Maybe<&mut T, G>

Return a mutable reference to the wrapped value, if it exists.

Source

pub fn as_ref(&self) -> Maybe<&T, G>

Return a reference to the wrapped value, if it exists.

Source

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

Return - as an Option - a mutable reference to the wrapped value, if it exists.

Source

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

Return - as an Option - a reference to the wrapped value, if it exists.

Source

pub fn as_deref(&self) -> Maybe<&<T as Deref>::Target, G>
where T: Deref,

Derefs the wrapped value, if it exists.

Source

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.

Source

pub fn as_opt_deref(&self) -> Option<&<T as Deref>::Target>
where T: Deref,

Derefs - as an Option - the wrapped value, if it exists.

Source

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.

Source

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.

Source

pub fn is_none(&self) -> bool

Return whether the Maybe value is empty.

Source

pub fn is_some(&self) -> bool

Return whether the Maybe value is not empty.

Trait Implementations§

Source§

impl<T, G> Clone for Maybe<T, G>
where T: Clone,

Source§

fn clone(&self) -> Maybe<T, G>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T, G> Debug for Maybe<T, G>
where T: Debug, G: Debug,

Source§

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

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

impl<T, G> Default for Maybe<T, G>

Source§

fn default() -> Maybe<T, G>

Returns the “default value” for a type. Read more
Source§

impl<T, G> Drop for Maybe<T, G>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl<T, G> Eq for Maybe<T, G>
where T: Eq,

Source§

impl<T, G> From<Maybe<T, G>> for Option<T>

Source§

fn from(value: Maybe<T, G>) -> Option<T>

Converts to this type from the input type.
Source§

impl<T, G> From<Option<T>> for Maybe<T, G>

Source§

fn from(value: Option<T>) -> Maybe<T, G>

Converts to this type from the input type.
Source§

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>

Deserialize the type from a TLV-encoded element.
Source§

fn init_from_tlv( element: TLVElement<'a>, ) -> impl Init<Maybe<T, AsNullable>, Error>

Generate an in-place initializer for the type that initializes the type from a TLV-encoded element.
Source§

fn nullable_from_tlv(element: &TLVElement<'a>) -> Result<Self, Error>

Deserialize the type from a TLV-encoded element. Read more
Source§

fn init_nullable_from_tlv(element: TLVElement<'a>) -> impl Init<Self, Error>

Generate an in-place initializer for the type that initializes the type from a TLV-encoded element. Read more
Source§

impl<'a, T> FromTLV<'a> for Maybe<T>
where T: FromTLV<'a> + 'a,

Source§

fn from_tlv(element: &TLVElement<'a>) -> Result<Maybe<T>, Error>

Deserialize the type from a TLV-encoded element.
Source§

fn init_from_tlv(element: TLVElement<'a>) -> impl Init<Maybe<T>, Error>

Generate an in-place initializer for the type that initializes the type from a TLV-encoded element.
Source§

fn nullable_from_tlv(element: &TLVElement<'a>) -> Result<Self, Error>

Deserialize the type from a TLV-encoded element. Read more
Source§

fn init_nullable_from_tlv(element: TLVElement<'a>) -> impl Init<Self, Error>

Generate an in-place initializer for the type that initializes the type from a TLV-encoded element. Read more
Source§

impl<T, G> Hash for Maybe<T, G>
where T: Hash,

Source§

fn hash<H>(&self, state: &mut H)
where H: Hasher,

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<T, G> PartialEq for Maybe<T, G>
where T: PartialEq,

Source§

fn eq(&self, other: &Maybe<T, G>) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

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,

Serialize the type to a TLV-encoded stream.
Source§

fn tlv_iter(&self, tag: TLVTag) -> impl Iterator<Item = Result<TLV<'_>, Error>>

Serialize the type as an iterator of TLV instances by potentially borrowing data from the type.
Source§

fn nullable_to_tlv<W>(&self, tag: &TLVTag, tw: W) -> Result<(), Error>
where W: TLVWrite,

Serialize the type to a TLV-encoded stream. Read more
Source§

fn nullable_tlv_iter( &self, tag: TLVTag, ) -> impl Iterator<Item = Result<TLV<'_>, Error>>

Serialize the type as an iterator of TLV instances by potentially borrowing data from the type. Read more
Source§

impl<T> ToTLV for Maybe<T>
where T: ToTLV,

Source§

fn to_tlv<W>(&self, tag: &TLVTag, tw: W) -> Result<(), Error>
where W: TLVWrite,

Serialize the type to a TLV-encoded stream.
Source§

fn tlv_iter(&self, tag: TLVTag) -> impl Iterator<Item = Result<TLV<'_>, Error>>

Serialize the type as an iterator of TLV instances by potentially borrowing data from the type.
Source§

fn nullable_to_tlv<W>(&self, tag: &TLVTag, tw: W) -> Result<(), Error>
where W: TLVWrite,

Serialize the type to a TLV-encoded stream. Read more
Source§

fn nullable_tlv_iter( &self, tag: TLVTag, ) -> impl Iterator<Item = Result<TLV<'_>, Error>>

Serialize the type as an iterator of TLV instances by potentially borrowing data from the type. Read more

Auto Trait Implementations§

§

impl<T, G> Freeze for Maybe<T, G>
where T: Freeze,

§

impl<T, G> RefUnwindSafe for Maybe<T, G>

§

impl<T, G> Send for Maybe<T, G>
where G: Send, T: Send,

§

impl<T, G> Sync for Maybe<T, G>
where G: Sync, T: Sync,

§

impl<T, G> Unpin for Maybe<T, G>
where G: Unpin, T: Unpin,

§

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> 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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, E> Init<T, E> for T

Source§

unsafe fn __init(self, slot: *mut T) -> Result<(), E>

Initializes slot. Read more
Source§

fn chain<F>(self, f: F) -> ChainInit<Self, F, T, E>
where F: FnOnce(&mut T) -> Result<(), E>,

First initializes the value using self then calls the function f with the initialized value. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T, I> IntoFallibleInit<T> for I
where I: Init<T>,

Source§

fn into_fallible<E>(self) -> impl Init<T, E>

Convert the infallible initializer to a fallible one.
Source§

impl<Source, Target> OctetsInto<Target> for Source
where Target: OctetsFrom<Source>,

Source§

type Error = <Target as OctetsFrom<Source>>::Error

Source§

fn try_octets_into( self, ) -> Result<Target, <Source as OctetsInto<Target>>::Error>

Performs the conversion.
Source§

fn octets_into(self) -> Target
where Self::Error: Into<Infallible>,

Performs an infallible conversion.
Source§

impl<T, E> PinInit<T, E> for T

Source§

unsafe fn __pinned_init(self, slot: *mut T) -> Result<(), E>

Initializes slot. Read more
Source§

fn pin_chain<F>(self, f: F) -> ChainPinInit<Self, F, T, E>
where F: FnOnce(Pin<&mut T>) -> Result<(), E>,

First initializes the value using self then calls the function f with the initialized value. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V