| 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::{Pattern, PatternBackend}; |
| 6 |
|
| 7 |
use alloc::boxed::Box; |
| 8 |
use zerovec::{ |
| 9 |
VarZeroSlice, VarZeroVec, |
| 10 |
maps::ZeroMapKV, |
| 11 |
ule::{UleError, VarULE}, |
| 12 |
}; |
| 13 |
|
| 14 |
impl<'a, B: PatternBackend> ZeroMapKV<'a> for Pattern<B> |
| 15 |
where |
| 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. |
| 39 |
unsafe impl<B, S: ?Sized + VarULE> VarULE for Pattern<B> |
| 40 |
where |
| 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 |
// Safety: validate_bytes succeeded, so S::parse_bytes succeeded, |
| 50 |
// so S::validate_bytes succeeded, so S::from_bytes_unchecked is safe. |
| 51 |
let store = unsafe { S::from_bytes_unchecked(bytes) }; |
| 52 |
Self::from_ref_store_unchecked(store) |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
#[cfg(test)] |
| 57 |
mod tests { |
| 58 |
use super::*; |
| 59 |
use crate::SinglePlaceholderPattern; |
| 60 |
use litemap::LiteMap; |
| 61 |
use zerovec::ZeroMap; |
| 62 |
|
| 63 |
#[test] |
| 64 |
fn test_zeromap() { |
| 65 |
let pattern = |
| 66 |
SinglePlaceholderPattern::try_from_str("Hello, {0}!", Default::default()).unwrap(); |
| 67 |
let mut litemap = LiteMap::<u32, Box<SinglePlaceholderPattern>>::new_vec(); |
| 68 |
litemap.insert(0, pattern.clone()); |
| 69 |
let zeromap = ZeroMap::<u32, SinglePlaceholderPattern>::from_iter(litemap); |
| 70 |
let recovered_pattern = zeromap.get(&0).unwrap(); |
| 71 |
assert_eq!(&*pattern, recovered_pattern); |
| 72 |
} |
| 73 |
} |