use core::cmp::Ordering;
use core::ptr::Alignment;
use std::mem::MaybeUninit;
use crate::impls::lexicographical_partial_ord;
use crate::list::ArchivedList;
use crate::primitive::ArchivedUsize;
use crate::util::max_alignment;
use crate::{Serialize, SerializeError, VerifyError};
impl<T: Serialize> Serialize for Vec<T> {
type Archived = ArchivedList<T::Archived>;
const ALIGN: Alignment = max_alignment([T::ALIGN, Alignment::of::<ArchivedUsize>()]);
unsafe fn serialize(
&self,
out: *mut MaybeUninit<ArchivedList<T::Archived>>,
heap: *mut MaybeUninit<u8>,
) -> usize {
unsafe { (**self).serialize(out, heap) }
}
fn serialized_size(&self, offset: usize) -> Result<usize, SerializeError> {
(**self).serialized_size(offset)
}
#[inline]
unsafe fn verify(
this: *const ArchivedList<T::Archived>,
buffer_end: *const u8,
) -> Result<(), VerifyError> {
unsafe { <[T]>::verify(this, buffer_end) }
}
}
impl<T: PartialEq<U>, U> PartialEq<Vec<U>> for ArchivedList<T> {
fn eq(&self, other: &Vec<U>) -> bool {
self.as_slice().eq(other.as_slice())
}
}
impl<T: PartialOrd<U>, U> PartialOrd<Vec<U>> for ArchivedList<T> {
fn partial_cmp(&self, other: &Vec<U>) -> Option<Ordering> {
lexicographical_partial_ord(self.as_slice(), other.as_slice())
}
}
#[cfg(test)]
mod tests {
use crate::test::roundtrip;
#[test]
fn roundtrip_vec() {
roundtrip(&Vec::<i32>::new());
roundtrip(&vec![1, 2, 3, 4]);
}
#[test]
fn roundtrip_vec_zst() {
roundtrip(&Vec::<()>::new());
roundtrip(&vec![(), (), (), ()]);
}
#[test]
fn roundtrip_option_vec() {
roundtrip(&Some(Vec::<i32>::new()));
roundtrip(&Some(vec![1, 2, 3, 4]));
}
#[test]
fn roundtrip_result_vec() {
roundtrip(&Ok::<_, ()>(Vec::<i32>::new()));
roundtrip(&Ok::<_, ()>(vec![1, 2, 3, 4]));
roundtrip(&Err::<(), _>(Vec::<i32>::new()));
roundtrip(&Err::<(), _>(vec![1, 2, 3, 4]));
}
}