(index<- ) ./libcore/raw.rs
git branch: * master 5200215 auto merge of #14035 : alexcrichton/rust/experimental, r=huonw
modified: Fri May 9 13:02:28 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 #![experimental]
13
14 //! Contains struct definitions for the layout of compiler built-in types.
15 //!
16 //! They can be used as targets of transmutes in unsafe code for manipulating
17 //! the raw representations directly.
18 //!
19 //! Their definition should always match the ABI defined in `rustc::back::abi`.
20
21 use cast;
22
23 /// The representation of a Rust managed box
24 pub struct Box<T> {
25 pub ref_count: uint,
26 pub drop_glue: fn(ptr: *mut u8),
27 pub prev: *mut Box<T>,
28 pub next: *mut Box<T>,
29 pub data: T,
30 }
31
32 /// The representation of a Rust vector
33 pub struct Vec<T> {
34 pub fill: uint,
35 pub alloc: uint,
36 pub data: T,
37 }
38
39 /// The representation of a Rust string
40 pub type String = Vec<u8>;
41
42 /// The representation of a Rust slice
43 pub struct Slice<T> {
44 pub data: *T,
45 pub len: uint,
46 }
47
48 /// The representation of a Rust closure
49 pub struct Closure {
50 pub code: *(),
51 pub env: *(),
52 }
53
54 /// The representation of a Rust procedure (`proc()`)
55 pub struct Procedure {
56 pub code: *(),
57 pub env: *(),
58 }
59
60 /// The representation of a Rust trait object.
61 ///
62 /// This struct does not have a `Repr` implementation
63 /// because there is no way to refer to all trait objects generically.
64 pub struct TraitObject {
65 pub vtable: *(),
66 pub data: *(),
67 }
68
69 /// This trait is meant to map equivalences between raw structs and their
70 /// corresponding rust values.
71 pub trait Repr<T> {
72 /// This function "unwraps" a rust value (without consuming it) into its raw
73 /// struct representation. This can be used to read/write different values
74 /// for the struct. This is a safe method because by default it does not
75 /// enable write-access to the fields of the return value in safe code.
76 #[inline]
77 fn repr(&self) -> T { unsafe { cast::transmute_copy(self) } }
78 }
79
80 impl<'a, T> Repr<Slice<T>> for &'a [T] {}
81 impl<'a> Repr<Slice<u8>> for &'a str {}
82 impl<T> Repr<*Box<T>> for @T {}
83 impl<T> Repr<*Vec<T>> for ~[T] {}
84 impl Repr<*String> for ~str {}
85
86 #[cfg(test)]
87 mod tests {
88 use super::*;
89
90 use cast;
91
92 #[test]
93 fn synthesize_closure() {
94 unsafe {
95 let x = 10;
96 let f: |int| -> int = |y| x + y;
97
98 assert_eq!(f(20), 30);
99
100 let original_closure: Closure = cast::transmute(f);
101
102 let actual_function_pointer = original_closure.code;
103 let environment = original_closure.env;
104
105 let new_closure = Closure {
106 code: actual_function_pointer,
107 env: environment
108 };
109
110 let new_f: |int| -> int = cast::transmute(new_closure);
111 assert_eq!(new_f(20), 30);
112 }
113 }
114 }
libcore/raw.rs:70:31-70: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, T> Repr<Slice<T>> for &'a [T] {}
81: impl<'a> Repr<Slice<u8>> for &'a str {}
82: impl<T> Repr<*Box<T>> for @T {}
83: impl<T> Repr<*Vec<T>> for ~[T] {}
84: impl Repr<*String> for ~str {}
libcore/raw.rs:32:40-32:40 -struct- definition:
/// The representation of a Rust vector
pub struct Vec<T> {
pub fill: uint,
references:- 1182: impl<T> Repr<*Box<T>> for @T {}
83: impl<T> Repr<*Vec<T>> for ~[T] {}
84: impl Repr<*String> for ~str {}
libcore/should_not_exist.rs:
154: unsafe {
155: let ret = alloc(size) as *mut Vec<A>;
libcore/raw.rs:63:71-63:71 -struct- definition:
/// because there is no way to refer to all trait objects generically.
pub struct TraitObject {
pub vtable: *(),
references:- 3libcore/any.rs:
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);
libcore/raw.rs:42:39-42:39 -struct- definition:
/// The representation of a Rust slice
pub struct Slice<T> {
pub data: *T,
references:- 16libcore/slice.rs:
1176: -> U {
1177: f(transmute(Slice {
1178: data: p,
--
1193: -> U {
1194: f(transmute(Slice {
1195: data: p as *T,
libcore/str.rs:
737: }
738: let v = Slice { data: s, len: len };
739: assert!(is_utf8(::cast::transmute(v)));
--
764: pub unsafe fn slice_unchecked<'a>(s: &'a str, begin: uint, end: uint) -> &'a str {
765: cast::transmute(Slice {
766: data: s.as_ptr().offset(begin as int),
libcore/raw.rs:
80: impl<'a, T> Repr<Slice<T>> for &'a [T] {}
81: impl<'a> Repr<Slice<u8>> for &'a str {}
82: impl<T> Repr<*Box<T>> for @T {}
libcore/slice.rs:
706: unsafe {
707: let s: &mut Slice<T> = transmute(self);
708: Some(&*raw::pop_ptr(s))
--
1204: */
1205: pub unsafe fn shift_ptr<T>(slice: &mut Slice<T>) -> *T {
1206: if slice.len == 0 { fail!("shift on empty slice"); }
--
1217: */
1218: pub unsafe fn pop_ptr<T>(slice: &mut Slice<T>) -> *T {
1219: if slice.len == 0 { fail!("pop on empty slice"); }
libcore/raw.rs:23:45-23:45 -struct- definition:
/// The representation of a Rust managed box
pub struct Box<T> {
pub ref_count: uint,
references:- 327: pub prev: *mut Box<T>,
28: pub next: *mut Box<T>,
29: pub data: T,
--
81: impl<'a> Repr<Slice<u8>> for &'a str {}
82: impl<T> Repr<*Box<T>> for @T {}
83: impl<T> Repr<*Vec<T>> for ~[T] {}