Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add databake impl for LiteMap #4275

Merged
merged 9 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
- `databake`
- Add implementations for `BTreeSet`, `BTreeMap` (https://github.com/unicode-org/icu4x/pull/4268, https://github.com/unicode-org/icu4x/pull/4274)
- Improvements to `databake::test_bake!()` (https://github.com/unicode-org/icu4x/pull/4182)
- `litemap`
- Implement `databake::Bake` on `LiteMap` (https://github.com/unicode-org/icu4x/pull/4275)
- `tinystr`
- Better Debug impl for UnvalidatedTinyAsciiStr (https://github.com/unicode-org/icu4x/pull/4189)
- `zerovec`
Expand Down
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions utils/litemap/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ independent = true
all-features = true

[dependencies]
databake = { workspace = true, default-features = false, optional = true }
robertbastian marked this conversation as resolved.
Show resolved Hide resolved
serde = {version = "1", optional = true, default-features = false, features = ["alloc"]}
yoke = { workspace = true, features = ["derive"], optional = true }

Expand All @@ -43,6 +44,9 @@ criterion = "0.4"
bench = ["serde"]
default = ["alloc"]
alloc = []
databake = ["dep:databake"]
serde = ["dep:serde"]
yoke = ["dep:yoke"]

# Enables the `testing` module with tools for testing custom stores.
testing = ["alloc"]
Expand Down
7 changes: 7 additions & 0 deletions utils/litemap/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

79 changes: 79 additions & 0 deletions utils/litemap/src/databake.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// This file is part of ICU4X. For terms of use, please see the file
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

use crate::LiteMap;
use databake::*;

/// Bakes a LiteMap into Rust code for fast runtime construction from data. Use this impl during
/// code generation, such as in a `build.rs` script.
///
/// For the most efficient bake, bake the [`LiteMap`] with a slice store. Use functions such as
/// the following for converting an allocated [`LiteMap`] to a borrowing [`LiteMap`]:
///
/// - [`LiteMap::to_borrowed_keys()`]
/// - [`LiteMap::to_borrowed_values()`]
/// - [`LiteMap::to_borrowed_keys_values()`]
/// - [`LiteMap::as_sliced()`]
///
/// # Examples
///
/// ```
/// use databake::*;
/// use litemap::LiteMap;
///
/// // Construct the LiteMap fully owned and allocated:
/// let mut litemap_alloc: LiteMap<usize, String, Vec<_>> = LiteMap::new_vec();
/// litemap_alloc.insert(1usize, "one".to_string());
/// litemap_alloc.insert(2usize, "two".to_string());
/// litemap_alloc.insert(10usize, "ten".to_string());
///
/// // Convert to a borrowed type for baking:
/// let litemap_str: LiteMap<usize, &str, Vec<_>> = litemap_alloc.to_borrowed_values();
/// let litemap_slice: LiteMap<usize, &str, &[_]> = litemap_str.as_sliced();
///
/// // The bake will now work for const construction:
/// let mut ctx = Default::default();
/// println!(
/// "const FOO: LiteMap<usize, &str, &[(usize, &str)]> = {};",
/// litemap_slice.bake(&mut ctx)
/// );
/// ```
impl<K, V, S> Bake for LiteMap<K, V, S>
where
S: Bake,
{
fn bake(&self, env: &CrateEnv) -> TokenStream {
env.insert("litemap");
let store = self.values.bake(env);
quote! { litemap::LiteMap::from_sorted_store_unchecked(#store) }
}
}

#[test]
fn test_baked_map() {
// Const construction:
test_bake!(
LiteMap<usize, &str, &[(usize, &str)]>,
const: crate::LiteMap::from_sorted_store_unchecked(
&[
(1usize, "one"),
(2usize, "two"),
(10usize, "ten")
]
),
litemap
);
// Non-const construction:
test_bake!(
LiteMap<usize, String, Vec<(usize, String)>>,
crate::LiteMap::from_sorted_store_unchecked(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am trying to figure out something similar for std::collections::HashMap -- how to keep a stable order so the unit tests actually pass reliably!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can sort quoted tokens in the bake implementation. This won't be a meaningful sort, but it will give you stability.

alloc::vec![
(1usize, "one".to_owned()),
(2usize, "two".to_owned()),
(10usize, "ten".to_owned()),
]
),
litemap
);
}
10 changes: 10 additions & 0 deletions utils/litemap/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@
//! random-access data store, giving that data store a map-like interface. See the [`store`]
//! module for more details.
//!
//! ## Const construction
//!
//! [`LiteMap`] supports const construction from any store that is const-constructible, such as a
//! static slice, via [`LiteMap::from_sorted_store_unchecked()`]. This also makes [`LiteMap`]
//! suitable for use with [`databake`]. See [`impl Bake for LiteMap`] for more details.
//!
//! [`impl Bake for LiteMap`]: ./struct.LiteMap.html#impl-Bake-for-LiteMap<K,+V,+S>
//! [`Vec`]: alloc::vec::Vec

// https://github.com/unicode-org/icu4x/blob/main/docs/process/boilerplate.md#library-annotations
Expand All @@ -44,6 +51,9 @@ extern crate std;

extern crate alloc;

#[cfg(feature = "databake")]
#[path = "databake.rs"] // to not conflict with `databake` as used in the docs
mod databake_impls;
mod map;
#[cfg(feature = "serde")]
mod serde;
Expand Down