1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// This file is part of ICU4X. For terms of use, please see the file
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

use crate::{Pattern, PatternBackend};

use alloc::boxed::Box;
use zerovec::{
    maps::ZeroMapKV,
    ule::{UleError, VarULE},
    VarZeroSlice, VarZeroVec,
};

impl<'a, B: PatternBackend> ZeroMapKV<'a> for Pattern<B>
where
    Pattern<B>: VarULE,
{
    type Container = VarZeroVec<'a, Pattern<B>>;
    type Slice = VarZeroSlice<Pattern<B>>;
    type GetType = Pattern<B>;
    type OwnedType = Box<Pattern<B>>;
}

/// Implement `VarULE` for `Pattern<SinglePlaceholder, str>`.
///
/// # Safety
///
/// Safety checklist for `ULE`:
///
/// 1. `Pattern<B>` does not include any uninitialized or padding bytes.
/// 2. `Pattern<B>` is aligned to 1 byte.
/// 3. The implementation of `validate_byte_slice()` returns an error
///    if any byte is not valid.
/// 4. The implementation of `validate_byte_slice()` returns an error
///    if the slice cannot be used to build a `Pattern<B>` in its entirety.
/// 5. The implementation of `from_byte_slice_unchecked()` returns a reference to the same data.
/// 6. `parse_byte_slice()` is equivalent to `validate_byte_slice()` followed by `from_byte_slice_unchecked()`.
/// 7. `Pattern<B>` byte equality is semantic equality.
unsafe impl<B, S: ?Sized + VarULE> VarULE for Pattern<B>
where
    B: PatternBackend<Store = S>,
{
    fn validate_byte_slice(bytes: &[u8]) -> Result<(), UleError> {
        let store = S::parse_byte_slice(bytes)?;
        B::validate_store(store).map_err(|_| UleError::parse::<Self>())
    }

    unsafe fn from_byte_slice_unchecked(bytes: &[u8]) -> &Self {
        let store = S::from_byte_slice_unchecked(bytes);
        Self::from_ref_store_unchecked(store)
    }
}