|
| 1 | +// The `Archive`, `Serialize` and `Deserialize` impls below mirror rkyv's own impls for |
| 2 | +// `[T; N]` (`rkyv-0.8/src/impls/core/mod.rs`) 1:1. In particular, they share the same |
| 3 | +// behavior on a failed element-wise serialize/deserialize: previously-written entries in |
| 4 | +// the resolver/result `MaybeUninit` are leaked rather than dropped. For typical rkyv |
| 5 | +// `Resolver`s (often `()`) and `Copy` element types this is a non-issue; staying in sync |
| 6 | +// with upstream is preferable to diverging here. |
| 7 | + |
| 8 | +use core::mem; |
| 9 | + |
| 10 | +use rkyv::{ |
| 11 | + rancor::Fallible, |
| 12 | + traits::{CopyOptimization, NoUndef}, |
| 13 | + Archive, Deserialize, Place, Portable, Serialize, |
| 14 | +}; |
| 15 | + |
| 16 | +use crate::{ArrayLength, GenericArray}; |
| 17 | + |
| 18 | +// SAFETY: `GenericArray<T, N>` is a `T` array and so is portable as long as `T` is also |
| 19 | +// `Portable`. |
| 20 | +unsafe impl<T: Portable, N: ArrayLength> Portable for GenericArray<T, N> {} |
| 21 | +// SAFETY: `GenericArray<T, N>` is a `T` array and so has no uninitialized bytes as long as |
| 22 | +// `T` also has no uninitialized bytes. |
| 23 | +unsafe impl<T: NoUndef, N: ArrayLength> NoUndef for GenericArray<T, N> {} |
| 24 | + |
| 25 | +/// Gets a `Place` to the `i`-th element of the array. |
| 26 | +/// |
| 27 | +/// # Safety |
| 28 | +/// |
| 29 | +/// `i` must be in-bounds for the array pointed to by this place. |
| 30 | +/// |
| 31 | +/// This is a 1:1 copy of [`Place<[T; N]>::index`] |
| 32 | +unsafe fn index_for_place_generic_array<T, N: ArrayLength>( |
| 33 | + place: Place<GenericArray<T, N>>, |
| 34 | + i: usize, |
| 35 | +) -> Place<T> { |
| 36 | + // SAFETY: The caller has guaranteed that `i` is in-bounds for the array |
| 37 | + // pointed to by this place. |
| 38 | + let ptr = unsafe { place.ptr().cast::<T>().add(i) }; |
| 39 | + // SAFETY: `ptr` is an element of `self`, and so is also properly |
| 40 | + // aligned, dereferenceable, and all of its bytes are initialized. |
| 41 | + unsafe { Place::new_unchecked(place.pos() + i * mem::size_of::<T>(), ptr) } |
| 42 | +} |
| 43 | + |
| 44 | +impl<T: Archive, N: ArrayLength> Archive for GenericArray<T, N> { |
| 45 | + const COPY_OPTIMIZATION: CopyOptimization<Self> = |
| 46 | + unsafe { CopyOptimization::enable_if(T::COPY_OPTIMIZATION.is_enabled()) }; |
| 47 | + |
| 48 | + type Archived = GenericArray<T::Archived, N>; |
| 49 | + type Resolver = GenericArray<T::Resolver, N>; |
| 50 | + |
| 51 | + fn resolve(&self, resolver: Self::Resolver, out: Place<Self::Archived>) { |
| 52 | + for (i, (value, resolver)) in self.iter().zip(resolver).enumerate() { |
| 53 | + let out_i = unsafe { index_for_place_generic_array(out, i) }; |
| 54 | + value.resolve(resolver, out_i); |
| 55 | + } |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +impl<T, S, N: ArrayLength> Serialize<S> for GenericArray<T, N> |
| 60 | +where |
| 61 | + T: Serialize<S>, |
| 62 | + S: Fallible + ?Sized, |
| 63 | +{ |
| 64 | + fn serialize(&self, serializer: &mut S) -> Result<Self::Resolver, S::Error> { |
| 65 | + let mut result = core::mem::MaybeUninit::<Self::Resolver>::uninit(); |
| 66 | + let result_ptr = result.as_mut_ptr().cast::<T::Resolver>(); |
| 67 | + for (i, value) in self.iter().enumerate() { |
| 68 | + unsafe { |
| 69 | + result_ptr.add(i).write(value.serialize(serializer)?); |
| 70 | + } |
| 71 | + } |
| 72 | + unsafe { Ok(result.assume_init()) } |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +impl<T, D, N: ArrayLength> Deserialize<GenericArray<T, N>, D> for GenericArray<T::Archived, N> |
| 77 | +where |
| 78 | + T: Archive, |
| 79 | + T::Archived: Deserialize<T, D>, |
| 80 | + D: Fallible + ?Sized, |
| 81 | +{ |
| 82 | + fn deserialize(&self, deserializer: &mut D) -> Result<GenericArray<T, N>, D::Error> { |
| 83 | + let mut result = core::mem::MaybeUninit::<GenericArray<T, N>>::uninit(); |
| 84 | + let result_ptr = result.as_mut_ptr().cast::<T>(); |
| 85 | + for (i, value) in self.iter().enumerate() { |
| 86 | + unsafe { |
| 87 | + result_ptr.add(i).write(value.deserialize(deserializer)?); |
| 88 | + } |
| 89 | + } |
| 90 | + unsafe { Ok(result.assume_init()) } |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +#[cfg(test)] |
| 95 | +mod tests { |
| 96 | + use crate::typenum::{U0, U32, U6}; |
| 97 | + use crate::{arr, GenericArray}; |
| 98 | + use rkyv::rancor::Error; |
| 99 | + use rkyv::traits::{NoUndef, Portable}; |
| 100 | + |
| 101 | + const fn assert_portable_noundef<T: Portable + NoUndef>() {} |
| 102 | + const _: () = assert_portable_noundef::<GenericArray<u8, U32>>(); |
| 103 | + const _: () = assert_portable_noundef::<GenericArray<u8, U0>>(); |
| 104 | + |
| 105 | + #[test] |
| 106 | + fn test_rkyv_roundtrip() { |
| 107 | + let array: GenericArray<u32, U6> = arr![1, 2, 3, 4, 5, 6]; |
| 108 | + let bytes = rkyv::to_bytes::<Error>(&array).unwrap(); |
| 109 | + let archived = |
| 110 | + unsafe { rkyv::access_unchecked::<rkyv::Archived<GenericArray<u32, U6>>>(&bytes) }; |
| 111 | + for (i, el) in archived.iter().enumerate() { |
| 112 | + assert_eq!(el.to_native(), array[i]); |
| 113 | + } |
| 114 | + let deserialized: GenericArray<u32, U6> = |
| 115 | + rkyv::deserialize::<GenericArray<u32, U6>, Error>(archived).unwrap(); |
| 116 | + assert_eq!(deserialized, array); |
| 117 | + } |
| 118 | + |
| 119 | + // Exercises a `T` with a non-trivial `Resolver` and `Drop` (`String` archives via an |
| 120 | + // out-of-line buffer, so `Resolver` carries position metadata that owns nothing but |
| 121 | + // the deserialized `T` does). A regression in either Serialize or Deserialize that |
| 122 | + // miscounted indices would surface here as a corrupted string or a leak under Miri. |
| 123 | + #[cfg(feature = "alloc")] |
| 124 | + #[test] |
| 125 | + fn test_rkyv_roundtrip_string() { |
| 126 | + use alloc::string::String; |
| 127 | + use typenum::U3; |
| 128 | + |
| 129 | + let array: GenericArray<String, U3> = arr![ |
| 130 | + String::from("hello"), |
| 131 | + String::from("rkyv"), |
| 132 | + String::from("world") |
| 133 | + ]; |
| 134 | + let bytes = rkyv::to_bytes::<Error>(&array).unwrap(); |
| 135 | + let archived = |
| 136 | + unsafe { rkyv::access_unchecked::<rkyv::Archived<GenericArray<String, U3>>>(&bytes) }; |
| 137 | + for (i, el) in archived.iter().enumerate() { |
| 138 | + assert_eq!(el.as_str(), array[i].as_str()); |
| 139 | + } |
| 140 | + let deserialized: GenericArray<String, U3> = |
| 141 | + rkyv::deserialize::<GenericArray<String, U3>, Error>(archived).unwrap(); |
| 142 | + assert_eq!(deserialized, array); |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +#[cfg(all(test, feature = "bytecheck-0_8"))] |
| 147 | +mod tests_full { |
| 148 | + use crate::typenum::U6; |
| 149 | + use crate::{arr, GenericArray}; |
| 150 | + use rkyv::rancor::Error; |
| 151 | + |
| 152 | + #[test] |
| 153 | + fn test_validated_roundtrip() { |
| 154 | + let array: GenericArray<u32, U6> = arr![10, 20, 30, 40, 50, 60]; |
| 155 | + let bytes = rkyv::to_bytes::<Error>(&array).unwrap(); |
| 156 | + let deserialized: GenericArray<u32, U6> = |
| 157 | + rkyv::from_bytes::<GenericArray<u32, U6>, Error>(&bytes).unwrap(); |
| 158 | + assert_eq!(deserialized, array); |
| 159 | + } |
| 160 | + |
| 161 | + #[test] |
| 162 | + fn test_validation_rejects_truncated() { |
| 163 | + let array: GenericArray<u32, U6> = arr![1, 2, 3, 4, 5, 6]; |
| 164 | + let bytes = rkyv::to_bytes::<Error>(&array).unwrap(); |
| 165 | + let truncated = &bytes[..bytes.len() - 1]; |
| 166 | + let result = rkyv::access::<rkyv::Archived<GenericArray<u32, U6>>, Error>(truncated); |
| 167 | + assert!(result.is_err()); |
| 168 | + } |
| 169 | +} |
0 commit comments