-
Notifications
You must be signed in to change notification settings - Fork 184
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
Conversation
// Non-const construction: | ||
test_bake!( | ||
LiteMap<usize, String, Vec<(usize, String)>>, | ||
crate::LiteMap::from_sorted_store_unchecked( |
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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.
🤔 |
utils/litemap/src/databake.rs
Outdated
use databake::*; | ||
|
||
/// Bakes a LiteMap into Rust code for fast runtime construction from data. Use this impl during | ||
/// code generation or in a `build.rs` script. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
build.rs is one mechanism to run code generation.
/// code generation or in a `build.rs` script. | |
/// code generation. |
utils/litemap/src/databake.rs
Outdated
/// use litemap::LiteMap; | ||
/// | ||
/// // Construct the LiteMap fully owned and allocated: | ||
/// let mut litemap_alloc = LiteMap::new_vec(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
explicit types?
/// let mut litemap_alloc = LiteMap::new_vec(); | |
/// let mut litemap_alloc: LiteMap<usize, String, Vec<(usize, String>)> = LiteMap::new_vec(); |
utils/litemap/src/databake.rs
Outdated
/// litemap_alloc.insert(10usize, "ten".to_string()); | ||
/// | ||
/// // Convert to a borrowed type for baking: | ||
/// let litemap_str: LiteMap<usize, &str> = litemap_alloc.to_borrowed_values(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// let litemap_str: LiteMap<usize, &str> = litemap_alloc.to_borrowed_values(); | |
/// let litemap_str: LiteMap<usize, &str, Vec<(usize, &str)>> = litemap_alloc.to_borrowed_values(); |
utils/litemap/src/databake.rs
Outdated
/// | ||
/// // Convert to a borrowed type for baking: | ||
/// let litemap_str: LiteMap<usize, &str> = litemap_alloc.to_borrowed_values(); | ||
/// let litemap_slice = litemap_str.as_sliced(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// let litemap_slice = litemap_str.as_sliced(); | |
/// let litemap_slice: LiteMap<usize, &str, &[(usize, &str)]> = litemap_str.as_sliced(); |
utils/litemap/src/databake.rs
Outdated
/// // The bake will now work for const construction: | ||
/// assert_eq!( | ||
/// litemap_slice.bake(&Default::default()).to_string(), | ||
/// r#"litemap :: LiteMap :: from_sorted_store_unchecked (& [(1usize , "one") , (2usize , "two") , (10usize , "ten")])"#, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please don't assert the literal representation. use test_bake
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@robertbastian: or something like
format!("const FOO: ... = {}", bar.bake(...))
See #4266
I thought we already had this impl but it seems to be missing!
@adamchalmers