planus/
vectors.rs

1use crate::{slice_helpers::SliceWithStartOffset, traits::VectorRead};
2use core::marker::PhantomData;
3
4/// A `Vec` like view into a serialized buffer that deserializes on demand.
5pub struct Vector<'buf, T: ?Sized> {
6    pub(crate) buffer: SliceWithStartOffset<'buf>,
7    pub(crate) len: usize,
8    pub(crate) _marker: PhantomData<&'buf T>,
9}
10
11impl<'buf, T: ?Sized> Copy for Vector<'buf, T> {}
12impl<'buf, T: ?Sized> Clone for Vector<'buf, T> {
13    #[inline]
14    fn clone(&self) -> Self {
15        *self
16    }
17}
18
19impl<'buf, T: VectorRead<'buf> + core::fmt::Debug> core::fmt::Debug for Vector<'buf, T>
20where
21    T: core::fmt::Debug,
22{
23    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
24        f.debug_list().entries(self.iter()).finish()
25    }
26}
27
28impl<T: ?Sized + 'static> Vector<'static, T> {
29    // Needed because beta 1.62 gave a weird error
30    #[allow(unused_attributes)]
31    #[doc(hidden)]
32    pub const EMPTY: Self = Self {
33        buffer: SliceWithStartOffset {
34            buffer: &[],
35            offset_from_start: 0,
36        },
37        len: 0,
38        _marker: PhantomData,
39    };
40}
41
42impl<'buf, T: ?Sized> Vector<'buf, T> {
43    /// Checks if the vector is empty.
44    pub fn is_empty(self) -> bool {
45        self.len == 0
46    }
47
48    /// Returns the number of elements in the vector.
49    pub fn len(self) -> usize {
50        self.len
51    }
52}
53
54impl<'buf, T: VectorRead<'buf>> Vector<'buf, T> {
55    /// Returns the element at the given index, or None if out of bounds.
56    #[inline]
57    pub fn get(self, index: usize) -> Option<T> {
58        if index < self.len {
59            Some(unsafe { T::from_buffer(self.buffer, T::STRIDE * index) })
60        } else {
61            None
62        }
63    }
64
65    /// Returns an iterator over the vector.
66    #[inline]
67    pub fn iter(self) -> VectorIter<'buf, T> {
68        VectorIter(self)
69    }
70}
71
72impl<'buf, T: VectorRead<'buf>> Vector<'buf, T> {
73    /// Copies self into a new `Vec`.
74    pub fn to_vec<O>(&self) -> crate::Result<alloc::vec::Vec<O>>
75    where
76        O: core::convert::TryFrom<T>,
77        crate::errors::Error: From<O::Error>,
78    {
79        self.iter()
80            .map(|v| O::try_from(v).map_err(crate::errors::Error::from))
81            .collect()
82    }
83}
84
85impl<'buf, T, E> Vector<'buf, core::result::Result<T, E>> {
86    /// Copies self into a new `Vec`.
87    pub fn to_vec_result<O>(&self) -> crate::Result<alloc::vec::Vec<O>>
88    where
89        T: crate::traits::VectorReadInner<'buf>,
90        E: core::convert::From<T::Error>,
91        O: core::convert::TryFrom<T>,
92        crate::errors::Error: From<E> + From<O::Error>,
93    {
94        self.iter()
95            .map(|v| O::try_from(v?).map_err(|e| e.into()))
96            .collect()
97    }
98}
99
100impl<'buf, T: VectorRead<'buf>> IntoIterator for Vector<'buf, T> {
101    type Item = T;
102    type IntoIter = VectorIter<'buf, T>;
103
104    #[inline]
105    fn into_iter(self) -> Self::IntoIter {
106        self.iter()
107    }
108}
109
110#[derive(Clone)]
111/// An iterator over the elements of a `Vector`.
112pub struct VectorIter<'buf, T: ?Sized>(Vector<'buf, T>);
113
114impl<'buf, T: VectorRead<'buf>> Iterator for VectorIter<'buf, T> {
115    type Item = T;
116
117    #[inline]
118    fn next(&mut self) -> Option<T> {
119        if self.0.len > 0 {
120            let result = unsafe { T::from_buffer(self.0.buffer, 0) };
121            self.0.buffer = self
122                .0
123                .buffer
124                .advance(T::STRIDE)
125                .expect("IMPOSSIBLE: we checked the length on creation");
126            self.0.len -= 1;
127            Some(result)
128        } else {
129            None
130        }
131    }
132}
133
134impl<'buf, T: VectorRead<'buf> + core::fmt::Debug> core::fmt::Debug for VectorIter<'buf, T> {
135    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
136        f.debug_tuple("VectorIter").field(&self.0).finish()
137    }
138}