1 // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! Misc low level stuff
18 use libc::{c_void, c_char, size_t};
22 pub type FreeGlue<'self> = &'self fn(*TypeDesc, *c_void);
24 // Corresponds to runtime type_desc type
31 // Remaining fields not listed
34 /// The representation of a Rust closure
41 #[abi = "rust-intrinsic"]
42 pub extern "rust-intrinsic" {
43 fn get_tydesc<T>() -> *();
44 fn size_of<T>() -> uint;
45 fn pref_align_of<T>() -> uint;
46 fn min_align_of<T>() -> uint;
51 use libc::{c_char, size_t};
55 unsafe fn rust_upcall_fail(expr: *c_char,
61 /// Compares contents of two pointers using the default method.
62 /// Equivalent to `*x1 == *x2`. Useful for hashtables.
63 pub fn shape_eq<T:Eq>(x1: &T, x2: &T) -> bool {
67 pub fn shape_lt<T:Ord>(x1: &T, x2: &T) -> bool {
71 pub fn shape_le<T:Ord>(x1: &T, x2: &T) -> bool {
76 * Returns a pointer to a type descriptor.
78 * Useful for calling certain function in the Rust runtime or otherwise
79 * performing dark magick.
82 pub fn get_type_desc<T>() -> *TypeDesc {
83 unsafe { rusti::get_tydesc::<T>() as *TypeDesc }
86 /// Returns a pointer to a type descriptor.
88 pub fn get_type_desc_val<T>(_val: &T) -> *TypeDesc {
92 /// Returns the size of a type
94 pub fn size_of<T>() -> uint {
95 unsafe { rusti::size_of::<T>() }
98 /// Returns the size of the type that `_val` points to
100 pub fn size_of_val<T>(_val: &T) -> uint {
105 * Returns the size of a type, or 1 if the actual size is zero.
107 * Useful for building structures containing variable-length arrays.
110 pub fn nonzero_size_of<T>() -> uint {
111 let s = size_of::<T>();
112 if s == 0 { 1 } else { s }
115 /// Returns the size of the type of the value that `_val` points to
117 pub fn nonzero_size_of_val<T>(_val: &T) -> uint {
118 nonzero_size_of::<T>()
123 * Returns the ABI-required minimum alignment of a type
125 * This is the alignment used for struct fields. It may be smaller
126 * than the preferred alignment.
129 pub fn min_align_of<T>() -> uint {
130 unsafe { rusti::min_align_of::<T>() }
133 /// Returns the ABI-required minimum alignment of the type of the value that
136 pub fn min_align_of_val<T>(_val: &T) -> uint {
140 /// Returns the preferred alignment of a type
142 pub fn pref_align_of<T>() -> uint {
143 unsafe { rusti::pref_align_of::<T>() }
146 /// Returns the preferred alignment of the type of the value that
149 pub fn pref_align_of_val<T>(_val: &T) -> uint {
153 /// Returns the refcount of a shared box (as just before calling this)
155 pub fn refcount<T>(t: @T) -> uint {
157 let ref_ptr: *uint = cast::transmute_copy(&t);
162 pub fn log_str<T>(t: &T) -> ~str {
163 do io::with_str_writer |wr| {
164 repr::write_repr(wr, t)
168 /// Trait for initiating task failure.
169 pub trait FailWithCause {
170 /// Fail the current task, taking ownership of `cause`
171 fn fail_with(cause: Self, file: &'static str, line: uint) -> !;
174 impl FailWithCause for ~str {
175 fn fail_with(cause: ~str, file: &'static str, line: uint) -> ! {
176 do str::as_buf(cause) |msg_buf, _msg_len| {
177 do str::as_buf(file) |file_buf, _file_len| {
179 let msg_buf = cast::transmute(msg_buf);
180 let file_buf = cast::transmute(file_buf);
181 begin_unwind_(msg_buf, file_buf, line as libc::size_t)
188 impl FailWithCause for &'static str {
189 fn fail_with(cause: &'static str, file: &'static str, line: uint) -> ! {
190 do str::as_buf(cause) |msg_buf, _msg_len| {
191 do str::as_buf(file) |file_buf, _file_len| {
193 let msg_buf = cast::transmute(msg_buf);
194 let file_buf = cast::transmute(file_buf);
195 begin_unwind_(msg_buf, file_buf, line as libc::size_t)
202 // NOTE: remove function after snapshot
204 pub fn begin_unwind(msg: ~str, file: ~str, line: uint) -> ! {
205 do str::as_buf(msg) |msg_buf, _msg_len| {
206 do str::as_buf(file) |file_buf, _file_len| {
208 let msg_buf = cast::transmute(msg_buf);
209 let file_buf = cast::transmute(file_buf);
210 begin_unwind_(msg_buf, file_buf, line as libc::size_t)
216 // FIXME #4427: Temporary until rt::rt_fail_ goes away
217 pub fn begin_unwind_(msg: *c_char, file: *c_char, line: size_t) -> ! {
219 gc::cleanup_stack_for_failure();
220 rustrt::rust_upcall_fail(msg, file, line);
225 // NOTE: remove function after snapshot
227 pub fn fail_assert(msg: &str, file: &str, line: uint) -> ! {
228 let (msg, file) = (msg.to_owned(), file.to_owned());
229 begin_unwind(~"assertion failed: " + msg, file, line)
239 assert!(size_of::<u8>() == 1u);
240 assert!(size_of::<u16>() == 2u);
241 assert!(size_of::<u32>() == 4u);
242 assert!(size_of::<u64>() == 8u);
246 #[cfg(target_arch = "x86")]
247 #[cfg(target_arch = "arm")]
248 #[cfg(target_arch = "mips")]
250 assert!(size_of::<uint>() == 4u);
251 assert!(size_of::<*uint>() == 4u);
255 #[cfg(target_arch = "x86_64")]
257 assert!(size_of::<uint>() == 8u);
258 assert!(size_of::<*uint>() == 8u);
262 fn size_of_val_basic() {
263 assert_eq!(size_of_val(&1u8), 1);
264 assert_eq!(size_of_val(&1u16), 2);
265 assert_eq!(size_of_val(&1u32), 4);
266 assert_eq!(size_of_val(&1u64), 8);
270 fn nonzero_size_of_basic() {
272 assert!(size_of::<Z>() == 0u);
273 assert!(nonzero_size_of::<Z>() == 1u);
274 assert!(nonzero_size_of::<uint>() == size_of::<uint>());
278 fn nonzero_size_of_val_basic() {
280 assert_eq!(size_of_val(&z), 0u);
281 assert_eq!(nonzero_size_of_val(&z), 1u);
282 assert_eq!(nonzero_size_of_val(&1u), size_of_val(&1u));
286 fn align_of_basic() {
287 assert!(pref_align_of::<u8>() == 1u);
288 assert!(pref_align_of::<u16>() == 2u);
289 assert!(pref_align_of::<u32>() == 4u);
293 #[cfg(target_arch = "x86")]
294 #[cfg(target_arch = "arm")]
295 #[cfg(target_arch = "mips")]
297 assert!(pref_align_of::<uint>() == 4u);
298 assert!(pref_align_of::<*uint>() == 4u);
302 #[cfg(target_arch = "x86_64")]
304 assert!(pref_align_of::<uint>() == 8u);
305 assert!(pref_align_of::<*uint>() == 8u);
309 fn align_of_val_basic() {
310 assert_eq!(pref_align_of_val(&1u8), 1u);
311 assert_eq!(pref_align_of_val(&1u16), 2u);
312 assert_eq!(pref_align_of_val(&1u32), 4u);
316 fn synthesize_closure() {
319 let f: &fn(int) -> int = |y| x + y;
321 assert!(f(20) == 30);
323 let original_closure: Closure = cast::transmute(f);
325 let actual_function_pointer = original_closure.code;
326 let environment = original_closure.env;
328 let new_closure = Closure {
329 code: actual_function_pointer,
333 let new_f: &fn(int) -> int = cast::transmute(new_closure);
334 assert!(new_f(20) == 30);
340 fn fail_static() { FailWithCause::fail_with("cause", file!(), line!()) }
344 fn fail_owned() { FailWithCause::fail_with(~"cause", file!(), line!()) }
350 // indent-tabs-mode: nil
352 // buffer-file-coding-system: utf-8-unix