diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-06-07 05:48:48 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-06-07 05:48:48 +0000 |
commit | ef24de24a82fe681581cc130f342363c47c0969a (patch) | |
tree | 0d494f7e1a38b95c92426f58fe6eaa877303a86c /vendor/tinystr/src/ule.rs | |
parent | Releasing progress-linux version 1.74.1+dfsg1-1~progress7.99u1. (diff) | |
download | rustc-ef24de24a82fe681581cc130f342363c47c0969a.tar.xz rustc-ef24de24a82fe681581cc130f342363c47c0969a.zip |
Merging upstream version 1.75.0+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/tinystr/src/ule.rs')
-rw-r--r-- | vendor/tinystr/src/ule.rs | 48 |
1 files changed, 44 insertions, 4 deletions
diff --git a/vendor/tinystr/src/ule.rs b/vendor/tinystr/src/ule.rs index 0fa212095..eda43890b 100644 --- a/vendor/tinystr/src/ule.rs +++ b/vendor/tinystr/src/ule.rs @@ -2,20 +2,20 @@ // called LICENSE at the top level of the ICU4X source tree // (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). -use crate::TinyAsciiStr; +use crate::{TinyAsciiStr, UnvalidatedTinyAsciiStr}; use zerovec::maps::ZeroMapKV; use zerovec::ule::*; use zerovec::{ZeroSlice, ZeroVec}; // Safety (based on the safety checklist on the ULE trait): -// 1. CharULE does not include any uninitialized or padding bytes. +// 1. TinyAsciiStr does not include any uninitialized or padding bytes. // (achieved by `#[repr(transparent)]` on a type that satisfies this invariant) -// 2. CharULE is aligned to 1 byte. +// 2. TinyAsciiStr is aligned to 1 byte. // (achieved by `#[repr(transparent)]` on a type that satisfies this invariant) // 3. The impl of validate_byte_slice() returns an error if any byte is not valid. // 4. The impl of validate_byte_slice() returns an error if there are extra bytes. // 5. The other ULE methods use the default impl. -// 6. CharULE byte equality is semantic equality +// 6. TinyAsciiStr byte equality is semantic equality unsafe impl<const N: usize> ULE for TinyAsciiStr<N> { #[inline] fn validate_byte_slice(bytes: &[u8]) -> Result<(), ZeroVecError> { @@ -52,6 +52,46 @@ impl<'a, const N: usize> ZeroMapKV<'a> for TinyAsciiStr<N> { type OwnedType = TinyAsciiStr<N>; } +// Safety (based on the safety checklist on the ULE trait): +// 1. UnvalidatedTinyAsciiStr does not include any uninitialized or padding bytes. +// (achieved by `#[repr(transparent)]` on a type that satisfies this invariant) +// 2. UnvalidatedTinyAsciiStr is aligned to 1 byte. +// (achieved by `#[repr(transparent)]` on a type that satisfies this invariant) +// 3. The impl of validate_byte_slice() returns an error if any byte is not valid. +// 4. The impl of validate_byte_slice() returns an error if there are extra bytes. +// 5. The other ULE methods use the default impl. +// 6. UnvalidatedTinyAsciiStr byte equality is semantic equality +unsafe impl<const N: usize> ULE for UnvalidatedTinyAsciiStr<N> { + #[inline] + fn validate_byte_slice(bytes: &[u8]) -> Result<(), ZeroVecError> { + if bytes.len() % N != 0 { + return Err(ZeroVecError::length::<Self>(bytes.len())); + } + Ok(()) + } +} + +impl<const N: usize> AsULE for UnvalidatedTinyAsciiStr<N> { + type ULE = Self; + + #[inline] + fn to_unaligned(self) -> Self::ULE { + self + } + + #[inline] + fn from_unaligned(unaligned: Self::ULE) -> Self { + unaligned + } +} + +impl<'a, const N: usize> ZeroMapKV<'a> for UnvalidatedTinyAsciiStr<N> { + type Container = ZeroVec<'a, UnvalidatedTinyAsciiStr<N>>; + type Slice = ZeroSlice<UnvalidatedTinyAsciiStr<N>>; + type GetType = UnvalidatedTinyAsciiStr<N>; + type OwnedType = UnvalidatedTinyAsciiStr<N>; +} + #[cfg(test)] mod test { use crate::*; |