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§
Required Associated Types§
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".