Trait ConstMarker

Source
pub trait ConstMarker: Sized {
    type Of;

    const VAL: Self::Of;
}
Expand description

A type that represents a constant

§Example

Emulating generic associated constants on stable

(this example Requires Rust 1.65.0)

use typewit::const_marker::{ConstMarker, ConstMarkerOf};
 
 
assert_eq!(make_array::<u8>(), ([3], [3, 3, 3]));
 
assert_eq!(make_array::<&str>(), (["hi"], ["hi", "hi", "hi"]));
 
const fn make_array<T: MakeArray>() -> ([T; 1], [T; 3]) {
    (T::Make::<1>::VAL, T::Make::<3>::VAL)
}
 
// `MakeArray` can make arrays of any length without writing a bound for each length!!
trait MakeArray: Sized {
    // emulates `const Make<const N: usize>: [Self; N];` (a generic associated constant)
    type Make<const N: usize>: ConstMarkerOf<[Self; N]>;
}
 
 
impl MakeArray for u8 {
    type Make<const N: usize> = MakeArrayConst<Self, N>;
}

impl<'a> MakeArray for &'a str {
    type Make<const N: usize> = MakeArrayConst<Self, N>;
}
 
struct MakeArrayConst<T, const N: usize>(core::marker::PhantomData<T>);
 
impl<const N: usize> ConstMarker for MakeArrayConst<u8, N> {
    type Of = [u8; N];
    const VAL: Self::Of = [3; N];
}
 
impl<'a, const N: usize> ConstMarker for MakeArrayConst<&'a str, N> {
    type Of = [&'a str; N];
    const VAL: Self::Of = ["hi"; N];
}
 

Required Associated Constants§

Source

const VAL: Self::Of

The value of this constant

Required Associated Types§

Source

type Of

The type of this constant

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<const VAL: bool> ConstMarker for Bool<VAL>

Source§

const VAL: Self::Of = VAL

Source§

type Of = bool

Source§

impl<const VAL: char> ConstMarker for Char<VAL>

Source§

const VAL: Self::Of = VAL

Source§

type Of = char

Source§

impl<const VAL: i8> ConstMarker for I8<VAL>

Source§

const VAL: Self::Of = VAL

Source§

type Of = i8

Source§

impl<const VAL: i16> ConstMarker for I16<VAL>

Source§

const VAL: Self::Of = VAL

Source§

type Of = i16

Source§

impl<const VAL: i32> ConstMarker for I32<VAL>

Source§

const VAL: Self::Of = VAL

Source§

type Of = i32

Source§

impl<const VAL: i64> ConstMarker for I64<VAL>

Source§

const VAL: Self::Of = VAL

Source§

type Of = i64

Source§

impl<const VAL: i128> ConstMarker for I128<VAL>

Source§

const VAL: Self::Of = VAL

Source§

type Of = i128

Source§

impl<const VAL: isize> ConstMarker for Isize<VAL>

Source§

const VAL: Self::Of = VAL

Source§

type Of = isize

Source§

impl<const VAL: u8> ConstMarker for U8<VAL>

Source§

const VAL: Self::Of = VAL

Source§

type Of = u8

Source§

impl<const VAL: u16> ConstMarker for U16<VAL>

Source§

const VAL: Self::Of = VAL

Source§

type Of = u16

Source§

impl<const VAL: u32> ConstMarker for U32<VAL>

Source§

const VAL: Self::Of = VAL

Source§

type Of = u32

Source§

impl<const VAL: u64> ConstMarker for U64<VAL>

Source§

const VAL: Self::Of = VAL

Source§

type Of = u64

Source§

impl<const VAL: u128> ConstMarker for U128<VAL>

Source§

const VAL: Self::Of = VAL

Source§

type Of = u128

Source§

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

Source§

const VAL: Self::Of = VAL

Source§

type Of = usize