| 1 | #![cfg_attr(test, deny(warnings))] |
| 2 | #![deny(missing_docs)] |
| 3 | #![allow(dyn_drop)] |
| 4 | |
| 5 | //! # traitobject |
| 6 | //! |
| 7 | //! Unsafe helpers for working with raw TraitObjects. |
| 8 | |
| 9 | /// Get the data pointer from this trait object. |
| 10 | /// |
| 11 | /// Highly unsafe, as there is no information about the type of the data. |
| 12 | pub unsafe fn data<T: ?Sized>(val: *const T) -> *const () { |
| 13 | val as *const () |
| 14 | } |
| 15 | |
| 16 | /// Get the data pointer from this trait object, mutably. |
| 17 | /// |
| 18 | /// Highly unsafe, as there is no information about the type of the data. |
| 19 | pub unsafe fn data_mut<T: ?Sized>(val: *mut T) -> *mut () { |
| 20 | val as *mut () |
| 21 | } |
| 22 | |
| 23 | #[test] |
| 24 | fn test_simple() { |
| 25 | let x = &7 as &dyn Send; |
| 26 | unsafe { assert!(&7 == std::mem::transmute::<_, &i32>(data(x))) }; |
| 27 | } |
| 28 | |
| 29 | #[test] |
| 30 | fn test_mut() { |
| 31 | let x = &mut 7 as &mut dyn Send; |
| 32 | unsafe { assert!(&mut 7 == std::mem::transmute::<_, &i32>(data_mut(x))) }; |
| 33 | } |
| 34 | |
| 35 | /// A trait implemented for all trait objects. |
| 36 | /// |
| 37 | /// Implementations for all traits in std are provided. |
| 38 | pub unsafe trait Trait {} |
| 39 | |
| 40 | mod impls; |
| 41 |