|
| 1 | +use { |
| 2 | + ::core::{cell::{Cell, UnsafeCell}, mem::MaybeUninit, pin::Pin}, |
| 3 | + crate::{Destructure, Restructure, StructuralPinning}, |
| 4 | +}; |
| 5 | + |
| 6 | +// *const T |
| 7 | + |
| 8 | +impl<T: ?Sized> Destructure for *const T { |
| 9 | + type Underlying = T; |
| 10 | + |
| 11 | + fn as_mut_ptr(&mut self) -> *mut Self::Underlying { |
| 12 | + *self as *mut T |
| 13 | + } |
| 14 | +} |
| 15 | + |
| 16 | +unsafe impl<T: ?Sized, U: ?Sized> Restructure<&*const T> for U { |
| 17 | + type Restructured = *const Self; |
| 18 | + |
| 19 | + unsafe fn restructure(ptr: *mut Self) -> Self::Restructured { |
| 20 | + ptr as *const Self |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +// *mut T |
| 25 | + |
| 26 | +impl<T: ?Sized> Destructure for *mut T { |
| 27 | + type Underlying = T; |
| 28 | + |
| 29 | + fn as_mut_ptr(&mut self) -> *mut Self::Underlying { |
| 30 | + *self |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +unsafe impl<T: ?Sized, U: ?Sized> Restructure<&*mut T> for U { |
| 35 | + type Restructured = *mut Self; |
| 36 | + |
| 37 | + unsafe fn restructure(ptr: *mut Self) -> Self::Restructured { |
| 38 | + ptr |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +// &MaybeUninit |
| 43 | + |
| 44 | +impl<'a, T> Destructure for &'a MaybeUninit<T> { |
| 45 | + type Underlying = T; |
| 46 | + |
| 47 | + fn as_mut_ptr(&mut self) -> *mut Self::Underlying { |
| 48 | + self.as_ptr() as *mut Self::Underlying |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +unsafe impl<'a: 'b, 'b, T, U: 'b> Restructure<&'b &'a MaybeUninit<T>> for U { |
| 53 | + type Restructured = &'b MaybeUninit<U>; |
| 54 | + |
| 55 | + unsafe fn restructure(ptr: *mut Self) -> Self::Restructured { |
| 56 | + // SAFETY: The caller has guaranteed that `ptr` points to a subfield of some |
| 57 | + // `&'b &'a MaybeUninit<T>`, so it's safe to dereference for the `'b` lifetime. |
| 58 | + unsafe { &*ptr.cast() } |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +// &mut MaybeUninit |
| 63 | + |
| 64 | +impl<'a, T> Destructure for &'a mut MaybeUninit<T> { |
| 65 | + type Underlying = T; |
| 66 | + |
| 67 | + fn as_mut_ptr(&mut self) -> *mut Self::Underlying { |
| 68 | + MaybeUninit::as_mut_ptr(self) |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +unsafe impl<'a: 'b, 'b, T, U: 'b> Restructure<&'b &'a mut MaybeUninit<T>> for U { |
| 73 | + type Restructured = &'b mut MaybeUninit<U>; |
| 74 | + |
| 75 | + unsafe fn restructure(ptr: *mut Self) -> Self::Restructured { |
| 76 | + // SAFETY: The caller has guaranteed that `ptr` points to a subfield of some |
| 77 | + // `&'b &'a mut MaybeUninit<T>`, so it's safe to mutably dereference for the `'b` lifetime. |
| 78 | + unsafe { &mut *ptr.cast() } |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +// &Cell<T> |
| 83 | + |
| 84 | +impl<'a, T> Destructure for &'a Cell<T> { |
| 85 | + type Underlying = T; |
| 86 | + |
| 87 | + fn as_mut_ptr(&mut self) -> *mut Self::Underlying { |
| 88 | + self.as_ptr() |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +unsafe impl<'a: 'b, 'b, T, U: 'b> Restructure<&'b &'a Cell<T>> for U { |
| 93 | + type Restructured = &'b Cell<U>; |
| 94 | + |
| 95 | + unsafe fn restructure(ptr: *mut Self) -> Self::Restructured { |
| 96 | + // SAFETY: The caller has guaranteed that `ptr` points to a subfield of some |
| 97 | + // `&'b &'a Cell<T>`, so it's safe to dereference for the `'b` lifetime. |
| 98 | + unsafe { &*ptr.cast() } |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +// &UnsafeCell<T> |
| 103 | + |
| 104 | +impl<'a, T> Destructure for &'a UnsafeCell<T> { |
| 105 | + type Underlying = T; |
| 106 | + |
| 107 | + fn as_mut_ptr(&mut self) -> *mut Self::Underlying { |
| 108 | + self.get() |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +unsafe impl<'a: 'b, 'b, T, U: 'b> Restructure<&'b &'a UnsafeCell<T>> for U { |
| 113 | + type Restructured = &'b UnsafeCell<U>; |
| 114 | + |
| 115 | + unsafe fn restructure(ptr: *mut Self) -> Self::Restructured { |
| 116 | + // SAFETY: The caller has guaranteed that `ptr` points to a subfield of some |
| 117 | + // `&'b &'a UnsafeCell<T>`, so it's safe to dereference for the `'b` lifetime. |
| 118 | + unsafe { &*ptr.cast() } |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +// Pin<&T> where T: StructuralPinning |
| 123 | + |
| 124 | +impl<'a, T: StructuralPinning> Destructure for Pin<&'a T> { |
| 125 | + type Underlying = T; |
| 126 | + |
| 127 | + fn as_mut_ptr(&mut self) -> *mut Self::Underlying { |
| 128 | + // SAFETY: The value pointed to by `self` will continue to be treated as pinned. |
| 129 | + unsafe { Pin::into_inner_unchecked(self.as_ref()) as *const T as *mut T } |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +unsafe impl<'a: 'b, 'b, T: StructuralPinning, U: 'b> Restructure<&'b Pin<&'a T>> for U { |
| 134 | + type Restructured = Pin<&'b U>; |
| 135 | + |
| 136 | + unsafe fn restructure(ptr: *mut Self) -> Self::Restructured { |
| 137 | + // SAFETY: The caller has guaranteed that `ptr` points to a subfield of some |
| 138 | + // `&'b Pin<&'a T>`, and `T` has structural pinning, so it's safe to derefence as pinned for |
| 139 | + // the `'b` lifetime. |
| 140 | + unsafe { Pin::new_unchecked(&*ptr) } |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +// Pin<&mut T> where T: StructuralPinning |
| 145 | + |
| 146 | +impl<'a, T: StructuralPinning> Destructure for Pin<&'a mut T> { |
| 147 | + type Underlying = T; |
| 148 | + |
| 149 | + fn as_mut_ptr(&mut self) -> *mut Self::Underlying { |
| 150 | + // SAFETY: The value pointed to by `self` will continue to be treated as pinned. |
| 151 | + unsafe { Pin::into_inner_unchecked(self.as_mut()) as *mut T } |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +unsafe impl<'a: 'b, 'b, T: StructuralPinning, U: 'b> Restructure<&'b Pin<&'a mut T>> for U { |
| 156 | + type Restructured = Pin<&'b mut U>; |
| 157 | + |
| 158 | + unsafe fn restructure(ptr: *mut Self) -> Self::Restructured { |
| 159 | + // SAFETY: The caller has guaranteed that `ptr` points to a subfield of some |
| 160 | + // `&'b Pin<&'a mut T>`, and `T` has structural pinning, so it's safe to mutably derefence |
| 161 | + // as pinned for the `'b` lifetime. |
| 162 | + unsafe { Pin::new_unchecked(&mut *ptr) } |
| 163 | + } |
| 164 | +} |
0 commit comments