Skip to main content

musli_zerocopy/buf/
store_buf.rs

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