|
34 | 34 | /// only designed to be used by unsafe code that needs to manipulate
|
35 | 35 | /// the low-level details.
|
36 | 36 | ///
|
37 |
| -/// There is no `Repr` implementation for `TraitObject` because there |
38 |
| -/// is no way to refer to all trait objects generically, so the only |
| 37 | +/// There is no way to refer to all trait objects generically, so the only |
39 | 38 | /// way to create values of this type is with functions like
|
40 |
| -/// `std::mem::transmute`. Similarly, the only way to create a true |
| 39 | +/// [`std::mem::transmute`][transmute]. Similarly, the only way to create a true |
41 | 40 | /// trait object from a `TraitObject` value is with `transmute`.
|
42 | 41 | ///
|
| 42 | +/// [transmute]: ../mem/fn.transmute.html |
| 43 | +/// |
43 | 44 | /// Synthesizing a trait object with mismatched types—one where the
|
44 | 45 | /// vtable does not correspond to the type of the value to which the
|
45 | 46 | /// data pointer points—is highly likely to lead to undefined
|
|
50 | 51 | /// ```
|
51 | 52 | /// #![feature(raw)]
|
52 | 53 | ///
|
53 |
| -/// use std::mem; |
54 |
| -/// use std::raw; |
| 54 | +/// use std::{mem, raw}; |
55 | 55 | ///
|
56 | 56 | /// // an example trait
|
57 | 57 | /// trait Foo {
|
58 | 58 | /// fn bar(&self) -> i32;
|
59 | 59 | /// }
|
| 60 | +/// |
60 | 61 | /// impl Foo for i32 {
|
61 | 62 | /// fn bar(&self) -> i32 {
|
62 | 63 | /// *self + 1
|
|
74 | 75 | /// // the data pointer is the address of `value`
|
75 | 76 | /// assert_eq!(raw_object.data as *const i32, &value as *const _);
|
76 | 77 | ///
|
77 |
| -/// |
78 | 78 | /// let other_value: i32 = 456;
|
79 | 79 | ///
|
80 | 80 | /// // construct a new object, pointing to a different `i32`, being
|
81 | 81 | /// // careful to use the `i32` vtable from `object`
|
82 | 82 | /// let synthesized: &Foo = unsafe {
|
83 | 83 | /// mem::transmute(raw::TraitObject {
|
84 | 84 | /// data: &other_value as *const _ as *mut (),
|
85 |
| -/// vtable: raw_object.vtable |
| 85 | +/// vtable: raw_object.vtable, |
86 | 86 | /// })
|
87 | 87 | /// };
|
88 | 88 | ///
|
89 |
| -/// // it should work just like we constructed a trait object out of |
| 89 | +/// // it should work just as if we had constructed a trait object out of |
90 | 90 | /// // `other_value` directly
|
91 | 91 | /// assert_eq!(synthesized.bar(), 457);
|
92 | 92 | /// ```
|
|
0 commit comments