Macro core_extensions::getconst
source · [−]macro_rules! getconst {
(
$(:: $(@$leading:tt@)? )? $($path:ident)::* <..>
) => { ... };
($ty:ty) => { ... };
}
Available on crate feature
const_val
only.Expand description
Gets the ConstVal::VAL
associated constant for a type.
Use this macro to unambiguously use the ConstVal::VAL
associated constant,
as opposed to an inherent VAL
associated constant,
or a VAL
associated constant from another trait.
Examples
Quasiconstants
Using the quasiconst
macro to declare (generic) constants.
use core_extensions::{getconst, quasiconst, IntegerExt};
#[derive(Debug, PartialEq)]
pub struct Single<T>(pub T);
quasiconst!{const Foo: &'static str = "hello"}
quasiconst!{const Bar: &'static str = "world"}
quasiconst!{const SINGLE_INT[T: IntegerExt = u8]: Single<T> = Single(T::ONE) }
assert_eq!(getconst!(Foo), "hello");
assert_eq!(getconst!(Bar), "world");
// `SINGLE_INT` == `SINGLE_INT<u8>`, because of the defaulted type parameter
assert_eq!(getconst!(SINGLE_INT), Single(1_u8));
assert_eq!(getconst!(SINGLE_INT<u16>), Single(1_u16));
assert_eq!(getconst!(SINGLE_INT<_>), Single(1_i8));
// `Type<..>` is special syntax from `getconst`, to infer all generic parameters.
assert_eq!(getconst!(SINGLE_INT<..>), Single(1u128));
Inherent VAL
associated constant
This demonstrates how inherent associated constants have priority over trait associated constants.
use core_extensions::{ConstVal, getconst};
#[derive(Debug, PartialEq)]
struct Foo(u32);
impl ConstVal for Foo {
type Ty = &'static str;
const VAL: Self::Ty = "hello";
}
impl Foo {
const VAL: &'static str = "world";
}
assert_eq!(getconst!(Foo), "hello");
assert_eq!(<Foo as ConstVal>::VAL, "hello");
assert_eq!(Foo::VAL, "world");