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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
//
// Implementation of the `DataBuf` trait
//
use ::core::mem::MaybeUninit;
/// Trait used to represent a data buffer, typically you'll passs a `[usize; N]` array.
///
/// Can also provide a `Vec<T>` (if the `alloc` feature is enabled) which will grow as-needed
///
/// UNSAFE: Used by the internal unsafe code, must confor to the following rules
/// - The `as_ref`/`as_mut` methods must return pointers to the same data
/// - The pointer returned by `as_mut` must be stable until either a call to `extend` or the
/// value is moved (i.e. `let a = foo.as_mut().as_ptr(); let b = foo.as_mut().as_ptr(); assert!(a == b)` always holds.)
/// - `extend` must not change any contained data (but may extend with unspecified values)
pub unsafe trait DataBuf {
/// Inner type of the buffer
type Inner: Pod;
/// Get the buffer slice as an immutable borrow
fn as_ref(&self) -> &[MaybeUninit<Self::Inner>];
/// Get the buffer slice as a mutable borrow
fn as_mut(&mut self) -> &mut [MaybeUninit<Self::Inner>];
/// Extend the buffer (fallible)
fn extend(&mut self, len: usize) -> Result<(), ()>;
/// Convert a byte count to a word count (rounding up)
fn round_to_words(bytes: usize) -> usize {
crate::round_to_words::<Self::Inner>(bytes)
}
}
/// Trait that indicates that a type is valid for any bit pattern
pub unsafe trait Pod: Copy {
/// Construct a new instance (sames as `Default::default`)
fn default() -> Self;
}
macro_rules! impl_pod {
( $($t:ty),* ) => {
$( unsafe impl Pod for $t { fn default() -> Self { 0 } } )*
}
}
impl_pod! { u8, u16, u32, u64, u128, usize }
unsafe impl<T> Pod for *const T {
fn default() -> Self { ::core::ptr::null() }
}
unsafe impl<T, U> DataBuf for &mut T
where
U: Pod,
T: DataBuf<Inner=U>,
{
type Inner = T::Inner;
fn as_ref(&self) -> &[MaybeUninit<Self::Inner>] {
(**self).as_ref()
}
fn as_mut(&mut self) -> &mut [MaybeUninit<Self::Inner>] {
(**self).as_mut()
}
fn extend(&mut self, len: usize) -> Result<(), ()> {
(**self).extend(len)
}
}
#[cfg(not(feature = "const_generics"))]
macro_rules! impl_databuf_array {
( $($n:expr),* ) => {
$(unsafe impl<T: Pod> DataBuf for [MaybeUninit<T>; $n] {
type Inner = T;
fn as_ref(&self) -> &[MaybeUninit<Self::Inner>] {
self
}
fn as_mut(&mut self) -> &mut [MaybeUninit<Self::Inner>] {
self
}
fn extend(&mut self, len: usize) -> Result<(), ()> {
if len > $n {
Err( () )
}
else {
Ok( () )
}
}
})*
}
}
#[cfg(not(feature = "const_generics"))]
impl_databuf_array! {
0, 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,48,
64,96,
128,192,
256
}
/// Array-specific impl
#[cfg(feature = "const_generics")]
unsafe impl<T: Pod, const N: usize> DataBuf for [MaybeUninit<T>; N] {
type Inner = T;
fn as_ref(&self) -> &[MaybeUninit<Self::Inner>] {
self
}
fn as_mut(&mut self) -> &mut [MaybeUninit<Self::Inner>] {
self
}
fn extend(&mut self, len: usize) -> Result<(), ()> {
if len > N {
Err(())
}
else {
Ok(())
}
}
}
/// Vector backed structures, can be used to auto-grow the allocation
///
/// ```
/// let mut buf = ::stack_dst::Fifo::<str, Vec<::std::mem::MaybeUninit<u8>>>::new();
/// buf.push_back_str("Hello world!");
/// buf.push_back_str("This is a very long string");
/// buf.push_back_str("The buffer should keep growing as it needs to");
/// for line in buf.iter() {
/// println!("{}", line);
/// }
/// ```
#[cfg(all(feature = "alloc"))]
unsafe impl<T: Pod> crate::DataBuf for ::alloc::vec::Vec< MaybeUninit<T> > {
type Inner = T;
fn as_ref(&self) -> &[MaybeUninit<Self::Inner>] {
self
}
fn as_mut(&mut self) -> &mut [MaybeUninit<Self::Inner>] {
self
}
fn extend(&mut self, len: usize) -> Result<(), ()> {
if len > self.len() {
self.resize(len, MaybeUninit::uninit());
let cap = self.capacity();
self.resize(cap, MaybeUninit::uninit());
}
Ok(())
}
}