attr_of 0.1.0

Inspect attributes of variables like type and size.
Documentation
/// 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(&()), "()");
    }
}