Struct typewit::const_marker::Usize

source ·
pub struct Usize<const VAL: usize>;
Expand description

Marker type for passing const VAL: usize as a type parameter.

Implementations§

source§

impl<const VAL: usize> Usize<VAL>

source

pub const fn eq<const OTHER: usize>( self, _other: Usize<OTHER> ) -> Result<TypeEq<Usize<VAL>, Usize<OTHER>>, TypeNe<Usize<VAL>, Usize<OTHER>>>

👎Deprecated since 1.8.0: superceeded by equals method

Compares self and other for equality.

Returns:

  • Ok(TypeEq): if VAL == OTHER
  • Err(TypeNe): if VAL != OTHER
source

pub const fn equals<const OTHER: usize>( self, _other: Usize<OTHER> ) -> TypeCmp<Usize<VAL>, Usize<OTHER>>

Compares self and other for equality.

Returns:

  • TypeCmp::Eq(TypeEq): if VAL == OTHER
  • TypeCmp::Ne(TypeNe): if VAL != OTHER
Examples
Array

This example demonstrates how Usize can be used to specialize behavior on array length.

(this example requires Rust 1.61.0, because it uses trait bounds in const fns)

 use typewit::{const_marker::Usize, TypeCmp, TypeEq};
 
 assert_eq!(try_from_pair::<_, 0>((3, 5)), Ok([]));
 assert_eq!(try_from_pair::<_, 1>((3, 5)), Ok([3]));
 assert_eq!(try_from_pair::<_, 2>((3, 5)), Ok([3, 5]));
 assert_eq!(try_from_pair::<_, 3>((3, 5)), Err((3, 5)));
 
 
 const fn try_from_pair<T: Copy, const LEN: usize>(pair: (T, T)) -> Result<[T; LEN], (T, T)> {
     if let TypeCmp::Eq(te_len) = Usize::<LEN>.equals(Usize::<0>) {
         // this branch is ran on `LEN == 0`
         // `te_len` is a `TypeEq<Usize<LEN>, Usize<0>>`
         Ok(
             TypeEq::new::<T>()    // `TypeEq<T, T>`
                 .in_array(te_len) // `TypeEq<[T; LEN], [T; 0]>`
                 .to_left([])      // Goes from `[T; 0]` to `[T; LEN]`
         )
     } else if let TypeCmp::Eq(te_len) = Usize.equals(Usize) {
         // this branch is ran on `LEN == 1`
         // `te_len` is inferred to be `TypeEq<Usize<LEN>, Usize<1>>`
         Ok(TypeEq::NEW.in_array(te_len).to_left([pair.0]))
     } else if let TypeCmp::Eq(te_len) = Usize.equals(Usize) {
         // this branch is ran on `LEN == 2`
         // `te_len` is inferred to be `TypeEq<Usize<LEN>, Usize<2>>`
         Ok(TypeEq::NEW.in_array(te_len).to_left([pair.0, pair.1]))
     } else {
         Err(pair)
     }
 }
 
Struct

This example demonstrates how Usize can be used to pass a const-generic struct to a function expecting a concrete type of that struct.

use typewit::{const_marker::Usize, TypeCmp};

assert_eq!(mutate(Array([])), Array([]));
assert_eq!(mutate(Array([3])), Array([3]));
assert_eq!(mutate(Array([3, 5])), Array([3, 5]));
assert_eq!(mutate(Array([3, 5, 8])), Array([8, 5, 3])); // reversed!
assert_eq!(mutate(Array([3, 5, 8, 13])), Array([3, 5, 8, 13]));


#[derive(Debug, PartialEq)]
struct Array<const CAP: usize>([u32; CAP]);

const fn mutate<const LEN: usize>(arr: Array<LEN>) -> Array<LEN> {
    match Usize::<LEN>.equals(Usize::<3>) {
        // `te_len` is a `TypeEq<Usize<LEN>, Usize<3>>`
        // this branch is ran on `LEN == 3`
        TypeCmp::Eq(te_len) => {
            // `te` is a `TypeEq<Array<LEN>, Array<3>>`
            let te = te_len.project::<GArray>();

            // `te.to_right(...)` here goes from `Array<LEN>` to `Array<3>`
            let ret = reverse3(te.to_right(arr));

            // `te.to_left(...)` here goes from `Array<3>` to `Array<LEN>`
            te.to_left(ret)
        }
        TypeCmp::Ne(_) => arr,
    }
}

const fn reverse3(Array([a, b, c]): Array<3>) -> Array<3> {
    Array([c, b, a])
}

typewit::type_fn!{
    // Type-level function from `Usize<LEN>` to `Array<LEN>`
    struct GArray;

    impl<const LEN: usize> Usize<LEN> => Array<LEN>
}

Trait Implementations§

source§

impl<const VAL: usize> Clone for Usize<VAL>

source§

fn clone(&self) -> Usize<VAL>

Returns a copy of the value. Read more
1.0.0 · source§

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

Performs copy-assignment from source. Read more
source§

impl<const VAL: usize> Debug for Usize<VAL>

source§

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

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

impl<const VAL: usize> Copy for Usize<VAL>

Auto Trait Implementations§

§

impl<const VAL: usize> RefUnwindSafe for Usize<VAL>

§

impl<const VAL: usize> Send for Usize<VAL>

§

impl<const VAL: usize> Sync for Usize<VAL>

§

impl<const VAL: usize> Unpin for Usize<VAL>

§

impl<const VAL: usize> UnwindSafe for Usize<VAL>

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, W> HasTypeWitness<W> for T
where W: MakeTypeWitness<Arg = T>, T: ?Sized,

source§

const WITNESS: W = const WITNESS: W = W::MAKE;

A constant of the type witness
source§

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

§

type Type = T

The same type as Self, used to emulate type equality bounds (T == U) with associated type equality constraints (T: Identity<Type = U>).
source§

const TYPE_EQ: TypeEq<T, <T as Identity>::Type> = const TYPE_EQ: TypeEq<Self, Self::Type> = TypeEq::NEW;

Proof that Self is the same type as Self::Type, provides methods for casting between Self and Self::Type.
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,

§

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

§

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

§

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.