1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
//! Unsafe versions of standard library `From<T>` and `Into<T>`. /// Unsafe version of `Into<T>` trait from `std`. /// /// Prefer implementing `UnsafeFrom` because it gives you `UnsafeInto` freely. /// /// # Example /// ``` /// use unsafe_from::UnsafeInto; /// /// struct MyUnsafeType(i32); /// /// unsafe impl UnsafeInto<MyUnsafeType> for i32 { /// unsafe fn unsafe_into(self) -> MyUnsafeType { /// MyUnsafeType(self) /// } /// } /// ``` pub unsafe trait UnsafeInto<T>: Sized { unsafe fn unsafe_into(self) -> T; } unsafe impl<T, U> UnsafeInto<U> for T where U: UnsafeFrom<T>, { unsafe fn unsafe_into(self) -> U { U::unsafe_from(self) } } /// Unsafe version of `From<T>` trait from `std`. /// /// Implementing this trait also gives you `UnsafeInto` freely. /// /// # Example /// ``` /// use unsafe_from::UnsafeFrom; /// /// struct MyUnsafeType(i32); /// /// unsafe impl UnsafeFrom<i32> for MyUnsafeType { /// unsafe fn unsafe_from(t: i32) -> MyUnsafeType { /// MyUnsafeType(t) /// } /// } /// ``` pub unsafe trait UnsafeFrom<T>: Sized { unsafe fn unsafe_from(t: T) -> Self; } unsafe impl<T> UnsafeFrom<T> for T { unsafe fn unsafe_from(t: T) -> Self { t } }