Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.94 KB | None | 0 0
  1. #![feature(marker_trait_attr)]
  2.  
  3. #[marker] trait CanBeUsedWithReinterpretAs {}
  4. impl<T> CanBeUsedWithReinterpretAs for Box<T> {}
  5. impl<T> CanBeUsedWithReinterpretAs for &mut T {}
  6. impl<T> CanBeUsedWithReinterpretAs for &T {}
  7. impl<T> CanBeUsedWithReinterpretAs for Owned<T> {}
  8.  
  9. #[marker] unsafe trait ReinterpretAs<T>
  10. where Self: CanBeUsedWithReinterpretAs, T: CanBeUsedWithReinterpretAs
  11. {}
  12.  
  13. struct Owned<T>(T);
  14.  
  15. unsafe impl<T> ReinterpretAs<Box<T>> for Box<T> {}
  16.  
  17. unsafe impl<'a, T, U> ReinterpretAs<&'a mut T> for &'a mut U
  18. where Box<U>: ReinterpretAs<Box<T>>
  19. {}
  20.  
  21. unsafe impl<'a, T, U> ReinterpretAs<&'a T> for &'a U
  22. where &'a mut U: ReinterpretAs<&'a mut T>
  23. {}
  24.  
  25. unsafe impl<T, U> ReinterpretAs<Owned<T>> for Owned<U>
  26. where for<'a> &'a U: ReinterpretAs<&'a T>
  27. {}
  28.  
  29. unsafe impl<'a, T, U> ReinterpretAs<Owned<&'a T>> for Owned<&'a U>
  30. where &'a U: ReinterpretAs<&'a T>
  31. {}
  32.  
  33. unsafe impl<'a, T, U> ReinterpretAs<Owned<&'a mut T>> for Owned<&'a mut U>
  34. where &'a mut U: ReinterpretAs<&'a mut T>
  35. {}
  36.  
  37. unsafe impl<T, U> ReinterpretAs<Owned<Box<T>>> for Owned<Box<U>>
  38. where Box<U>: ReinterpretAs<Box<T>>
  39. {}
  40.  
  41.  
  42.  
  43. unsafe impl<T> ReinterpretAs<Box<T>> for Box<[T; 1]> {}
  44. unsafe impl<T> ReinterpretAs<Box<[T; 1]>> for Box<T> {}
  45.  
  46. use std::mem::{align_of, forget, size_of};
  47. use std::ptr;
  48. fn reinterpret<T, U>(x: T) -> U
  49. where Owned<T>: ReinterpretAs<Owned<U>>
  50. {
  51. assert!(size_of::<U>() <= size_of::<T>(),
  52. "the `ReinterpretAs` implementation that let you call this is unsound");
  53.  
  54. let p = &x as *const T as *const U;
  55. forget(x);
  56. if align_of::<U>() >= align_of::<T>() {
  57. unsafe { ptr::read(p) }
  58. } else {
  59. unsafe { ptr::read_unaligned(p) }
  60. }
  61. }
  62.  
  63. macro_rules! reinterpret_primitives_as_bytes {
  64. ($($t:ty,)+) => ($(
  65. unsafe impl<'a> ReinterpretAs<&'a mut [u8; size_of::<$t>()]> for &'a mut $t {}
  66. unsafe impl ReinterpretAs<Owned<$t>> for Owned<[u8; size_of::<$t>()]> {}
  67. )+)
  68. }
  69. reinterpret_primitives_as_bytes! {
  70. u8, u16, u32, u64, u128, usize,
  71. i8, i16, i32, i64, i128, isize,
  72. }
  73.  
  74. macro_rules! reinterpret_primitives_change_sign {
  75. ($([$s:ty,$u:ty])+) => ($(
  76. unsafe impl ReinterpretAs<Box<$s>> for Box<$u> {}
  77. unsafe impl ReinterpretAs<Box<$u>> for Box<$s> {}
  78. )+)
  79. }
  80. reinterpret_primitives_change_sign! {
  81. [u8, i8]
  82. [u16, i16]
  83. [u32, i32]
  84. [u64, i64]
  85. [u128, i128]
  86. [usize, isize]
  87. }
  88.  
  89. pub fn foo1(x: [u8; 16]) -> [u32; 4] { unsafe { std::mem::transmute(x) } }
  90. pub fn foo2(x: u32) -> [u8; 4] { unsafe { std::mem::transmute(x) } }
  91. pub fn foo3(x: u32) -> [u8; 4] { reinterpret::<u32, [u8; 4]>(x) }
  92.  
  93. pub fn foo4(x: &u128) -> &i128 { unsafe { std::mem::transmute(x) } }
  94. pub fn foo5(x: &u128) -> &i128 { reinterpret::<&u128, &i128>(x) }
  95.  
  96. pub fn foo6(x: Box<u128>) -> Box<i128> { reinterpret::<Box<u128>, Box<i128>>(x) }
  97.  
  98. unsafe impl ReinterpretAs<Owned<i32>> for Owned<u128> {}
  99. pub fn foo7(x: u128) -> i32 { reinterpret::<u128, i32>(x) }
  100.  
  101.  
  102. //fn main() {
  103. // let _: u16 = reinterpret::<u8, u16>(0_u8);
  104. //}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement