Created
January 25, 2019 01:58
-
-
Save 0e4ef622/02aa6d8d19df8d021c191350089e306a to your computer and use it in GitHub Desktop.
jank
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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