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>
impl<const VAL: usize> Usize<VAL>
Sourcepub 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
pub const fn eq<const OTHER: usize>( self, _other: Usize<OTHER>, ) -> Result<TypeEq<Usize<VAL>, Usize<OTHER>>, TypeNe<Usize<VAL>, Usize<OTHER>>>
equals
methodCompares self
and other
for equality.
Returns:
Ok(TypeEq)
: ifVAL == OTHER
Err(TypeNe)
: ifVAL != OTHER
Sourcepub const fn equals<const OTHER: usize>(
self,
_other: Usize<OTHER>,
) -> TypeCmp<Usize<VAL>, Usize<OTHER>>
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)
: ifVAL == OTHER
TypeCmp::Ne(TypeNe)
: ifVAL != 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§
impl<const VAL: usize> Copy for Usize<VAL>
Auto Trait Implementations§
impl<const VAL: usize> Freeze for Usize<VAL>
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> 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