wiwi::nominal

Struct Nominal

Source
pub struct Nominal<T, M> { /* private fields */ }
Expand description

Nominal wrapper struct

This struct is zero cost; it is simply a type safe wrapper.

Newtypes are primarily created with assistance from the nominal and nominal_mod macros. The macros will help save you the boilerplate of writing the types and declaring unit structs to use as the marker.

Implementations§

Source§

impl<T, M> Nominal<T, M>

Source

pub fn new(item: T) -> Self

Creates a nominal struct with the given value

Source

pub fn into_inner(self) -> T

Unwraps the nominal struct and returns the value

Source

pub fn as_value_ref(&self) -> &T

Gets a reference to the wrapped value

Note: Deref is not implemented on purpose, to prevent unintentional auto-derefs

Source

pub fn as_value_mut(&mut self) -> &mut T

Gets a mut reference to the wrapped value

Note: DerefMut is not implemented on purpose, to prevent unintentional auto-derefs

Source

pub fn with_other_marker<M2>(self) -> Nominal<T, M2>

Unwraps and rewraps the value as another nominal type, without modifying the wrapped value

If you’re using this function, make sure you know why you’re using it! after all, the whole point of this is to seperate otherwise identical types into newtypes based on semantic meaning.

Source

pub fn map<T2, F>(self, f: F) -> Nominal<T2, M>
where F: FnOnce(T) -> T2,

Consumes and maps the wrapped value into another value, wrapping it in a nominal type with the same marker

Source

pub async fn map_async<T2, F, Fu>(self, f: F) -> Nominal<T2, M>
where F: FnOnce(T) -> Fu, Fu: IntoFuture<Output = T2>,

Consumes and asyncronously maps the wrapped value into another value, wrapping it in a nominal type with the same marker

Source§

impl<T, M, E> Nominal<Result<T, E>, M>

Source

pub fn transpose(self) -> Result<Nominal<T, M>, E>

Transpose a nominal wrapped Result into a Result of a nominal wrapped value

The value gets wrapped, but the error does not. Both are not otherwise modified in any way.

Source

pub fn map_result_ok<T2, F>(self, f: F) -> Nominal<Result<T2, E>, M>
where F: FnOnce(T) -> T2,

Maps the Ok value of a Result, wrapping the resulting Result in a nominal type with the same marker

Source

pub fn map_result_err<E2, F>(self, f: F) -> Nominal<Result<T, E2>, M>
where F: FnOnce(E) -> E2,

Maps the Err value of a Result, wrapping the resulting Result in a nominal type with the same marker

Source§

impl<T, M> Nominal<Option<T>, M>

Source

pub fn transpose(self) -> Option<Nominal<T, M>>

Transpose a nominal wrapped Option into an Option of a nominal wrapped value

The value is not otherwise modified in any way.

Source

pub fn map_option_some<T2, F>(self, f: F) -> Nominal<Option<T2>, M>
where F: FnOnce(T) -> T2,

Maps the Some value of an Option, wrapping the resulting Option in a nominal type with the same marker

Trait Implementations§

Source§

impl<T, M> Clone for Nominal<T, M>
where T: Clone,

Source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
Source§

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

Performs copy-assignment from source. Read more
Source§

impl<T, M> Debug for Nominal<T, M>
where T: Debug,

Source§

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

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

impl<T, M> Default for Nominal<T, M>
where T: Default,

Source§

fn default() -> Self

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

impl<T, M> Display for Nominal<T, M>
where T: Display,

Source§

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

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

impl<T, M> From<T> for Nominal<T, M>

Source§

fn from(value: T) -> Self

Converts to this type from the input type.
Source§

impl<T, M> Hash for Nominal<T, M>
where T: Hash,

Source§

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

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

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

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

impl<T, M> Ord for Nominal<T, M>
where T: Ord,

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
Source§

fn max(self, other: Self) -> Self

Compares and returns the maximum of two values. Read more
Source§

fn min(self, other: Self) -> Self

Compares and returns the minimum of two values. Read more
Source§

fn clamp(self, min: Self, max: Self) -> Self

Restrict a value to a certain interval. Read more
Source§

impl<T, M, TR, MR> PartialEq<Nominal<TR, MR>> for Nominal<T, M>
where T: PartialEq<TR>,

Source§

fn eq(&self, other: &Nominal<TR, MR>) -> bool

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

fn ne(&self, other: &Nominal<TR, MR>) -> bool

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

impl<T, M, TR, MR> PartialOrd<Nominal<TR, MR>> for Nominal<T, M>
where T: PartialOrd<TR>,

Source§

fn partial_cmp(&self, other: &Nominal<TR, MR>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
Source§

fn lt(&self, other: &Nominal<TR, MR>) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
Source§

fn le(&self, other: &Nominal<TR, MR>) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
Source§

fn gt(&self, other: &Nominal<TR, MR>) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
Source§

fn ge(&self, other: &Nominal<TR, MR>) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<T, M> Copy for Nominal<T, M>
where T: Copy,

Source§

impl<T, M> Eq for Nominal<T, M>
where T: Eq,

Auto Trait Implementations§

§

impl<T, M> Freeze for Nominal<T, M>
where T: Freeze,

§

impl<T, M> RefUnwindSafe for Nominal<T, M>

§

impl<T, M> Send for Nominal<T, M>
where T: Send, M: Send,

§

impl<T, M> Sync for Nominal<T, M>
where T: Sync, M: Sync,

§

impl<T, M> Unpin for Nominal<T, M>
where T: Unpin, M: Unpin,

§

impl<T, M> UnwindSafe for Nominal<T, M>
where T: UnwindSafe, M: 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

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

impl<Q, K> Comparable<K> for Q
where Q: Ord + ?Sized, K: Borrow<Q> + ?Sized,

§

fn compare(&self, key: &K) -> Ordering

Compare self to key and return their ordering.
Source§

impl<T> Encode for T

Source§

fn encode<E>(self) -> <E as Encoding>::EncodeOutput
where T: Encodable<E>, E: Encoding,

Source§

fn decode<E>(self) -> <E as Encoding>::DecodeOutput
where T: Decodable<E>, E: Encoding,

§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<!> for T

Source§

fn from(t: !) -> T

Converts to this type from the input type.
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> 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> ToString for T
where T: Display + ?Sized,

Source§

default fn to_string(&self) -> String

Converts the given value to a String. 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.