| 1 |
use core::marker::PhantomData; |
| 2 |
|
| 3 |
/// a mutable "slice" (bundle of pointer and length). The main goal of this type is passing MIRI |
| 4 |
/// with stacked borrows. In particular, storing a standard slice in data structures violates the |
| 5 |
/// stacked borrows rule when that slice is deallocated. By only materializing the slice when |
| 6 |
/// needed for data access, hence bounding the lifetime more tightly, this restriction is circumvented. |
| 7 |
#[derive(Debug)] |
| 8 |
pub(crate) struct WeakSliceMut<'a, T> { |
| 9 |
ptr: *mut T, |
| 10 |
len: usize, |
| 11 |
_marker: PhantomData<&'a mut [T]>, |
| 12 |
} |
| 13 |
|
| 14 |
impl<'a, T> WeakSliceMut<'a, T> { |
| 15 |
/// # Safety |
| 16 |
/// |
| 17 |
/// The arguments must satisfy the requirements of [`core::slice::from_raw_parts_mut`]. The |
| 18 |
/// difference versus a slice is that the slice requirements are only enforced when a slice is |
| 19 |
/// needed, so in practice we mostly get the bounds checking and other convenient slice APIs, |
| 20 |
/// without the exact correctness constraints of a rust core/std slice. |
| 21 |
pub(crate) unsafe fn from_raw_parts_mut(ptr: *mut T, len: usize) -> Self { |
| 22 |
Self { |
| 23 |
ptr, |
| 24 |
len, |
| 25 |
_marker: PhantomData, |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
pub(crate) fn into_raw_parts(self) -> (*mut T, usize) { |
| 30 |
(self.ptr, self.len) |
| 31 |
} |
| 32 |
|
| 33 |
pub(crate) fn as_slice(&self) -> &'a [T] { |
| 34 |
unsafe { core::slice::from_raw_parts(self.ptr, self.len) } |
| 35 |
} |
| 36 |
|
| 37 |
pub(crate) fn as_mut_slice(&mut self) -> &'a mut [T] { |
| 38 |
unsafe { core::slice::from_raw_parts_mut(self.ptr, self.len) } |
| 39 |
} |
| 40 |
|
| 41 |
pub(crate) fn as_ptr(&self) -> *const T { |
| 42 |
self.ptr |
| 43 |
} |
| 44 |
|
| 45 |
pub(crate) fn as_mut_ptr(&mut self) -> *mut T { |
| 46 |
self.ptr |
| 47 |
} |
| 48 |
|
| 49 |
pub(crate) fn len(&self) -> usize { |
| 50 |
self.len |
| 51 |
} |
| 52 |
|
| 53 |
pub(crate) fn is_empty(&self) -> bool { |
| 54 |
self.len() == 0 |
| 55 |
} |
| 56 |
|
| 57 |
pub(crate) fn empty() -> Self { |
| 58 |
let buf = &mut []; |
| 59 |
Self { |
| 60 |
ptr: buf.as_mut_ptr(), |
| 61 |
len: buf.len(), |
| 62 |
_marker: PhantomData, |
| 63 |
} |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
#[derive(Debug)] |
| 68 |
pub(crate) struct WeakArrayMut<'a, T, const N: usize> { |
| 69 |
ptr: *mut [T; N], |
| 70 |
_marker: PhantomData<&'a mut [T; N]>, |
| 71 |
} |
| 72 |
|
| 73 |
impl<'a, T, const N: usize> WeakArrayMut<'a, T, N> { |
| 74 |
/// # Safety |
| 75 |
/// |
| 76 |
/// The pointer must be [convertable to a reference](https://doc.rust-lang.org/std/ptr/index.html#pointer-to-reference-conversion). |
| 77 |
pub(crate) unsafe fn from_ptr(ptr: *mut [T; N]) -> Self { |
| 78 |
Self { |
| 79 |
ptr, |
| 80 |
_marker: PhantomData, |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
pub(crate) fn as_slice(&self) -> &'a [T] { |
| 85 |
unsafe { core::slice::from_raw_parts(self.ptr.cast(), N) } |
| 86 |
} |
| 87 |
|
| 88 |
pub(crate) fn as_mut_slice(&mut self) -> &'a mut [T] { |
| 89 |
unsafe { core::slice::from_raw_parts_mut(self.ptr.cast(), N) } |
| 90 |
} |
| 91 |
|
| 92 |
pub(crate) fn as_mut_ptr(&mut self) -> *mut [T; N] { |
| 93 |
self.ptr |
| 94 |
} |
| 95 |
} |