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