|
| 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::LiteMap; |
| 6 | +use databake::*; |
| 7 | + |
| 8 | +/// Bakes a LiteMap into Rust code for fast runtime construction from data. Use this impl during |
| 9 | +/// code generation, such as in a `build.rs` script. |
| 10 | +/// |
| 11 | +/// For the most efficient bake, bake the [`LiteMap`] with a slice store. Use functions such as |
| 12 | +/// the following for converting an allocated [`LiteMap`] to a borrowing [`LiteMap`]: |
| 13 | +/// |
| 14 | +/// - [`LiteMap::to_borrowed_keys()`] |
| 15 | +/// - [`LiteMap::to_borrowed_values()`] |
| 16 | +/// - [`LiteMap::to_borrowed_keys_values()`] |
| 17 | +/// - [`LiteMap::as_sliced()`] |
| 18 | +/// |
| 19 | +/// # Examples |
| 20 | +/// |
| 21 | +/// ``` |
| 22 | +/// use databake::*; |
| 23 | +/// use litemap::LiteMap; |
| 24 | +/// |
| 25 | +/// // Construct the LiteMap fully owned and allocated: |
| 26 | +/// let mut litemap_alloc: LiteMap<usize, String, Vec<_>> = LiteMap::new_vec(); |
| 27 | +/// litemap_alloc.insert(1usize, "one".to_string()); |
| 28 | +/// litemap_alloc.insert(2usize, "two".to_string()); |
| 29 | +/// litemap_alloc.insert(10usize, "ten".to_string()); |
| 30 | +/// |
| 31 | +/// // Convert to a borrowed type for baking: |
| 32 | +/// let litemap_str: LiteMap<usize, &str, Vec<_>> = litemap_alloc.to_borrowed_values(); |
| 33 | +/// let litemap_slice: LiteMap<usize, &str, &[_]> = litemap_str.as_sliced(); |
| 34 | +/// |
| 35 | +/// // The bake will now work for const construction: |
| 36 | +/// let mut ctx = Default::default(); |
| 37 | +/// println!( |
| 38 | +/// "const FOO: LiteMap<usize, &str, &[(usize, &str)]> = {};", |
| 39 | +/// litemap_slice.bake(&mut ctx) |
| 40 | +/// ); |
| 41 | +/// ``` |
| 42 | +impl<K, V, S> Bake for LiteMap<K, V, S> |
| 43 | +where |
| 44 | + S: Bake, |
| 45 | +{ |
| 46 | + fn bake(&self, env: &CrateEnv) -> TokenStream { |
| 47 | + env.insert("litemap"); |
| 48 | + let store = self.values.bake(env); |
| 49 | + quote! { litemap::LiteMap::from_sorted_store_unchecked(#store) } |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +#[test] |
| 54 | +fn test_baked_map() { |
| 55 | + // Const construction: |
| 56 | + test_bake!( |
| 57 | + LiteMap<usize, &str, &[(usize, &str)]>, |
| 58 | + const: crate::LiteMap::from_sorted_store_unchecked( |
| 59 | + &[ |
| 60 | + (1usize, "one"), |
| 61 | + (2usize, "two"), |
| 62 | + (10usize, "ten") |
| 63 | + ] |
| 64 | + ), |
| 65 | + litemap |
| 66 | + ); |
| 67 | + // Non-const construction: |
| 68 | + test_bake!( |
| 69 | + LiteMap<usize, String, Vec<(usize, String)>>, |
| 70 | + crate::LiteMap::from_sorted_store_unchecked( |
| 71 | + alloc::vec![ |
| 72 | + (1usize, "one".to_owned()), |
| 73 | + (2usize, "two".to_owned()), |
| 74 | + (10usize, "ten".to_owned()), |
| 75 | + ] |
| 76 | + ), |
| 77 | + litemap |
| 78 | + ); |
| 79 | +} |
0 commit comments