Skip to content

Commit 5774ab6

Browse files
authored
Add databake impl for LiteMap (#4275)
1 parent 9cbe971 commit 5774ab6

File tree

6 files changed

+103
-0
lines changed

6 files changed

+103
-0
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
- `databake`
3232
- Add implementations for `BTreeSet`, `BTreeMap` (https://github.com/unicode-org/icu4x/pull/4268, https://github.com/unicode-org/icu4x/pull/4274)
3333
- Improvements to `databake::test_bake!()` (https://github.com/unicode-org/icu4x/pull/4182)
34+
- `litemap`
35+
- Implement `databake::Bake` on `LiteMap` (https://github.com/unicode-org/icu4x/pull/4275)
3436
- `tinystr`
3537
- Better Debug impl for UnvalidatedTinyAsciiStr (https://github.com/unicode-org/icu4x/pull/4189)
3638
- `zerovec`

Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

utils/litemap/Cargo.toml

+4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ independent = true
2323
all-features = true
2424

2525
[dependencies]
26+
databake = { workspace = true, default-features = false, optional = true }
2627
serde = {version = "1", optional = true, default-features = false, features = ["alloc"]}
2728
yoke = { workspace = true, features = ["derive"], optional = true }
2829

@@ -43,6 +44,9 @@ criterion = "0.4"
4344
bench = ["serde"]
4445
default = ["alloc"]
4546
alloc = []
47+
databake = ["dep:databake"]
48+
serde = ["dep:serde"]
49+
yoke = ["dep:yoke"]
4650

4751
# Enables the `testing` module with tools for testing custom stores.
4852
testing = ["alloc"]

utils/litemap/README.md

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

utils/litemap/src/databake.rs

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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+
}

utils/litemap/src/lib.rs

+10
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@
2121
//! random-access data store, giving that data store a map-like interface. See the [`store`]
2222
//! module for more details.
2323
//!
24+
//! ## Const construction
25+
//!
26+
//! [`LiteMap`] supports const construction from any store that is const-constructible, such as a
27+
//! static slice, via [`LiteMap::from_sorted_store_unchecked()`]. This also makes [`LiteMap`]
28+
//! suitable for use with [`databake`]. See [`impl Bake for LiteMap`] for more details.
29+
//!
30+
//! [`impl Bake for LiteMap`]: ./struct.LiteMap.html#impl-Bake-for-LiteMap<K,+V,+S>
2431
//! [`Vec`]: alloc::vec::Vec
2532
2633
// https://github.com/unicode-org/icu4x/blob/main/docs/process/boilerplate.md#library-annotations
@@ -44,6 +51,9 @@ extern crate std;
4451

4552
extern crate alloc;
4653

54+
#[cfg(feature = "databake")]
55+
#[path = "databake.rs"] // to not conflict with `databake` as used in the docs
56+
mod databake_impls;
4757
mod map;
4858
#[cfg(feature = "serde")]
4959
mod serde;

0 commit comments

Comments
 (0)