Skip to content

Commit f2b0ef1

Browse files
committed
auto merge of #5970 : huonw/rust/core-sys-size_of-val, r=pcwalton
This allows one to write ```rust let x = function_with_complicated_return_type(); let size = size_of_val(&x); ``` instead of ```rust let x = function_with_complicated_return_type(); let size = size_of::<ComplicatedReturnType<Foo, Bar>>(); ```
2 parents 4ff701b + 5c2e9b2 commit f2b0ef1

File tree

1 file changed

+57
-1
lines changed

1 file changed

+57
-1
lines changed

src/libcore/sys.rs

+57-1
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,24 @@ pub fn get_type_desc<T>() -> *TypeDesc {
8383
unsafe { rusti::get_tydesc::<T>() as *TypeDesc }
8484
}
8585

86+
/// Returns a pointer to a type descriptor.
87+
#[inline(always)]
88+
pub fn get_type_desc_val<T>(_val: &T) -> *TypeDesc {
89+
get_type_desc::<T>()
90+
}
91+
8692
/// Returns the size of a type
8793
#[inline(always)]
8894
pub fn size_of<T>() -> uint {
8995
unsafe { rusti::size_of::<T>() }
9096
}
9197

98+
/// Returns the size of the type that `_val` points to
99+
#[inline(always)]
100+
pub fn size_of_val<T>(_val: &T) -> uint {
101+
size_of::<T>()
102+
}
103+
92104
/**
93105
* Returns the size of a type, or 1 if the actual size is zero.
94106
*
@@ -100,6 +112,13 @@ pub fn nonzero_size_of<T>() -> uint {
100112
if s == 0 { 1 } else { s }
101113
}
102114

115+
/// Returns the size of the type of the value that `_val` points to
116+
#[inline(always)]
117+
pub fn nonzero_size_of_val<T>(_val: &T) -> uint {
118+
nonzero_size_of::<T>()
119+
}
120+
121+
103122
/**
104123
* Returns the ABI-required minimum alignment of a type
105124
*
@@ -111,12 +130,26 @@ pub fn min_align_of<T>() -> uint {
111130
unsafe { rusti::min_align_of::<T>() }
112131
}
113132

133+
/// Returns the ABI-required minimum alignment of the type of the value that
134+
/// `_val` points to
135+
#[inline(always)]
136+
pub fn min_align_of_val<T>(_val: &T) -> uint {
137+
min_align_of::<T>()
138+
}
139+
114140
/// Returns the preferred alignment of a type
115141
#[inline(always)]
116142
pub fn pref_align_of<T>() -> uint {
117143
unsafe { rusti::pref_align_of::<T>() }
118144
}
119145

146+
/// Returns the preferred alignment of the type of the value that
147+
/// `_val` points to
148+
#[inline(always)]
149+
pub fn pref_align_of_val<T>(_val: &T) -> uint {
150+
pref_align_of::<T>()
151+
}
152+
120153
/// Returns the refcount of a shared box (as just before calling this)
121154
#[inline(always)]
122155
pub fn refcount<T>(t: @T) -> uint {
@@ -162,7 +195,7 @@ pub fn fail_assert(msg: &str, file: &str, line: uint) -> ! {
162195
#[cfg(test)]
163196
mod tests {
164197
use cast;
165-
use sys::{Closure, pref_align_of, size_of, nonzero_size_of};
198+
use sys::*;
166199
167200
#[test]
168201
fn size_of_basic() {
@@ -188,6 +221,14 @@ mod tests {
188221
assert!(size_of::<*uint>() == 8u);
189222
}
190223

224+
#[test]
225+
fn size_of_val_basic() {
226+
assert_eq!(size_of_val(&1u8), 1);
227+
assert_eq!(size_of_val(&1u16), 2);
228+
assert_eq!(size_of_val(&1u32), 4);
229+
assert_eq!(size_of_val(&1u64), 8);
230+
}
231+
191232
#[test]
192233
fn nonzero_size_of_basic() {
193234
type Z = [i8, ..0];
@@ -196,6 +237,14 @@ mod tests {
196237
assert!(nonzero_size_of::<uint>() == size_of::<uint>());
197238
}
198239

240+
#[test]
241+
fn nonzero_size_of_val_basic() {
242+
let z = [0u8, ..0];
243+
assert_eq!(size_of_val(&z), 0u);
244+
assert_eq!(nonzero_size_of_val(&z), 1u);
245+
assert_eq!(nonzero_size_of_val(&1u), size_of_val(&1u));
246+
}
247+
199248
#[test]
200249
fn align_of_basic() {
201250
assert!(pref_align_of::<u8>() == 1u);
@@ -219,6 +268,13 @@ mod tests {
219268
assert!(pref_align_of::<*uint>() == 8u);
220269
}
221270

271+
#[test]
272+
fn align_of_val_basic() {
273+
assert_eq!(pref_align_of_val(&1u8), 1u);
274+
assert_eq!(pref_align_of_val(&1u16), 2u);
275+
assert_eq!(pref_align_of_val(&1u32), 4u);
276+
}
277+
222278
#[test]
223279
fn synthesize_closure() {
224280
unsafe {

0 commit comments

Comments
 (0)