-
Notifications
You must be signed in to change notification settings - Fork 168
feat: add no_std macros indexmap_with_default
and indexset_with_default
#380
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
Conversation
4dc6528
to
b304a9b
Compare
This will break type inference, because the compiler can't tell which |
src/macros.rs
Outdated
@@ -63,7 +60,8 @@ macro_rules! indexset { | |||
// Note: `stringify!($value)` is just here to consume the repetition, | |||
// but we throw away that string literal during constant evaluation. | |||
const CAP: usize = <[()]>::len(&[$({ stringify!($value); }),*]); | |||
let mut set = $crate::IndexSet::with_capacity(CAP); | |||
#[allow(unused_mut)] | |||
let mut set = $crate::IndexSet::with_capacity_with_hasher(CAP, <_>::default()); |
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 mut set = $crate::IndexSet::with_capacity_with_hasher(CAP, <_>::default()); | |
let mut set = $crate::IndexSet::with_capacity_and_hasher(CAP, <_>::default()); |
b304a9b
to
af692e2
Compare
@cuviper Really thanks for letting me know! How do you think we can modify it to fix the issue then? I don't really want this to depend on any particular hasher. Maybe the hasher should be made easy to specify somehow? |
Will something like this be acceptable? #[macro_export]
macro_rules! indexmap {
($S:ty; $($key:expr => $value:expr),*) => {{
const CAP: usize = <[()]>::len(&[$({ stringify!($key); }),*]);
#[allow(unused_mut)]
// Specify your custom S (must implement Default) as the hasher:
let mut map = $crate::IndexMap::<_, _, $S>::with_capacity_and_hasher(CAP, <$S>::default());
$(
map.insert($key, $value);
)*
map
}};
// fallback without hasher type specified
($($key:expr => $value:expr),*) => {{
use std::collections::hash_map::RandomState;
const CAP: usize = <[()]>::len(&[$({ stringify!($key); }),*]);
#[allow(unused_mut)]
let mut map = $crate::IndexMap::<_, _, RandomState>::with_capacity_and_hasher(
CAP,
RandomState::default()
);
$(
map.insert($key, $value);
)*
map
}};
} This way if |
I don't know -- this is why the macros are currently hard-coded. 😉
// this still needs *something* to drive the type inference for `S`
let map = IndexMap::from_iter([(1, 'a'), (2, 'b'), (3, 'c')]); |
If this works without any change to the existing tests, then yes, I think we can do that! |
Another solution could be this: #[cfg(feature = "std")]
let mut map = $crate::IndexMap::with_capacity(CAP);
#[cfg(not(feature = "std"))]
let mut map = $crate::IndexMap::with_capacity_and_hasher(CAP, <_>::default()); My syntax might be off, but something like that should work. It would require specifying the type explicitly when |
No, we can't just alternate it because |
Ah, yes. |
@cuviper @JayWhite2357 Alternatively maybe we can separate I do not want to define |
af692e2
to
2c601cb
Compare
indexmap
and indexset
no-stdindexmap_with_hasher
and indexset_with_hasher
25d45d4
to
c94b22b
Compare
ff6c67a
to
d1784ac
Compare
Now use cases need to look like this if we use a different hasher use ahash::AHasher;
use indexmap::indexmap_with_hasher;
fn main() {
let map = indexmap_with_hasher! {
AHasher; "a" => 1, "b" => 2,
};
assert_eq!(map["a"], 1);
assert_eq!(map["b"], 2);
assert_eq!(map.get("c"), None);
} Alternatively people annoyed about having to specify the hasher all the time can do use ahash::AHasher;
use indexmap::indexmap_with_hasher;
macro_rules! indexmap {
($($key:expr => $value:expr,)+) => { indexmap_with_hasher!{AHasher; $($key => $value),+} };
($($key:expr => $value:expr),*) => { indexmap_with_hasher!{AHasher; $($key => $value),*} };
}
fn main() {
let map = indexmap! {
"a" => 1, "b" => 2,
};
assert_eq!(map["a"], 1);
assert_eq!(map["b"], 2);
assert_eq!(map.get("c"), None);
} |
62a81c4
to
2df8725
Compare
I have tried this with both |
2df8725
to
877b133
Compare
indexmap_with_hasher
and indexset_with_hasher
indexmap_with_default
and indexset_with_default
877b133
to
6301a2a
Compare
6301a2a
to
941caf4
Compare
941caf4
to
5fd8971
Compare
@cuviper Really thanks! It's ready again haha. |
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.
Looks good, thanks!
Closes #379.
I'm adding
indexmap_with_default
andindexset_with_default
which should be no-std.Thanks @JayWhite2357 for providing a solution in Proof of SQL which we are upstreaming!