musli_zerocopy/buf/
store_buf.rs

1use core::slice::SliceIndex;
2
3use crate::traits::{UnsizedZeroCopy, ZeroCopy};
4use crate::{Buf, ByteOrder, Error, Ref, Size};
5
6mod sealed {
7    #[cfg(feature = "alloc")]
8    use crate::buf::OwnedBuf;
9    use crate::buf::SliceMut;
10    use crate::{ByteOrder, Size};
11
12    pub trait Sealed {}
13
14    impl<E, O> Sealed for SliceMut<'_, E, O>
15    where
16        E: ByteOrder,
17        O: Size,
18    {
19    }
20
21    #[cfg(feature = "alloc")]
22    impl<E, O> Sealed for OwnedBuf<E, O>
23    where
24        E: ByteOrder,
25        O: Size,
26    {
27    }
28}
29
30/// A buffer that we can store things into.
31#[allow(clippy::len_without_is_empty)]
32pub trait StoreBuf: self::sealed::Sealed {
33    /// The sticky endianness associated with the buffer.
34    type ByteOrder: ByteOrder;
35
36    /// The sticky size associated with the buffer.
37    type Size: Size;
38
39    /// The current initialized length of the buffer.
40    #[doc(hidden)]
41    fn len(&self) -> usize;
42
43    /// Truncate the buffer to the given length.
44    #[doc(hidden)]
45    fn truncate(&mut self, len: usize);
46
47    /// Store an unsigned value.
48    #[doc(hidden)]
49    fn store_unsized<T>(&mut self, value: &T) -> Ref<T, Self::ByteOrder, Self::Size>
50    where
51        T: ?Sized + UnsizedZeroCopy;
52
53    /// Store a [`ZeroCopy`] value.
54    #[doc(hidden)]
55    fn store<T>(&mut self, value: &T) -> Ref<T, Self::ByteOrder, Self::Size>
56    where
57        T: ZeroCopy;
58
59    /// Swap the location of two references.
60    #[doc(hidden)]
61    fn swap<T>(
62        &mut self,
63        a: Ref<T, Self::ByteOrder, Self::Size>,
64        b: Ref<T, Self::ByteOrder, Self::Size>,
65    ) -> Result<(), Error>
66    where
67        T: ZeroCopy;
68
69    /// Ensure that the store buffer is aligned.
70    ///
71    /// For buffers which cannot be re-aligned, this will simply panic.
72    #[doc(hidden)]
73    fn align_in_place(&mut self);
74
75    /// Construct an offset aligned for `T` into the current buffer which points
76    /// to the next location that will be written.
77    #[doc(hidden)]
78    fn next_offset<T>(&mut self) -> usize;
79
80    /// Align by `align` and `reserve` additional space in the buffer or panic.
81    #[doc(hidden)]
82    fn next_offset_with_and_reserve(&mut self, align: usize, reserve: usize);
83
84    /// Fill the buffer with `len` repetitions of `byte`.
85    #[doc(hidden)]
86    fn fill(&mut self, byte: u8, len: usize);
87
88    /// Get an immutable slice.
89    #[doc(hidden)]
90    fn get<I>(&self, index: I) -> Option<&I::Output>
91    where
92        I: SliceIndex<[u8]>;
93
94    /// Get a mutable slice.
95    #[doc(hidden)]
96    unsafe fn get_mut<I>(&mut self, index: I) -> Option<&mut I::Output>
97    where
98        I: SliceIndex<[u8]>;
99
100    /// Get the underlying buffer.
101    #[doc(hidden)]
102    fn as_buf(&self) -> &Buf;
103
104    /// Get the underlying buffer mutably.
105    #[doc(hidden)]
106    unsafe fn as_mut_buf(&mut self) -> &mut Buf;
107}