attr_of/
lib.rs

1/// Returns the size of `T`.
2///
3/// ```
4/// use attr_of::size_of;
5/// assert_eq!(size_of(&1i64), 8);
6/// assert_eq!(size_of(&()), 0);
7/// ```
8pub const fn size_of<T>(_: &T) -> usize {
9	std::mem::size_of::<T>()
10}
11
12/// Returns the name of `T`.
13///
14/// ```
15/// use attr_of::type_of;
16/// assert_eq!(type_of(&String::new()), "alloc::string::String");
17/// assert_eq!(type_of(&1), "i32");
18/// ```
19pub fn type_of<T>(_: &T) -> &'static str {
20	std::any::type_name::<T>()
21}
22
23#[cfg(test)]
24mod tests {
25	use super::*;
26
27    #[test]
28    fn size_of_test() {
29        assert_eq!(size_of(&1), 4);
30        assert_eq!(size_of(&1i64), 8);
31        assert_eq!(size_of(&()), 0);
32    }
33
34    #[test]
35    fn type_of_test() {
36        assert_eq!(type_of(&String::new()), "alloc::string::String");
37        assert_eq!(type_of(&1), "i32");
38        assert_eq!(type_of(&()), "()");
39    }
40}