| 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 |
|
| 10 |
icu_benchmark_macros::static_setup!(); |
| 11 |
|
| 12 |
use litemap::LiteMap; |
| 13 |
|
| 14 |
const DATA: [(&str, &str); 11] = [ |
| 15 |
("ar", "Arabic"), |
| 16 |
("bn", "Bangla"), |
| 17 |
("ccp", "Chakma"), |
| 18 |
("en", "English"), |
| 19 |
("es", "Spanish"), |
| 20 |
("fr", "French"), |
| 21 |
("ja", "Japanese"), |
| 22 |
("ru", "Russian"), |
| 23 |
("sr", "Serbian"), |
| 24 |
("th", "Thai"), |
| 25 |
("tr", "Turkish"), |
| 26 |
]; |
| 27 |
|
| 28 |
const POSTCARD: [u8; 117] = [ |
| 29 |
11, 2, 97, 114, 6, 65, 114, 97, 98, 105, 99, 2, 98, 110, 6, 66, 97, 110, 103, 108, 97, 3, 99, |
| 30 |
99, 112, 6, 67, 104, 97, 107, 109, 97, 2, 101, 110, 7, 69, 110, 103, 108, 105, 115, 104, 2, |
| 31 |
101, 115, 7, 83, 112, 97, 110, 105, 115, 104, 2, 102, 114, 6, 70, 114, 101, 110, 99, 104, 2, |
| 32 |
106, 97, 8, 74, 97, 112, 97, 110, 101, 115, 101, 2, 114, 117, 7, 82, 117, 115, 115, 105, 97, |
| 33 |
110, 2, 115, 114, 7, 83, 101, 114, 98, 105, 97, 110, 2, 116, 104, 4, 84, 104, 97, 105, 2, 116, |
| 34 |
114, 7, 84, 117, 114, 107, 105, 115, 104, |
| 35 |
]; |
| 36 |
|
| 37 |
/// Run this function to print new data to the console. |
| 38 |
#[allow(dead_code)] |
| 39 |
fn generate() { |
| 40 |
let mut map = LiteMap::new_vec(); |
| 41 |
for (lang, name) in DATA.iter() { |
| 42 |
map.try_append(lang, name).ok_or(()).unwrap_err(); |
| 43 |
} |
| 44 |
|
| 45 |
let buf = postcard::to_stdvec(&map).unwrap(); |
| 46 |
println!("{buf:?}"); |
| 47 |
} |
| 48 |
|
| 49 |
#[no_mangle] |
| 50 |
fn main(_argc: isize, _argv: *const *const u8) -> isize { |
| 51 |
icu_benchmark_macros::main_setup!(); |
| 52 |
|
| 53 |
// Uncomment the following line to re-generate the binary data. |
| 54 |
// generate(); |
| 55 |
|
| 56 |
let map: LiteMap<&str, &str> = postcard::from_bytes(&POSTCARD).unwrap(); |
| 57 |
assert_eq!(map.get("tr"), Some(&"Turkish")); |
| 58 |
|
| 59 |
0 |
| 60 |
} |