Skip to content

libcore: wrappers for size/align_of to act on values without needing explicit ::<type> annotations #5970

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 20, 2013
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 57 additions & 1 deletion src/libcore/sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,24 @@ pub fn get_type_desc<T>() -> *TypeDesc {
unsafe { rusti::get_tydesc::<T>() as *TypeDesc }
}

/// Returns a pointer to a type descriptor.
#[inline(always)]
pub fn get_type_desc_val<T>(_val: &T) -> *TypeDesc {
get_type_desc::<T>()
}

/// Returns the size of a type
#[inline(always)]
pub fn size_of<T>() -> uint {
unsafe { rusti::size_of::<T>() }
}

/// Returns the size of the type that `_val` points to
#[inline(always)]
pub fn size_of_val<T>(_val: &T) -> uint {
size_of::<T>()
}

/**
* Returns the size of a type, or 1 if the actual size is zero.
*
Expand All @@ -100,6 +112,13 @@ pub fn nonzero_size_of<T>() -> 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<T>(_val: &T) -> uint {
nonzero_size_of::<T>()
}


/**
* Returns the ABI-required minimum alignment of a type
*
Expand All @@ -111,12 +130,26 @@ pub fn min_align_of<T>() -> uint {
unsafe { rusti::min_align_of::<T>() }
}

/// Returns the ABI-required minimum alignment of the type of the value that
/// `_val` points to
#[inline(always)]
pub fn min_align_of_val<T>(_val: &T) -> uint {
min_align_of::<T>()
}

/// Returns the preferred alignment of a type
#[inline(always)]
pub fn pref_align_of<T>() -> uint {
unsafe { rusti::pref_align_of::<T>() }
}

/// Returns the preferred alignment of the type of the value that
/// `_val` points to
#[inline(always)]
pub fn pref_align_of_val<T>(_val: &T) -> uint {
pref_align_of::<T>()
}

/// Returns the refcount of a shared box (as just before calling this)
#[inline(always)]
pub fn refcount<T>(t: @T) -> uint {
Expand Down Expand Up @@ -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() {
Expand All @@ -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];
Expand All @@ -196,6 +237,14 @@ mod tests {
assert!(nonzero_size_of::<uint>() == size_of::<uint>());
}

#[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::<u8>() == 1u);
Expand All @@ -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 {
Expand Down