pub trait Named {
fn name(&self) -> &'static str;
}
#[macro_export]
macro_rules! define_named {
($type:ident, $name:literal) => {
impl $crate::named::Named for $type {
fn name(&self) -> &'static str {
$name
}
}
};
($type:ident<$($param:ident $(: $bound:tt)?),+>, $name:literal) => {
impl<$($param $(: $bound)?),+> $crate::named::Named for $type<$($param),+> {
fn name(&self) -> &'static str {
$name
}
}
};
}
#[macro_export]
macro_rules! impl_named_field {
($type:ident) => {
impl $crate::named::Named for $type {
fn name(&self) -> &'static str {
self.name
}
}
};
}
#[macro_export]
macro_rules! impl_named_field_generic {
($type:ident, $param:ident: $($bound:tt)+) => {
impl<$param: $($bound)+> $crate::named::Named for $type<$param> {
fn name(&self) -> &'static str {
self.name
}
}
};
}
#[cfg(test)]
mod tests {
use super::*;
pub struct PlainType;
define_named!(PlainType, "plain");
pub struct GenericOne<T>(pub T);
define_named!(GenericOne<T>, "generic-one");
pub struct GenericTwo<T, U>(pub T, pub U);
define_named!(GenericTwo<T, U>, "generic-two");
pub struct BoundedGeneric<T: Send>(pub T);
define_named!(BoundedGeneric<T: Send>, "bounded-generic");
pub struct FieldNonGeneric {
name: &'static str,
#[allow(dead_code)]
payload: u32,
}
crate::impl_named_field!(FieldNonGeneric);
pub struct FieldGenericSimple<T: Clone> {
name: &'static str,
#[allow(dead_code)]
payload: T,
}
crate::impl_named_field_generic!(FieldGenericSimple, T: Clone);
pub struct FieldGenericMultiBound<T: Clone + Send + Sync + 'static> {
name: &'static str,
#[allow(dead_code)]
payload: T,
}
crate::impl_named_field_generic!(FieldGenericMultiBound, T: Clone + Send + Sync + 'static);
#[test]
fn plain_type_named() {
let p = PlainType;
assert_eq!(p.name(), "plain");
}
#[test]
fn generic_one_named() {
let g: GenericOne<u32> = GenericOne(42);
assert_eq!(g.name(), "generic-one");
}
#[test]
fn generic_two_named() {
let g: GenericTwo<u32, String> = GenericTwo(1, "x".into());
assert_eq!(g.name(), "generic-two");
}
#[test]
fn bounded_generic_named() {
let g: BoundedGeneric<u64> = BoundedGeneric(5);
assert_eq!(g.name(), "bounded-generic");
}
fn telemetry<N: Named>(n: &N) -> &'static str {
n.name()
}
#[test]
fn impl_named_field_macro_reads_self_name() {
let f = FieldNonGeneric {
name: "non-generic-field",
payload: 0,
};
assert_eq!(f.name(), "non-generic-field");
}
#[test]
fn impl_named_field_generic_simple_bound() {
let f: FieldGenericSimple<u32> = FieldGenericSimple {
name: "simple-bound",
payload: 42,
};
assert_eq!(f.name(), "simple-bound");
}
#[test]
fn impl_named_field_generic_multi_bound() {
let f: FieldGenericMultiBound<String> = FieldGenericMultiBound {
name: "multi-bound",
payload: "x".into(),
};
assert_eq!(f.name(), "multi-bound");
}
#[test]
fn polymorphic_dispatch_via_trait() {
let p = PlainType;
let g: GenericOne<bool> = GenericOne(true);
assert_eq!(telemetry(&p), "plain");
assert_eq!(telemetry(&g), "generic-one");
}
#[test]
fn dyn_dispatch_via_trait_object() {
let p = PlainType;
let g: GenericTwo<u8, u8> = GenericTwo(0, 0);
let names: Vec<&dyn Named> = vec![&p, &g];
let collected: Vec<&'static str> = names.iter().map(|n| n.name()).collect();
assert_eq!(collected, vec!["plain", "generic-two"]);
}
#[test]
fn substrate_names_use_kebab_or_snake_case() {
for name in ["plain", "generic-one", "generic-two", "bounded-generic"] {
assert!(
name.chars()
.all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == '-' || c == '_'),
"name {name} must be kebab- or snake-case"
);
}
}
}