icu_pattern/implementations.rs
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
5use crate::{Pattern, PatternBackend};
6
7use alloc::boxed::Box;
8use zerovec::{
9 maps::ZeroMapKV,
10 ule::{UleError, VarULE},
11 VarZeroSlice, VarZeroVec,
12};
13
14impl<'a, B: PatternBackend> ZeroMapKV<'a> for Pattern<B>
15where
16 Pattern<B>: VarULE,
17{
18 type Container = VarZeroVec<'a, Pattern<B>>;
19 type Slice = VarZeroSlice<Pattern<B>>;
20 type GetType = Pattern<B>;
21 type OwnedType = Box<Pattern<B>>;
22}
23
24/// Implement `VarULE` for `Pattern<SinglePlaceholder, str>`.
25///
26/// # Safety
27///
28/// Safety checklist for `ULE`:
29///
30/// 1. `Pattern<B>` does not include any uninitialized or padding bytes.
31/// 2. `Pattern<B>` is aligned to 1 byte.
32/// 3. The implementation of `validate_bytes()` returns an error
33/// if any byte is not valid.
34/// 4. The implementation of `validate_bytes()` returns an error
35/// if the slice cannot be used to build a `Pattern<B>` in its entirety.
36/// 5. The implementation of `from_bytes_unchecked()` returns a reference to the same data.
37/// 6. `parse_bytes()` is equivalent to `validate_bytes()` followed by `from_bytes_unchecked()`.
38/// 7. `Pattern<B>` byte equality is semantic equality.
39unsafe impl<B, S: ?Sized + VarULE> VarULE for Pattern<B>
40where
41 B: PatternBackend<Store = S>,
42{
43 fn validate_bytes(bytes: &[u8]) -> Result<(), UleError> {
44 let store = S::parse_bytes(bytes)?;
45 B::validate_store(store).map_err(|_| UleError::parse::<Self>())
46 }
47
48 unsafe fn from_bytes_unchecked(bytes: &[u8]) -> &Self {
49 let store = S::from_bytes_unchecked(bytes);
50 Self::from_ref_store_unchecked(store)
51 }
52}