]> git.lizzy.rs Git - rust.git/blob - src/libcore/raw.rs
prefer "FIXME" to "TODO".
[rust.git] / src / libcore / raw.rs
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_docs)]
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 mem;
22 use kinds::Sized;
23
24 /// The representation of a Rust slice
25 #[repr(C)]
26 pub struct Slice<T> {
27     pub data: *const T,
28     pub len: uint,
29 }
30
31 /// The representation of a Rust closure
32 #[repr(C)]
33 pub struct Closure {
34     pub code: *mut (),
35     pub env: *mut (),
36 }
37
38 /// The representation of a Rust procedure (`proc()`)
39 #[repr(C)]
40 pub struct Procedure {
41     pub code: *mut (),
42     pub env: *mut (),
43 }
44
45 /// The representation of a Rust trait object.
46 ///
47 /// This struct does not have a `Repr` implementation
48 /// because there is no way to refer to all trait objects generically.
49 #[repr(C)]
50 pub struct TraitObject {
51     pub data: *mut (),
52     pub vtable: *mut (),
53 }
54
55 /// This trait is meant to map equivalences between raw structs and their
56 /// corresponding rust values.
57 pub trait Repr<T> for Sized? {
58     /// This function "unwraps" a rust value (without consuming it) into its raw
59     /// struct representation. This can be used to read/write different values
60     /// for the struct. This is a safe method because by default it does not
61     /// enable write-access to the fields of the return value in safe code.
62     #[inline]
63     fn repr(&self) -> T { unsafe { mem::transmute_copy(&self) } }
64 }
65
66 impl<T> Repr<Slice<T>> for [T] {}
67 impl Repr<Slice<u8>> for str {}