|
2 | 2 |
|
3 | 3 | #![allow(unsafe_code)] |
4 | 4 |
|
| 5 | +#[cfg(feature = "alloc")] |
| 6 | +use alloc::vec::Vec; |
5 | 7 | use core::mem::MaybeUninit; |
6 | 8 | use core::slice; |
7 | 9 |
|
| 10 | +/// A memory buffer that may be uninitialized. |
| 11 | +pub trait Buffer<T> { |
| 12 | + /// The result of the process operation. |
| 13 | + type Result; |
| 14 | + |
| 15 | + /// Convert this buffer into a maybe-unitiailized view. |
| 16 | + fn as_maybe_uninitialized(&mut self) -> &mut [MaybeUninit<T>]; |
| 17 | + |
| 18 | + /// Convert a finished buffer pointer into its result. |
| 19 | + /// |
| 20 | + /// # Safety |
| 21 | + /// |
| 22 | + /// At least `len` bytes of the buffer must now be initialized. |
| 23 | + unsafe fn finish(self, len: usize) -> Self::Result; |
| 24 | +} |
| 25 | + |
| 26 | +/// Implements [`Buffer`] around the a slice of bytes. |
| 27 | +/// |
| 28 | +/// `Result` is a `usize` indicating how many bytes were written. |
| 29 | +impl<T> Buffer<T> for &mut [T] { |
| 30 | + type Result = usize; |
| 31 | + |
| 32 | + #[inline] |
| 33 | + fn as_maybe_uninitialized(&mut self) -> &mut [MaybeUninit<T>] { |
| 34 | + // SAFETY: This just casts away the knowledge that the elements are |
| 35 | + // initialized. |
| 36 | + unsafe { core::mem::transmute::<&mut [T], &mut [MaybeUninit<T>]>(self) } |
| 37 | + } |
| 38 | + |
| 39 | + #[inline] |
| 40 | + unsafe fn finish(self, len: usize) -> Self::Result { |
| 41 | + len |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +/// Implements [`Buffer`] around the a slice of bytes. |
| 46 | +/// |
| 47 | +/// `Result` is a `usize` indicating how many bytes were written. |
| 48 | +impl<T, const N: usize> Buffer<T> for &mut [T; N] { |
| 49 | + type Result = usize; |
| 50 | + |
| 51 | + #[inline] |
| 52 | + fn as_maybe_uninitialized(&mut self) -> &mut [MaybeUninit<T>] { |
| 53 | + // SAFETY: This just casts away the knowledge that the elements are |
| 54 | + // initialized. |
| 55 | + unsafe { core::mem::transmute::<&mut [T], &mut [MaybeUninit<T>]>(*self) } |
| 56 | + } |
| 57 | + |
| 58 | + #[inline] |
| 59 | + unsafe fn finish(self, len: usize) -> Self::Result { |
| 60 | + len |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +/// Implements [`Buffer`] around the a slice of bytes. |
| 65 | +/// |
| 66 | +/// `Result` is a `usize` indicating how many bytes were written. |
| 67 | +impl<T> Buffer<T> for &mut Vec<T> { |
| 68 | + type Result = usize; |
| 69 | + |
| 70 | + #[inline] |
| 71 | + fn as_maybe_uninitialized(&mut self) -> &mut [MaybeUninit<T>] { |
| 72 | + // SAFETY: This just casts away the knowledge that the elements are |
| 73 | + // initialized. |
| 74 | + unsafe { core::mem::transmute::<&mut [T], &mut [MaybeUninit<T>]>(self) } |
| 75 | + } |
| 76 | + |
| 77 | + #[inline] |
| 78 | + unsafe fn finish(self, len: usize) -> Self::Result { |
| 79 | + len |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +/// Implements [`Buffer`] around the a slice of uninitialized bytes. |
| 84 | +/// |
| 85 | +/// `Result` is a pair of slices giving the initialized and uninitialized |
| 86 | +/// subslices after the new data is written. |
| 87 | +impl<'a, T> Buffer<T> for &'a mut [MaybeUninit<T>] { |
| 88 | + type Result = (&'a mut [T], &'a mut [MaybeUninit<T>]); |
| 89 | + |
| 90 | + #[inline] |
| 91 | + fn as_maybe_uninitialized(&mut self) -> &mut [MaybeUninit<T>] { |
| 92 | + self |
| 93 | + } |
| 94 | + |
| 95 | + #[inline] |
| 96 | + unsafe fn finish(self, len: usize) -> Self::Result { |
| 97 | + let (init, uninit) = self.split_at_mut(len); |
| 98 | + |
| 99 | + // SAFETY: The user asserts that the slice is now initialized. |
| 100 | + let init = slice::from_raw_parts_mut(init.as_mut_ptr().cast::<T>(), init.len()); |
| 101 | + |
| 102 | + (init, uninit) |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +/// Implements [`Buffer`] around the `Vec` type. |
| 107 | +/// |
| 108 | +/// This implementation fills the buffer, overwriting any previous data, with |
| 109 | +/// the new data data and sets the length. |
| 110 | +#[cfg(feature = "alloc")] |
| 111 | +impl<T> Buffer<T> for Vec<T> { |
| 112 | + type Result = Vec<T>; |
| 113 | + |
| 114 | + #[inline] |
| 115 | + fn as_maybe_uninitialized(&mut self) -> &mut [MaybeUninit<T>] { |
| 116 | + self.clear(); |
| 117 | + self.spare_capacity_mut() |
| 118 | + } |
| 119 | + |
| 120 | + #[inline] |
| 121 | + unsafe fn finish(mut self, len: usize) -> Self::Result { |
| 122 | + self.set_len(len); |
| 123 | + self |
| 124 | + } |
| 125 | +} |
| 126 | + |
8 | 127 | /// Split an uninitialized byte slice into initialized and uninitialized parts. |
9 | 128 | /// |
10 | 129 | /// # Safety |
|
0 commit comments