Skip to content

Instantly share code, notes, and snippets.

@0e4ef622
Created January 25, 2019 01:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 0e4ef622/02aa6d8d19df8d021c191350089e306a to your computer and use it in GitHub Desktop.
Save 0e4ef622/02aa6d8d19df8d021c191350089e306a to your computer and use it in GitHub Desktop.
jank
pub trait TypeName {
const NAME: &'static str;
}
macro_rules! typename {
($($typ:ty,)*) => {
$(impl TypeName for $typ {
const NAME: &'static str = stringify!($typ);
})*
}
}
typename! {
String,
str,
&str,
i32,
Box<String>,
fn(),
}
fn type_name_of_val<T: TypeName + ?Sized>(_: &T) -> &'static str {
T::NAME
}
fn main() {
let a = Box::new(String::from("hello"));
let b = 10;
let c = "potato";
println!("a: {}", type_name_of_val(&a));
println!("b: {}", type_name_of_val(&b));
println!("c: {}", type_name_of_val(&c));
println!("{}", type_name_of_val(""));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment