Merge pull request #14 from drbawb/feature/namespaced-enums
Use namespaced enum variants for HashMap `Entry`s
This commit is contained in:
commit
8cc1b0c9f0
22
src/lib.rs
22
src/lib.rs
|
@ -16,8 +16,6 @@ use std::hash::{Hash, Hasher, Writer};
|
||||||
use std::mem::transmute;
|
use std::mem::transmute;
|
||||||
use std::raw::TraitObject;
|
use std::raw::TraitObject;
|
||||||
|
|
||||||
pub use Entry::{Vacant, Occupied};
|
|
||||||
|
|
||||||
struct TypeIdHasher;
|
struct TypeIdHasher;
|
||||||
|
|
||||||
struct TypeIdState {
|
struct TypeIdState {
|
||||||
|
@ -188,8 +186,8 @@ impl AnyMap {
|
||||||
/// Gets the given key's corresponding entry in the map for in-place manipulation
|
/// Gets the given key's corresponding entry in the map for in-place manipulation
|
||||||
pub fn entry<T: Any + 'static>(&mut self) -> Entry<T> {
|
pub fn entry<T: Any + 'static>(&mut self) -> Entry<T> {
|
||||||
match self.data.entry(TypeId::of::<T>()) {
|
match self.data.entry(TypeId::of::<T>()) {
|
||||||
hash_map::Occupied(e) => Occupied(OccupiedEntry { entry: e }),
|
hash_map::Entry::Occupied(e) => Entry::Occupied(OccupiedEntry { entry: e }),
|
||||||
hash_map::Vacant(e) => Vacant(VacantEntry { entry: e }),
|
hash_map::Entry::Vacant(e) => Entry::Vacant(VacantEntry { entry: e }),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -315,8 +313,8 @@ fn test_entry() {
|
||||||
|
|
||||||
// Existing key (insert)
|
// Existing key (insert)
|
||||||
match map.entry::<A>() {
|
match map.entry::<A>() {
|
||||||
Vacant(_) => unreachable!(),
|
Entry::Vacant(_) => unreachable!(),
|
||||||
Occupied(mut view) => {
|
Entry::Occupied(mut view) => {
|
||||||
assert_eq!(view.get(), &A(10));
|
assert_eq!(view.get(), &A(10));
|
||||||
assert_eq!(view.set(A(100)), A(10));
|
assert_eq!(view.set(A(100)), A(10));
|
||||||
}
|
}
|
||||||
|
@ -327,8 +325,8 @@ fn test_entry() {
|
||||||
|
|
||||||
// Existing key (update)
|
// Existing key (update)
|
||||||
match map.entry::<B>() {
|
match map.entry::<B>() {
|
||||||
Vacant(_) => unreachable!(),
|
Entry::Vacant(_) => unreachable!(),
|
||||||
Occupied(mut view) => {
|
Entry::Occupied(mut view) => {
|
||||||
let v = view.get_mut();
|
let v = view.get_mut();
|
||||||
let new_v = B(v.0 * 10);
|
let new_v = B(v.0 * 10);
|
||||||
*v = new_v;
|
*v = new_v;
|
||||||
|
@ -340,8 +338,8 @@ fn test_entry() {
|
||||||
|
|
||||||
// Existing key (take)
|
// Existing key (take)
|
||||||
match map.entry::<C>() {
|
match map.entry::<C>() {
|
||||||
Vacant(_) => unreachable!(),
|
Entry::Vacant(_) => unreachable!(),
|
||||||
Occupied(view) => {
|
Entry::Occupied(view) => {
|
||||||
assert_eq!(view.take(), C(30));
|
assert_eq!(view.take(), C(30));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -351,8 +349,8 @@ fn test_entry() {
|
||||||
|
|
||||||
// Inexistent key (insert)
|
// Inexistent key (insert)
|
||||||
match map.entry::<J>() {
|
match map.entry::<J>() {
|
||||||
Occupied(_) => unreachable!(),
|
Entry::Occupied(_) => unreachable!(),
|
||||||
Vacant(view) => {
|
Entry::Vacant(view) => {
|
||||||
assert_eq!(*view.set(J(1000)), J(1000));
|
assert_eq!(*view.set(J(1000)), J(1000));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue