| 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 |
// LiteMap is intended as a small and low-memory drop-in replacement for |
| 6 |
// HashMap. This example demonstrates how it works with Serde. |
| 7 |
|
| 8 |
#![no_main] // https://github.com/unicode-org/icu4x/issues/395 |
| 9 |
icu_benchmark_macros::instrument!(); |
| 10 |
|
| 11 |
use litemap::LiteMap; |
| 12 |
|
| 13 |
const DATA: [(&str, &str); 11] = [ |
| 14 |
("ar", "Arabic"), |
| 15 |
("bn", "Bangla"), |
| 16 |
("ccp", "Chakma"), |
| 17 |
("en", "English"), |
| 18 |
("es", "Spanish"), |
| 19 |
("fr", "French"), |
| 20 |
("ja", "Japanese"), |
| 21 |
("ru", "Russian"), |
| 22 |
("sr", "Serbian"), |
| 23 |
("th", "Thai"), |
| 24 |
("tr", "Turkish"), |
| 25 |
]; |
| 26 |
|
| 27 |
#[allow(dead_code)] |
| 28 |
const BINCODE: [u8; 278] = [ |
| 29 |
11, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 97, 114, 6, 0, 0, 0, 0, 0, 0, 0, 65, 114, 97, |
| 30 |
98, 105, 99, 2, 0, 0, 0, 0, 0, 0, 0, 98, 110, 6, 0, 0, 0, 0, 0, 0, 0, 66, 97, 110, 103, 108, |
| 31 |
97, 3, 0, 0, 0, 0, 0, 0, 0, 99, 99, 112, 6, 0, 0, 0, 0, 0, 0, 0, 67, 104, 97, 107, 109, 97, 2, |
| 32 |
0, 0, 0, 0, 0, 0, 0, 101, 110, 7, 0, 0, 0, 0, 0, 0, 0, 69, 110, 103, 108, 105, 115, 104, 2, 0, |
| 33 |
0, 0, 0, 0, 0, 0, 101, 115, 7, 0, 0, 0, 0, 0, 0, 0, 83, 112, 97, 110, 105, 115, 104, 2, 0, 0, |
| 34 |
0, 0, 0, 0, 0, 102, 114, 6, 0, 0, 0, 0, 0, 0, 0, 70, 114, 101, 110, 99, 104, 2, 0, 0, 0, 0, 0, |
| 35 |
0, 0, 106, 97, 8, 0, 0, 0, 0, 0, 0, 0, 74, 97, 112, 97, 110, 101, 115, 101, 2, 0, 0, 0, 0, 0, |
| 36 |
0, 0, 114, 117, 7, 0, 0, 0, 0, 0, 0, 0, 82, 117, 115, 115, 105, 97, 110, 2, 0, 0, 0, 0, 0, 0, |
| 37 |
0, 115, 114, 7, 0, 0, 0, 0, 0, 0, 0, 83, 101, 114, 98, 105, 97, 110, 2, 0, 0, 0, 0, 0, 0, 0, |
| 38 |
116, 104, 4, 0, 0, 0, 0, 0, 0, 0, 84, 104, 97, 105, 2, 0, 0, 0, 0, 0, 0, 0, 116, 114, 7, 0, 0, |
| 39 |
0, 0, 0, 0, 0, 84, 117, 114, 107, 105, 115, 104, |
| 40 |
]; |
| 41 |
|
| 42 |
/// Run this function to print new data to the console. |
| 43 |
#[allow(dead_code)] |
| 44 |
fn generate() { |
| 45 |
let mut map = LiteMap::new_vec(); |
| 46 |
for (lang, name) in DATA.iter() { |
| 47 |
map.try_append(lang, name).ok_or(()).unwrap_err(); |
| 48 |
} |
| 49 |
|
| 50 |
let buf = bincode::serialize(&map).unwrap(); |
| 51 |
println!("{buf:?}"); |
| 52 |
} |
| 53 |
|
| 54 |
fn main() { |
| 55 |
// Uncomment the following line to re-generate the binary data. |
| 56 |
// generate(); |
| 57 |
|
| 58 |
let map: LiteMap<&str, &str> = bincode::deserialize(&BINCODE).unwrap(); |
| 59 |
assert_eq!(map.get("tr"), Some(&"Turkish")); |
| 60 |
} |