1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
use crate::{
ser::{ScratchSpace, Serializer},
vec::{ArchivedVec, VecResolver},
Archive, Deserialize, DeserializeUnsized, Fallible, Serialize,
};
#[cfg(not(feature = "std"))]
use ::alloc::{alloc, boxed::Box, vec::Vec};
use ::core::cmp;
#[cfg(feature = "std")]
use ::std::alloc;
impl<T: PartialEq<U>, U> PartialEq<Vec<U>> for ArchivedVec<T> {
#[inline]
fn eq(&self, other: &Vec<U>) -> bool {
self.as_slice().eq(other.as_slice())
}
}
impl<T: PartialEq<U>, U> PartialEq<ArchivedVec<U>> for Vec<T> {
#[inline]
fn eq(&self, other: &ArchivedVec<U>) -> bool {
self.as_slice().eq(other.as_slice())
}
}
impl<T: PartialOrd> PartialOrd<Vec<T>> for ArchivedVec<T> {
#[inline]
fn partial_cmp(&self, other: &Vec<T>) -> Option<cmp::Ordering> {
self.as_slice().partial_cmp(other.as_slice())
}
}
impl<T: PartialOrd> PartialOrd<ArchivedVec<T>> for Vec<T> {
#[inline]
fn partial_cmp(&self, other: &ArchivedVec<T>) -> Option<cmp::Ordering> {
self.as_slice().partial_cmp(other.as_slice())
}
}
impl<T: Archive> Archive for Vec<T> {
type Archived = ArchivedVec<T::Archived>;
type Resolver = VecResolver;
#[inline]
unsafe fn resolve(&self, pos: usize, resolver: Self::Resolver, out: *mut Self::Archived) {
ArchivedVec::resolve_from_slice(self.as_slice(), pos, resolver, out);
}
}
impl<T: Serialize<S>, S: ScratchSpace + Serializer + ?Sized> Serialize<S> for Vec<T> {
#[inline]
fn serialize(&self, serializer: &mut S) -> Result<Self::Resolver, S::Error> {
ArchivedVec::<T::Archived>::serialize_from_slice(self.as_slice(), serializer)
}
}
impl<T: Archive, D: Fallible + ?Sized> Deserialize<Vec<T>, D> for ArchivedVec<T::Archived>
where
[T::Archived]: DeserializeUnsized<[T], D>,
{
#[inline]
fn deserialize(&self, deserializer: &mut D) -> Result<Vec<T>, D::Error> {
unsafe {
let data_address = self
.as_slice()
.deserialize_unsized(deserializer, |layout| alloc::alloc(layout))?;
let metadata = self.as_slice().deserialize_metadata(deserializer)?;
let ptr = ptr_meta::from_raw_parts_mut(data_address, metadata);
Ok(Box::<[T]>::from_raw(ptr).into())
}
}
}