(index<- ) ./libstd/raw.rs
git branch: * master c7553ea auto merge of #13609 : richo/rust/str-type-vim, r=alexcrichton
modified: Wed Apr 9 17:27:02 2014
1 // Copyright 2013 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.
4 //
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.
10
11 #![allow(missing_doc)]
12
13 //! Contains struct definitions for the layout of compiler built-in types.
14 //!
15 //! They can be used as targets of transmutes in unsafe code for manipulating
16 //! the raw representations directly.
17 //!
18 //! Their definitition should always match the ABI defined in `rustc::back::abi`.
19
20 use cast;
21
22 /// The representation of a Rust managed box
23 pub struct Box<T> {
24 pub ref_count: uint,
25 pub drop_glue: fn(ptr: *mut u8),
26 pub prev: *mut Box<T>,
27 pub next: *mut Box<T>,
28 pub data: T,
29 }
30
31 /// The representation of a Rust vector
32 pub struct Vec<T> {
33 pub fill: uint,
34 pub alloc: uint,
35 pub data: T,
36 }
37
38 /// The representation of a Rust string
39 pub type String = Vec<u8>;
40
41 /// The representation of a Rust slice
42 pub struct Slice<T> {
43 pub data: *T,
44 pub len: uint,
45 }
46
47 /// The representation of a Rust closure
48 pub struct Closure {
49 pub code: *(),
50 pub env: *(),
51 }
52
53 /// The representation of a Rust procedure (`proc()`)
54 pub struct Procedure {
55 pub code: *(),
56 pub env: *(),
57 }
58
59 /// The representation of a Rust trait object.
60 ///
61 /// This struct does not have a `Repr` implementation
62 /// because there is no way to refer to all trait objects generically.
63 pub struct TraitObject {
64 pub vtable: *(),
65 pub data: *(),
66 }
67
68 /// This trait is meant to map equivalences between raw structs and their
69 /// corresponding rust values.
70 pub trait Repr<T> {
71 /// This function "unwraps" a rust value (without consuming it) into its raw
72 /// struct representation. This can be used to read/write different values
73 /// for the struct. This is a safe method because by default it does not
74 /// enable write-access to the fields of the return value in safe code.
75 #[inline]
76 fn repr(&self) -> T { unsafe { cast::transmute_copy(self) } }
77 }
78
79 impl<'a, T> Repr<Slice<T>> for &'a [T] {}
80 impl<'a> Repr<Slice<u8>> for &'a str {}
81 impl<T> Repr<*Box<T>> for @T {}
82 impl<T> Repr<*Vec<T>> for ~[T] {}
83 impl Repr<*String> for ~str {}
84
85 #[cfg(test)]
86 mod tests {
87 use super::*;
88
89 use cast;
90
91 #[test]
92 fn synthesize_closure() {
93 unsafe {
94 let x = 10;
95 let f: |int| -> int = |y| x + y;
96
97 assert_eq!(f(20), 30);
98
99 let original_closure: Closure = cast::transmute(f);
100
101 let actual_function_pointer = original_closure.code;
102 let environment = original_closure.env;
103
104 let new_closure = Closure {
105 code: actual_function_pointer,
106 env: environment
107 };
108
109 let new_f: |int| -> int = cast::transmute(new_closure);
110 assert_eq!(new_f(20), 30);
111 }
112 }
113 }
libstd/raw.rs:41:39-41:39 -struct- definition:
/// The representation of a Rust slice
pub struct Slice<T> {
pub data: *T,
references:- 2379: impl<'a, T> Repr<Slice<T>> for &'a [T] {}
80: impl<'a> Repr<Slice<u8>> for &'a str {}
81: impl<T> Repr<*Box<T>> for @T {}
libstd/slice.rs:
877: unsafe {
878: transmute(Slice {
879: data: self.as_ptr().offset(start as int),
--
1021: unsafe {
1022: let s: &mut Slice<T> = transmute(self);
1023: Some(&*raw::shift_ptr(s))
--
1647: unsafe {
1648: transmute(Slice {
1649: data: self.as_mut_ptr().offset(start as int) as *T,
--
1718: unsafe {
1719: let s: &mut Slice<T> = transmute(self);
1720: Some(cast::transmute_mut(&*raw::shift_ptr(s)))
--
1879: -> U {
1880: f(transmute(Slice {
1881: data: p,
--
1937: */
1938: pub unsafe fn pop_ptr<T>(slice: &mut Slice<T>) -> *T {
1939: if slice.len == 0 { fail!("pop on empty slice"); }
libstd/vec.rs:
1367: fn as_slice<'a>(&'a self) -> &'a [T] {
1368: unsafe { transmute(Slice { data: self.as_ptr(), len: self.len }) }
1369: }
libstd/str.rs:
1402: }
1403: let v = Slice { data: s, len: len };
1404: assert!(is_utf8(::cast::transmute(v)));
--
1429: pub unsafe fn slice_unchecked<'a>(s: &'a str, begin: uint, end: uint) -> &'a str {
1430: cast::transmute(Slice {
1431: data: s.as_ptr().offset(begin as int),
libstd/c_str.rs:
198: unsafe {
199: cast::transmute(Slice { data: self.buf, len: self.len() })
200: }
libstd/c_vec.rs:
111: unsafe {
112: cast::transmute(raw::Slice { data: self.base as *T, len: self.len })
113: }
libstd/repr.rs:
360: fn visit_evec_slice(&mut self, mtbl: uint, inner: *TyDesc) -> bool {
361: self.get::<raw::Slice<()>>(|this, s| {
362: try!(this, this.writer.write(['&' as u8]));
libstd/slice.rs:
1896: -> U {
1897: f(transmute(Slice {
1898: data: p as *T,
libstd/raw.rs:62:71-62:71 -struct- definition:
/// because there is no way to refer to all trait objects generically.
pub struct TraitObject {
pub vtable: *(),
references:- 3libstd/any.rs:
84: // Get the raw representation of the trait object
85: let to: TraitObject = transmute_copy(&self);
--
108: // Get the raw representation of the trait object
109: let to: TraitObject = transmute_copy(&self);
--
132: // Get the raw representation of the trait object
133: let to: TraitObject = transmute_copy(&self);
libstd/raw.rs:31:40-31:40 -struct- definition:
/// The representation of a Rust vector
pub struct Vec<T> {
pub fill: uint,
references:- 738: /// The representation of a Rust string
39: pub type String = Vec<u8>;
--
81: impl<T> Repr<*Box<T>> for @T {}
82: impl<T> Repr<*Vec<T>> for ~[T] {}
83: impl Repr<*String> for ~str {}
libstd/slice.rs:
2293: let data_size = data_size.expect("overflow in from_iter()");
2294: let size = mem::size_of::<RawVec<()>>().checked_add(&data_size);
2295: let size = size.expect("overflow in from_iter()");
--
2301: unsafe {
2302: let ret = malloc_raw(size) as *mut RawVec<()>;
libstd/repr.rs:
226: pub fn write_unboxed_vec_repr(&mut self, _: uint, v: &raw::Vec<()>, inner: *TyDesc) -> bool {
227: self.write_vec_range(&v.data, v.fill, inner)
--
353: fn visit_evec_uniq(&mut self, mtbl: uint, inner: *TyDesc) -> bool {
354: self.get::<&raw::Vec<()>>(|this, b| {
355: try!(this, this.writer.write(['~' as u8]));
libstd/raw.rs:22:45-22:45 -struct- definition:
/// The representation of a Rust managed box
pub struct Box<T> {
pub ref_count: uint,
references:- 1026: pub prev: *mut Box<T>,
27: pub next: *mut Box<T>,
28: pub data: T,
libstd/rt/global_heap.rs:
97: let alloc = p as *mut raw::Box<()>;
98: (*alloc).drop_glue = drop_glue;
libstd/rt/local_heap.rs:
43: live_allocs: *mut raw::Box<()>,
44: }
libstd/cleanup.rs:
24: unsafe fn each_live_alloc(read_next_before: bool,
25: f: |alloc: *mut raw::Box<()>| -> bool)
26: -> bool {
libstd/repr.rs:
345: fn visit_evec_box(&mut self, mtbl: uint, inner: *TyDesc) -> bool {
346: self.get::<&raw::Box<raw::Vec<()>>>(|this, b| {
347: try!(this, this.writer.write(['@' as u8]));
libstd/raw.rs:
80: impl<'a> Repr<Slice<u8>> for &'a str {}
81: impl<T> Repr<*Box<T>> for @T {}
82: impl<T> Repr<*Vec<T>> for ~[T] {}
libstd/raw.rs:69:31-69:31 -trait- definition:
/// corresponding rust values.
pub trait Repr<T> {
/// This function "unwraps" a rust value (without consuming it) into its raw
references:- 580: impl<'a> Repr<Slice<u8>> for &'a str {}
81: impl<T> Repr<*Box<T>> for @T {}
82: impl<T> Repr<*Vec<T>> for ~[T] {}
83: impl Repr<*String> for ~str {}
libstd/raw.rs:47:41-47:41 -struct- definition:
/// The representation of a Rust closure
pub struct Closure {
pub code: *(),
references:- 2libstd/rt/unwind.rs:
109: unsafe {
110: let closure: || = cast::transmute(Closure {
111: code: code as *(),