rkyv_test/vec/
raw.rs

1use crate::{
2    ser::Serializer,
3    vec::{ArchivedVec, VecResolver},
4    Archive, Serialize,
5};
6use core::{
7    borrow::Borrow,
8    cmp,
9    ops::{Deref, Index, IndexMut},
10    pin::Pin,
11    slice::SliceIndex,
12};
13
14/// An archived [`Vec`].
15///
16/// This uses a [`RelPtr`](crate::rel_ptr::RelPtr) to a `[T]` under the hood. Unlike
17/// [`ArchivedString`](crate::string::ArchivedString), it does not have an inline representation.
18#[derive(Hash, Eq, Debug)]
19#[repr(transparent)]
20pub struct RawArchivedVec<T> {
21    inner: ArchivedVec<T>,
22}
23
24impl<T> RawArchivedVec<T> {
25    /// Returns a pointer to the first element of the archived vec.
26    #[inline]
27    pub fn as_ptr(&self) -> *const T {
28        self.inner.as_ptr()
29    }
30
31    /// Returns the number of elements in the archived vec.
32    #[inline]
33    pub fn len(&self) -> usize {
34        self.inner.len()
35    }
36
37    /// Returns whether the archived vec is empty.
38    #[inline]
39    pub fn is_empty(&self) -> bool {
40        self.inner.is_empty()
41    }
42
43    /// Gets the elements of the archived vec as a slice.
44    #[inline]
45    pub fn as_slice(&self) -> &[T] {
46        self.inner.as_slice()
47    }
48
49    /// Gets the elements of the archived vec as a pinned mutable slice.
50    #[inline]
51    pub fn pin_mut_slice(self: Pin<&mut Self>) -> Pin<&mut [T]> {
52        unsafe { self.map_unchecked_mut(|s| &mut s.inner).pin_mut_slice() }
53    }
54
55    // This method can go away once pinned slices have indexing support
56    // https://github.com/rust-lang/rust/pull/78370
57
58    /// Gets the element at the given index ot this archived vec as a pinned mutable reference.
59    #[inline]
60    pub fn index_pin<I>(self: Pin<&mut Self>, index: I) -> Pin<&mut <[T] as Index<I>>::Output>
61    where
62        [T]: IndexMut<I>,
63    {
64        unsafe { self.map_unchecked_mut(|s| &mut s.inner).index_pin(index) }
65    }
66
67    /// Resolves an archived `Vec` from a given slice.
68    ///
69    /// # Safety
70    ///
71    /// - `pos` must be the position of `out` within the archive
72    /// - `resolver` must be the result of serializing `value` with
73    ///   [`serialize_copy_from_slice`](RawArchivedVec::serialize_copy_from_slice).
74    #[inline]
75    pub unsafe fn resolve_from_slice<U: Archive<Archived = T>>(
76        slice: &[U],
77        pos: usize,
78        resolver: VecResolver,
79        out: *mut Self,
80    ) {
81        ArchivedVec::resolve_from_slice(slice, pos, resolver, out.cast());
82    }
83
84    /// Serializes an archived `Vec` from a given slice by directly copying bytes.
85    ///
86    /// # Safety
87    ///
88    /// The type being serialized must be copy-safe. Copy-safe types must be trivially copyable
89    /// (have the same archived and unarchived representations) and contain no padding bytes. In
90    /// situations where copying uninitialized bytes the output is acceptable, this function may be
91    /// used with types that contain padding bytes.
92    ///
93    /// Additionally, the type being serialized must not require any validation. All bit patterns
94    /// must represent valid values.
95    #[inline]
96    pub unsafe fn serialize_copy_from_slice<U, S>(
97        slice: &[U],
98        serializer: &mut S,
99    ) -> Result<VecResolver, S::Error>
100    where
101        U: Serialize<S, Archived = T>,
102        S: Serializer + ?Sized,
103    {
104        ArchivedVec::serialize_copy_from_slice(slice, serializer)
105    }
106}
107
108impl<T> AsRef<[T]> for RawArchivedVec<T> {
109    #[inline]
110    fn as_ref(&self) -> &[T] {
111        self.inner.as_ref()
112    }
113}
114
115impl<T> Borrow<[T]> for RawArchivedVec<T> {
116    #[inline]
117    fn borrow(&self) -> &[T] {
118        self.inner.borrow()
119    }
120}
121
122impl<T> Deref for RawArchivedVec<T> {
123    type Target = [T];
124
125    #[inline]
126    fn deref(&self) -> &Self::Target {
127        self.inner.deref()
128    }
129}
130
131impl<T, I: SliceIndex<[T]>> Index<I> for RawArchivedVec<T> {
132    type Output = <[T] as Index<I>>::Output;
133
134    #[inline]
135    fn index(&self, index: I) -> &Self::Output {
136        self.inner.index(index)
137    }
138}
139
140impl<T: PartialEq<U>, U> PartialEq<RawArchivedVec<U>> for RawArchivedVec<T> {
141    #[inline]
142    fn eq(&self, other: &RawArchivedVec<U>) -> bool {
143        self.inner.eq(&other.inner)
144    }
145}
146
147impl<T: PartialEq<U>, U, const N: usize> PartialEq<[U; N]> for RawArchivedVec<T> {
148    #[inline]
149    fn eq(&self, other: &[U; N]) -> bool {
150        self.inner.eq(&other[..])
151    }
152}
153
154impl<T: PartialEq<U>, U, const N: usize> PartialEq<RawArchivedVec<T>> for [U; N] {
155    #[inline]
156    fn eq(&self, other: &RawArchivedVec<T>) -> bool {
157        self.eq(&other.inner)
158    }
159}
160
161impl<T: PartialEq<U>, U> PartialEq<[U]> for RawArchivedVec<T> {
162    #[inline]
163    fn eq(&self, other: &[U]) -> bool {
164        self.inner.eq(other)
165    }
166}
167
168impl<T: PartialEq<U>, U> PartialEq<RawArchivedVec<U>> for [T] {
169    #[inline]
170    fn eq(&self, other: &RawArchivedVec<U>) -> bool {
171        self.eq(&other.inner)
172    }
173}
174
175impl<T: PartialOrd> PartialOrd<RawArchivedVec<T>> for RawArchivedVec<T> {
176    #[inline]
177    fn partial_cmp(&self, other: &RawArchivedVec<T>) -> Option<cmp::Ordering> {
178        self.inner.partial_cmp(&other.inner)
179    }
180}
181
182impl<T: Ord> Ord for RawArchivedVec<T> {
183    #[inline]
184    fn cmp(&self, other: &Self) -> cmp::Ordering {
185        self.inner.cmp(&other.inner)
186    }
187}
188
189impl<T: PartialOrd> PartialOrd<[T]> for RawArchivedVec<T> {
190    #[inline]
191    fn partial_cmp(&self, other: &[T]) -> Option<cmp::Ordering> {
192        self.inner.partial_cmp(other)
193    }
194}
195
196impl<T: PartialOrd> PartialOrd<RawArchivedVec<T>> for [T] {
197    #[inline]
198    fn partial_cmp(&self, other: &RawArchivedVec<T>) -> Option<cmp::Ordering> {
199        self.partial_cmp(&other.inner)
200    }
201}
202
203#[cfg(feature = "validation")]
204const _: () = {
205    use crate::validation::{owned::CheckOwnedPointerError, ArchiveContext};
206    use bytecheck::{CheckBytes, Error};
207
208    impl<T, C> CheckBytes<C> for RawArchivedVec<T>
209    where
210        T: CheckBytes<C>,
211        C: ArchiveContext + ?Sized,
212        C::Error: Error,
213    {
214        type Error = CheckOwnedPointerError<[T], C>;
215
216        #[inline]
217        unsafe fn check_bytes<'a>(
218            value: *const Self,
219            context: &mut C,
220        ) -> Result<&'a Self, Self::Error> {
221            ArchivedVec::<T>::check_bytes_with::<C, _>(value.cast(), context, |_, _| Ok(()))?;
222            Ok(&*value)
223        }
224    }
225};