Chris Morgan
›
Git
›
anymap
› blobdiff
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
commit
grep
author
committer
pickaxe
?
search:
re
Merge pull request #14 from drbawb/feature/namespaced-enums
[anymap]
/
src
/
lib.rs
diff --git
a/src/lib.rs
b/src/lib.rs
index 043578f158cde1cf059048033f7efcc737bdab72..0e9282219014f2bbe0f2584e624b5ed0ed922359 100644
(file)
--- a/
src/lib.rs
+++ b/
src/lib.rs
@@
-13,11
+13,9
@@
use std::intrinsics::{forget, TypeId};
use std::collections::HashMap;
use std::collections::hash_map;
use std::hash::{Hash, Hasher, Writer};
use std::collections::HashMap;
use std::collections::hash_map;
use std::hash::{Hash, Hasher, Writer};
-use std::mem::
{transmute, transmute_copy}
;
+use std::mem::
transmute
;
use std::raw::TraitObject;
use std::raw::TraitObject;
-pub use Entry::{Vacant, Occupied};
-
struct TypeIdHasher;
struct TypeIdState {
struct TypeIdHasher;
struct TypeIdState {
@@
-54,11
+52,11
@@
trait UncheckedAnyRefExt<'a> {
unsafe fn downcast_ref_unchecked<T: 'static>(self) -> &'a T;
}
unsafe fn downcast_ref_unchecked<T: 'static>(self) -> &'a T;
}
-impl<'a> UncheckedAnyRefExt<'a> for &'a
(Any + 'a)
{
+impl<'a> UncheckedAnyRefExt<'a> for &'a
Any
{
#[inline]
unsafe fn downcast_ref_unchecked<T: 'static>(self) -> &'a T {
// Get the raw representation of the trait object
#[inline]
unsafe fn downcast_ref_unchecked<T: 'static>(self) -> &'a T {
// Get the raw representation of the trait object
- let to: TraitObject = transmute
_copy(&
self);
+ let to: TraitObject = transmute
(
self);
// Extract the data pointer
transmute(to.data)
// Extract the data pointer
transmute(to.data)
@@
-72,11
+70,11
@@
trait UncheckedAnyMutRefExt<'a> {
unsafe fn downcast_mut_unchecked<T: 'static>(self) -> &'a mut T;
}
unsafe fn downcast_mut_unchecked<T: 'static>(self) -> &'a mut T;
}
-impl<'a> UncheckedAnyMutRefExt<'a> for &'a mut
(Any + 'a)
{
+impl<'a> UncheckedAnyMutRefExt<'a> for &'a mut
Any
{
#[inline]
unsafe fn downcast_mut_unchecked<T: 'static>(self) -> &'a mut T {
// Get the raw representation of the trait object
#[inline]
unsafe fn downcast_mut_unchecked<T: 'static>(self) -> &'a mut T {
// Get the raw representation of the trait object
- let to: TraitObject = transmute
_copy(&
self);
+ let to: TraitObject = transmute
(
self);
// Extract the data pointer
transmute(to.data)
// Extract the data pointer
transmute(to.data)
@@
-188,8
+186,8
@@
impl AnyMap {
/// Gets the given key's corresponding entry in the map for in-place manipulation
pub fn entry<T: Any + 'static>(&mut self) -> Entry<T> {
match self.data.entry(TypeId::of::<T>()) {
/// Gets the given key's corresponding entry in the map for in-place manipulation
pub fn entry<T: Any + 'static>(&mut self) -> Entry<T> {
match self.data.entry(TypeId::of::<T>()) {
- hash_map::
Occupied(e) =>
Occupied(OccupiedEntry { entry: e }),
- hash_map::
Vacant(e) =>
Vacant(VacantEntry { entry: e }),
+ hash_map::
Entry::Occupied(e) => Entry::
Occupied(OccupiedEntry { entry: e }),
+ hash_map::
Entry::Vacant(e) => Entry::
Vacant(VacantEntry { entry: e }),
}
}
}
}
@@
-209,12
+207,12
@@
impl AnyMap {
}
}
}
}
-/// A view into a single occupied location in a
Hash
Map
+/// A view into a single occupied location in a
n Any
Map
pub struct OccupiedEntry<'a, V: 'a> {
entry: hash_map::OccupiedEntry<'a, TypeId, Box<Any + 'static>>,
}
pub struct OccupiedEntry<'a, V: 'a> {
entry: hash_map::OccupiedEntry<'a, TypeId, Box<Any + 'static>>,
}
-/// A view into a single empty location in a
Hash
Map
+/// A view into a single empty location in a
n Any
Map
pub struct VacantEntry<'a, V: 'a> {
entry: hash_map::VacantEntry<'a, TypeId, Box<Any + 'static>>,
}
pub struct VacantEntry<'a, V: 'a> {
entry: hash_map::VacantEntry<'a, TypeId, Box<Any + 'static>>,
}
@@
-315,8
+313,8
@@
fn test_entry() {
// Existing key (insert)
match map.entry::<A>() {
// Existing key (insert)
match map.entry::<A>() {
- Vacant(_) => unreachable!(),
- Occupied(mut view) => {
+
Entry::
Vacant(_) => unreachable!(),
+
Entry::
Occupied(mut view) => {
assert_eq!(view.get(), &A(10));
assert_eq!(view.set(A(100)), A(10));
}
assert_eq!(view.get(), &A(10));
assert_eq!(view.set(A(100)), A(10));
}
@@
-327,8
+325,8
@@
fn test_entry() {
// Existing key (update)
match map.entry::<B>() {
// Existing key (update)
match map.entry::<B>() {
- Vacant(_) => unreachable!(),
- Occupied(mut view) => {
+
Entry::
Vacant(_) => unreachable!(),
+
Entry::
Occupied(mut view) => {
let v = view.get_mut();
let new_v = B(v.0 * 10);
*v = new_v;
let v = view.get_mut();
let new_v = B(v.0 * 10);
*v = new_v;
@@
-340,8
+338,8
@@
fn test_entry() {
// Existing key (take)
match map.entry::<C>() {
// Existing key (take)
match map.entry::<C>() {
- Vacant(_) => unreachable!(),
- Occupied(view) => {
+
Entry::
Vacant(_) => unreachable!(),
+
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)
match map.entry::<J>() {
// Inexistent key (insert)
match map.entry::<J>() {
- Occupied(_) => unreachable!(),
- Vacant(view) => {
+
Entry::
Occupied(_) => unreachable!(),
+
Entry::
Vacant(view) => {
assert_eq!(*view.set(J(1000)), J(1000));
}
}
assert_eq!(*view.set(J(1000)), J(1000));
}
}