Skip to main content

le_stream/
prefixed.rs

1use core::fmt::Debug;
2use core::marker::PhantomData;
3use core::ops::{Deref, DerefMut};
4
5mod alloc;
6mod heapless;
7
8/// A wrapper type that adds a size prefix to the data it contains.
9#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
10#[repr(transparent)]
11pub struct Prefixed<P, D> {
12    data: D,
13    prefix: PhantomData<P>,
14}
15
16impl<P, D> Prefixed<P, D> {
17    /// Extract the data.
18    pub fn into_data(self) -> D {
19        self.data
20    }
21}
22
23impl<P, D, T> AsRef<T> for Prefixed<P, D>
24where
25    T: ?Sized,
26    D: AsRef<T>,
27{
28    fn as_ref(&self) -> &T {
29        self.data.as_ref()
30    }
31}
32
33impl<P, D, T> AsMut<T> for Prefixed<P, D>
34where
35    T: ?Sized,
36    D: AsMut<T>,
37{
38    fn as_mut(&mut self) -> &mut T {
39        self.data.as_mut()
40    }
41}
42
43impl<P, D> Deref for Prefixed<P, D>
44where
45    D: Deref,
46{
47    type Target = D::Target;
48
49    fn deref(&self) -> &Self::Target {
50        &self.data
51    }
52}
53
54impl<P, D> DerefMut for Prefixed<P, D>
55where
56    D: DerefMut,
57{
58    fn deref_mut(&mut self) -> &mut Self::Target {
59        &mut self.data
60    }
61}