Debian Codegraph @

third_party/rust/litemap/examples/litemap_bincode.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 // 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 #[allow(dead_code)]
29 const BINCODE: [u8; 278] = [
30 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,
31 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,
32 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,
33 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,
34 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,
35 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,
36 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,
37 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,
38 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,
39 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,
40 0, 0, 0, 0, 0, 84, 117, 114, 107, 105, 115, 104,
41 ];
42
43 /// Run this function to print new data to the console.
44 #[allow(dead_code)]
45 fn generate() {
46 let mut map = LiteMap::new_vec();
47 for (lang, name) in DATA.iter() {
48 map.try_append(lang, name).ok_or(()).unwrap_err();
49 }
50
51 let buf = bincode::serialize(&map).unwrap();
52 println!("{buf:?}");
53 }
54
55 #[no_mangle]
56 fn main(_argc: isize, _argv: *const *const u8) -> isize {
57 icu_benchmark_macros::main_setup!();
58
59 // Uncomment the following line to re-generate the binary data.
60 // generate();
61
62 let map: LiteMap<&str, &str> = bincode::deserialize(&BINCODE).unwrap();
63 assert_eq!(map.get("tr"), Some(&"Turkish"));
64
65 0
66 }