From 5c2e9b29f10694ad76dcb6b88e95243a44813c05 Mon Sep 17 00:00:00 2001 From: Huon Wilson Date: Sat, 20 Apr 2013 14:34:18 +1000 Subject: [PATCH] libcore: wrappers for size/align_of to act on values without needing explicit :: annotations --- src/libcore/sys.rs | 58 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) diff --git a/src/libcore/sys.rs b/src/libcore/sys.rs index 04f96f5eb22..c4ec83aa176 100644 --- a/src/libcore/sys.rs +++ b/src/libcore/sys.rs @@ -83,12 +83,24 @@ pub fn get_type_desc() -> *TypeDesc { unsafe { rusti::get_tydesc::() as *TypeDesc } } +/// Returns a pointer to a type descriptor. +#[inline(always)] +pub fn get_type_desc_val(_val: &T) -> *TypeDesc { + get_type_desc::() +} + /// Returns the size of a type #[inline(always)] pub fn size_of() -> uint { unsafe { rusti::size_of::() } } +/// Returns the size of the type that `_val` points to +#[inline(always)] +pub fn size_of_val(_val: &T) -> uint { + size_of::() +} + /** * Returns the size of a type, or 1 if the actual size is zero. * @@ -100,6 +112,13 @@ pub fn nonzero_size_of() -> uint { if s == 0 { 1 } else { s } } +/// Returns the size of the type of the value that `_val` points to +#[inline(always)] +pub fn nonzero_size_of_val(_val: &T) -> uint { + nonzero_size_of::() +} + + /** * Returns the ABI-required minimum alignment of a type * @@ -111,12 +130,26 @@ pub fn min_align_of() -> uint { unsafe { rusti::min_align_of::() } } +/// Returns the ABI-required minimum alignment of the type of the value that +/// `_val` points to +#[inline(always)] +pub fn min_align_of_val(_val: &T) -> uint { + min_align_of::() +} + /// Returns the preferred alignment of a type #[inline(always)] pub fn pref_align_of() -> uint { unsafe { rusti::pref_align_of::() } } +/// Returns the preferred alignment of the type of the value that +/// `_val` points to +#[inline(always)] +pub fn pref_align_of_val(_val: &T) -> uint { + pref_align_of::() +} + /// Returns the refcount of a shared box (as just before calling this) #[inline(always)] pub fn refcount(t: @T) -> uint { @@ -162,7 +195,7 @@ pub fn fail_assert(msg: &str, file: &str, line: uint) -> ! { #[cfg(test)] mod tests { use cast; - use sys::{Closure, pref_align_of, size_of, nonzero_size_of}; + use sys::*; #[test] fn size_of_basic() { @@ -188,6 +221,14 @@ mod tests { assert!(size_of::<*uint>() == 8u); } + #[test] + fn size_of_val_basic() { + assert_eq!(size_of_val(&1u8), 1); + assert_eq!(size_of_val(&1u16), 2); + assert_eq!(size_of_val(&1u32), 4); + assert_eq!(size_of_val(&1u64), 8); + } + #[test] fn nonzero_size_of_basic() { type Z = [i8, ..0]; @@ -196,6 +237,14 @@ mod tests { assert!(nonzero_size_of::() == size_of::()); } + #[test] + fn nonzero_size_of_val_basic() { + let z = [0u8, ..0]; + assert_eq!(size_of_val(&z), 0u); + assert_eq!(nonzero_size_of_val(&z), 1u); + assert_eq!(nonzero_size_of_val(&1u), size_of_val(&1u)); + } + #[test] fn align_of_basic() { assert!(pref_align_of::() == 1u); @@ -219,6 +268,13 @@ mod tests { assert!(pref_align_of::<*uint>() == 8u); } + #[test] + fn align_of_val_basic() { + assert_eq!(pref_align_of_val(&1u8), 1u); + assert_eq!(pref_align_of_val(&1u16), 2u); + assert_eq!(pref_align_of_val(&1u32), 4u); + } + #[test] fn synthesize_closure() { unsafe {