-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make HashMap extension fn like get_many_mut but for variable sizes See also rust-lang/hashbrown#332
- Loading branch information
1 parent
c214c01
commit bea84f0
Showing
8 changed files
with
86 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
use std::{ | ||
collections::HashMap, | ||
hash::{BuildHasher, Hash}, | ||
}; | ||
|
||
use itertools::Itertools; | ||
|
||
/// Workaround for <https://github.com/rust-lang/hashbrown/issues/332> | ||
#[allow(clippy::missing_safety_doc)] | ||
pub trait HashMapExt { | ||
type K; | ||
type V; | ||
|
||
unsafe fn get_many_var_unchecked_mut(&mut self, keys: &[Self::K]) -> Option<Vec<&mut Self::V>>; | ||
fn get_many_var_mut(&mut self, keys: &[Self::K]) -> Option<Vec<&mut Self::V>>; | ||
} | ||
|
||
impl<K: Eq + Hash, V, S: BuildHasher> HashMapExt for HashMap<K, V, S> { | ||
type K = K; | ||
type V = V; | ||
|
||
unsafe fn get_many_var_unchecked_mut(&mut self, keys: &[K]) -> Option<Vec<&mut V>> { | ||
let out = keys | ||
.iter() | ||
.map(|k| self.get_mut(k).map(|v| &mut *(v as &mut _ as *mut _))) | ||
.collect(); | ||
out | ||
} | ||
|
||
fn get_many_var_mut(&mut self, keys: &[K]) -> Option<Vec<&mut V>> { | ||
let unique = keys.iter().duplicates().next().is_none(); | ||
if unique { | ||
unsafe { self.get_many_var_unchecked_mut(keys) } | ||
} else { | ||
None | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters