epserde/impls/
vec.rs

1/*
2 * SPDX-FileCopyrightText: 2023 Inria
3 * SPDX-FileCopyrightText: 2023 Sebastiano Vigna
4 *
5 * SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
6 */
7
8//! Implementations for vectors.
9
10use crate::deser;
11use crate::deser::helpers::*;
12use crate::deser::*;
13use crate::ser;
14use crate::ser::helpers::*;
15use crate::ser::*;
16use crate::traits::*;
17
18#[cfg(not(feature = "std"))]
19use alloc::{boxed::Box, vec::Vec};
20
21unsafe impl<T> CopyType for Vec<T> {
22    type Copy = Deep;
23}
24
25// For use with PhantomData
26impl<T: TypeHash> TypeHash for Vec<T> {
27    fn type_hash(hasher: &mut impl core::hash::Hasher) {
28        <Box<[T]>>::type_hash(hasher);
29    }
30}
31
32impl<T: CopyType + SerInner> SerInner for Vec<T>
33where
34    Vec<T>: SerHelper<<T as CopyType>::Copy>,
35{
36    type SerType = Box<[T::SerType]>;
37    const IS_ZERO_COPY: bool = false;
38    unsafe fn _ser_inner(&self, backend: &mut impl WriteWithNames) -> ser::Result<()> {
39        unsafe { SerHelper::_ser_inner(self, backend) }
40    }
41}
42
43impl<T: ZeroCopy> SerHelper<Zero> for Vec<T> {
44    #[inline(always)]
45    unsafe fn _ser_inner(&self, backend: &mut impl WriteWithNames) -> ser::Result<()> {
46        ser_slice_zero(backend, self.as_slice())
47    }
48}
49
50impl<T: DeepCopy> SerHelper<Deep> for Vec<T> {
51    #[inline(always)]
52    unsafe fn _ser_inner(&self, backend: &mut impl WriteWithNames) -> ser::Result<()> {
53        ser_slice_deep(backend, self.as_slice())
54    }
55}
56
57// This delegates to a private helper trait which we can specialize on in stable rust
58impl<T: CopyType + DeserInner> DeserInner for Vec<T>
59where
60    Vec<T>: DeserHelper<<T as CopyType>::Copy, FullType = Vec<T>>,
61{
62    type DeserType<'a> = <Vec<T> as DeserHelper<<T as CopyType>::Copy>>::DeserType<'a>;
63    #[inline(always)]
64    unsafe fn _deser_full_inner(backend: &mut impl ReadWithPos) -> deser::Result<Self> {
65        unsafe { <Vec<T> as DeserHelper<<T as CopyType>::Copy>>::_deser_full_inner_impl(backend) }
66    }
67
68    #[inline(always)]
69    unsafe fn _deser_eps_inner<'a>(
70        backend: &mut SliceWithPos<'a>,
71    ) -> deser::Result<<Vec<T> as DeserHelper<<T as CopyType>::Copy>>::DeserType<'a>> {
72        unsafe { <Vec<T> as DeserHelper<<T as CopyType>::Copy>>::_deser_eps_inner_impl(backend) }
73    }
74}
75
76impl<T: ZeroCopy + DeserInner> DeserHelper<Zero> for Vec<T> {
77    type FullType = Self;
78    type DeserType<'a> = &'a [T];
79    #[inline(always)]
80    unsafe fn _deser_full_inner_impl(backend: &mut impl ReadWithPos) -> deser::Result<Self> {
81        unsafe { deser_full_vec_zero(backend) }
82    }
83    #[inline(always)]
84    unsafe fn _deser_eps_inner_impl<'a>(
85        backend: &mut SliceWithPos<'a>,
86    ) -> deser::Result<DeserType<'a, Self>> {
87        unsafe { deser_eps_slice_zero(backend) }
88    }
89}
90
91impl<T: DeepCopy + DeserInner> DeserHelper<Deep> for Vec<T> {
92    type FullType = Self;
93    type DeserType<'a> = Vec<DeserType<'a, T>>;
94    #[inline(always)]
95    unsafe fn _deser_full_inner_impl(backend: &mut impl ReadWithPos) -> deser::Result<Self> {
96        deser_full_vec_deep::<T>(backend)
97    }
98    #[inline(always)]
99    unsafe fn _deser_eps_inner_impl<'a>(
100        backend: &mut SliceWithPos<'a>,
101    ) -> deser::Result<DeserType<'a, Self>> {
102        deser_eps_vec_deep::<T>(backend)
103    }
104}