Debian Codegraph @

third_party/rust/zerovec/src/ule/slices.rs

raw
1 // This file is part of ICU4X. For terms of use, please see the file
2 // called LICENSE at the top level of the ICU4X source tree
3 // (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
4
5 use crate::ule::*;
6 use core::str;
7
8 // Safety (based on the safety checklist on the ULE trait):
9 // 1. [T; N] does not include any uninitialized or padding bytes since T is ULE
10 // 2. [T; N] is aligned to 1 byte since T is ULE
11 // 3. The impl of validate_byte_slice() returns an error if any byte is not valid.
12 // 4. The impl of validate_byte_slice() returns an error if there are leftover bytes.
13 // 5. The other ULE methods use the default impl.
14 // 6. [T; N] byte equality is semantic equality since T is ULE
15 unsafe impl<T: ULE, const N: usize> ULE for [T; N] {
16 #[inline]
17 fn validate_byte_slice(bytes: &[u8]) -> Result<(), ZeroVecError> {
18 // a slice of multiple Selfs is equivalent to just a larger slice of Ts
19 T::validate_byte_slice(bytes)
20 }
21 }
22
23 impl<T: AsULE, const N: usize> AsULE for [T; N] {
24 type ULE = [T::ULE; N];
25 #[inline]
26 fn to_unaligned(self) -> Self::ULE {
27 self.map(T::to_unaligned)
28 }
29 #[inline]
30 fn from_unaligned(unaligned: Self::ULE) -> Self {
31 unaligned.map(T::from_unaligned)
32 }
33 }
34
35 unsafe impl<T: EqULE, const N: usize> EqULE for [T; N] {}
36
37 // Safety (based on the safety checklist on the VarULE trait):
38 // 1. str does not include any uninitialized or padding bytes.
39 // 2. str is aligned to 1 byte.
40 // 3. The impl of `validate_byte_slice()` returns an error if any byte is not valid.
41 // 4. The impl of `validate_byte_slice()` returns an error if the slice cannot be used in its entirety
42 // 5. The impl of `from_byte_slice_unchecked()` returns a reference to the same data.
43 // 6. `parse_byte_slice()` is equivalent to `validate_byte_slice()` followed by `from_byte_slice_unchecked()`
44 // 7. str byte equality is semantic equality
45 unsafe impl VarULE for str {
46 #[inline]
47 fn validate_byte_slice(bytes: &[u8]) -> Result<(), ZeroVecError> {
48 str::from_utf8(bytes).map_err(|_| ZeroVecError::parse::<Self>())?;
49 Ok(())
50 }
51
52 #[inline]
53 fn parse_byte_slice(bytes: &[u8]) -> Result<&Self, ZeroVecError> {
54 str::from_utf8(bytes).map_err(|_| ZeroVecError::parse::<Self>())
55 }
56 /// Invariant: must be safe to call when called on a slice that previously
57 /// succeeded with `parse_byte_slice`
58 #[inline]
59 unsafe fn from_byte_slice_unchecked(bytes: &[u8]) -> &Self {
60 str::from_utf8_unchecked(bytes)
61 }
62 }
63
64 /// Note: VarULE is well-defined for all `[T]` where `T: ULE`, but [`ZeroSlice`] is more ergonomic
65 /// when `T` is a low-level ULE type. For example:
66 ///
67 /// ```no_run
68 /// # use zerovec::ZeroSlice;
69 /// # use zerovec::VarZeroVec;
70 /// # use zerovec::ule::AsULE;
71 /// // OK: [u8] is a useful type
72 /// let _: VarZeroVec<[u8]> = unimplemented!();
73 ///
74 /// // Technically works, but [u32::ULE] is not very useful
75 /// let _: VarZeroVec<[<u32 as AsULE>::ULE]> = unimplemented!();
76 ///
77 /// // Better: ZeroSlice<u32>
78 /// let _: VarZeroVec<ZeroSlice<u32>> = unimplemented!();
79 /// ```
80 ///
81 /// [`ZeroSlice`]: crate::ZeroSlice
82 // Safety (based on the safety checklist on the VarULE trait):
83 // 1. [T] does not include any uninitialized or padding bytes (achieved by being a slice of a ULE type)
84 // 2. [T] is aligned to 1 byte (achieved by being a slice of a ULE type)
85 // 3. The impl of `validate_byte_slice()` returns an error if any byte is not valid.
86 // 4. The impl of `validate_byte_slice()` returns an error if the slice cannot be used in its entirety
87 // 5. The impl of `from_byte_slice_unchecked()` returns a reference to the same data.
88 // 6. All other methods are defaulted
89 // 7. `[T]` byte equality is semantic equality (achieved by being a slice of a ULE type)
90 unsafe impl<T> VarULE for [T]
91 where
92 T: ULE,
93 {
94 #[inline]
95 fn validate_byte_slice(slice: &[u8]) -> Result<(), ZeroVecError> {
96 T::validate_byte_slice(slice)
97 }
98
99 #[inline]
100 unsafe fn from_byte_slice_unchecked(bytes: &[u8]) -> &Self {
101 T::from_byte_slice_unchecked(bytes)
102 }
103 }