Debian Codegraph @

debian/missing-sources/zerotrie/src/builder/litemap.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 //! Impls for functions gated on the "litemap" feature.
6
7 use super::konst::*;
8 use crate::builder::bytestr::ByteStr;
9 use crate::error::ZeroTrieBuildError;
10 use crate::zerotrie::ZeroTrieSimpleAscii;
11 use crate::ZeroTrie;
12 use alloc::borrow::Borrow;
13 use alloc::vec::Vec;
14 use litemap::LiteMap;
15
16 impl ZeroTrieSimpleAscii<Vec<u8>> {
17 #[doc(hidden)]
18 pub fn try_from_litemap_with_const_builder<'a, S>(
19 items: &LiteMap<&'a [u8], usize, S>,
20 ) -> Result<Self, ZeroTrieBuildError>
21 where
22 S: litemap::store::StoreSlice<&'a [u8], usize, Slice = [(&'a [u8], usize)]>,
23 {
24 let tuples = items.as_slice();
25 let byte_str_slice = ByteStr::from_byte_slice_with_value(tuples);
26 ZeroTrieBuilderConst::<10000>::from_sorted_const_tuple_slice::<100>(byte_str_slice.into())
27 .map(|s| Self {
28 store: s.as_bytes().to_vec(),
29 })
30 }
31 }
32
33 impl<K, S> TryFrom<&LiteMap<K, usize, S>> for ZeroTrie<Vec<u8>>
34 where
35 // Borrow, not AsRef, because we rely on Ord being the same. Unfortunately
36 // this means `LiteMap<&str, usize>` does not work.
37 K: Borrow<[u8]>,
38 S: litemap::store::StoreSlice<K, usize, Slice = [(K, usize)]>,
39 {
40 type Error = ZeroTrieBuildError;
41 fn try_from(items: &LiteMap<K, usize, S>) -> Result<Self, ZeroTrieBuildError> {
42 let byte_litemap = items.to_borrowed_keys::<[u8], Vec<_>>();
43 let byte_slice = byte_litemap.as_slice();
44 let byte_str_slice = ByteStr::from_byte_slice_with_value(byte_slice);
45 Self::try_from_tuple_slice(byte_str_slice)
46 }
47 }
48
49 // TODO(#7084): Make this more infallible by calculating the required length,
50 // heap-allocating the required capacity, and pointing ConstAsciiTrieBuilderStore
51 // to the heap buffer.