1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/// Returns the size of `T`.
///
/// ```
/// use attr_of::size_of;
/// assert_eq!(size_of(&1i64), 8);
/// assert_eq!(size_of(&()), 0);
/// ```
pub const fn size_of<T>(_: &T) -> usize {
	std::mem::size_of::<T>()
}

/// Returns the name of `T`.
///
/// ```
/// use attr_of::type_of;
/// assert_eq!(type_of(&String::new()), "alloc::string::String");
/// assert_eq!(type_of(&1), "i32");
/// ```
pub fn type_of<T>(_: &T) -> &'static str {
	std::any::type_name::<T>()
}

#[cfg(test)]
mod tests {
	use super::*;

    #[test]
    fn size_of_test() {
        assert_eq!(size_of(&1), 4);
        assert_eq!(size_of(&1i64), 8);
        assert_eq!(size_of(&()), 0);
    }

    #[test]
    fn type_of_test() {
        assert_eq!(type_of(&String::new()), "alloc::string::String");
        assert_eq!(type_of(&1), "i32");
        assert_eq!(type_of(&()), "()");
    }
}