]> git.lizzy.rs Git - rust.git/blob - src/libcore/raw.rs
Various minor/cosmetic improvements to code
[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 #![unstable(feature = "raw", issue = "27751")]
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 /// The representation of a trait object like `&SomeTrait`.
22 ///
23 /// This struct has the same layout as types like `&SomeTrait` and
24 /// `Box<dyn AnotherTrait>`.
25 ///
26 /// `TraitObject` is guaranteed to match layouts, but it is not the
27 /// type of trait objects (e.g., the fields are not directly accessible
28 /// on a `&SomeTrait`) nor does it control that layout (changing the
29 /// definition will not change the layout of a `&SomeTrait`). It is
30 /// only designed to be used by unsafe code that needs to manipulate
31 /// the low-level details.
32 ///
33 /// There is no way to refer to all trait objects generically, so the only
34 /// way to create values of this type is with functions like
35 /// [`std::mem::transmute`][transmute]. Similarly, the only way to create a true
36 /// trait object from a `TraitObject` value is with `transmute`.
37 ///
38 /// [transmute]: ../intrinsics/fn.transmute.html
39 ///
40 /// Synthesizing a trait object with mismatched types—one where the
41 /// vtable does not correspond to the type of the value to which the
42 /// data pointer points—is highly likely to lead to undefined
43 /// behavior.
44 ///
45 /// # Examples
46 ///
47 /// ```
48 /// #![feature(raw)]
49 ///
50 /// use std::{mem, raw};
51 ///
52 /// // an example trait
53 /// trait Foo {
54 ///     fn bar(&self) -> i32;
55 /// }
56 ///
57 /// impl Foo for i32 {
58 ///     fn bar(&self) -> i32 {
59 ///          *self + 1
60 ///     }
61 /// }
62 ///
63 /// let value: i32 = 123;
64 ///
65 /// // let the compiler make a trait object
66 /// let object: &Foo = &value;
67 ///
68 /// // look at the raw representation
69 /// let raw_object: raw::TraitObject = unsafe { mem::transmute(object) };
70 ///
71 /// // the data pointer is the address of `value`
72 /// assert_eq!(raw_object.data as *const i32, &value as *const _);
73 ///
74 /// let other_value: i32 = 456;
75 ///
76 /// // construct a new object, pointing to a different `i32`, being
77 /// // careful to use the `i32` vtable from `object`
78 /// let synthesized: &Foo = unsafe {
79 ///      mem::transmute(raw::TraitObject {
80 ///          data: &other_value as *const _ as *mut (),
81 ///          vtable: raw_object.vtable,
82 ///      })
83 /// };
84 ///
85 /// // it should work just as if we had constructed a trait object out of
86 /// // `other_value` directly
87 /// assert_eq!(synthesized.bar(), 457);
88 /// ```
89 #[repr(C)]
90 #[derive(Copy, Clone)]
91 #[allow(missing_debug_implementations)]
92 pub struct TraitObject {
93     pub data: *mut (),
94     pub vtable: *mut (),
95 }