2014-06-12 17:29:24 +10:00
|
|
|
|
//! This crate provides the `AnyMap` type, a safe and convenient store for one value of each type.
|
|
|
|
|
|
2014-07-10 18:11:53 +10:00
|
|
|
|
#![crate_name = "anymap"]
|
|
|
|
|
#![crate_type = "lib"]
|
2014-06-12 22:29:59 +10:00
|
|
|
|
#![feature(default_type_params)]
|
|
|
|
|
#![warn(unnecessary_qualification, non_uppercase_statics,
|
2014-06-12 17:29:24 +10:00
|
|
|
|
variant_size_difference, managed_heap_memory, unnecessary_typecast,
|
2014-06-15 07:08:21 +10:00
|
|
|
|
missing_doc, unused_result)]
|
2014-06-12 17:29:24 +10:00
|
|
|
|
|
2014-06-12 22:25:17 +10:00
|
|
|
|
#[cfg(test)]
|
|
|
|
|
extern crate test;
|
|
|
|
|
|
2014-06-12 22:59:03 +10:00
|
|
|
|
use std::any::Any;
|
2014-06-12 17:29:24 +10:00
|
|
|
|
use std::intrinsics::TypeId;
|
2014-06-23 19:23:54 +10:00
|
|
|
|
use std::collections::{Collection, HashMap, Mutable};
|
2014-06-12 22:29:59 +10:00
|
|
|
|
use std::hash::{Hash, Hasher, Writer};
|
2014-06-12 22:59:03 +10:00
|
|
|
|
use std::mem::{transmute, transmute_copy};
|
|
|
|
|
use std::raw::TraitObject;
|
2014-06-12 22:29:59 +10:00
|
|
|
|
|
|
|
|
|
struct TypeIdHasher;
|
|
|
|
|
|
|
|
|
|
struct TypeIdState {
|
|
|
|
|
value: u64,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Writer for TypeIdState {
|
|
|
|
|
#[inline(always)]
|
|
|
|
|
fn write(&mut self, bytes: &[u8]) {
|
|
|
|
|
// This expects to receive one and exactly one 64-bit value
|
|
|
|
|
debug_assert!(bytes.len() == 8);
|
|
|
|
|
unsafe {
|
|
|
|
|
std::ptr::copy_nonoverlapping_memory(&mut self.value,
|
2014-06-12 22:59:03 +10:00
|
|
|
|
transmute(&bytes[0]),
|
2014-06-12 22:29:59 +10:00
|
|
|
|
1)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Hasher<TypeIdState> for TypeIdHasher {
|
|
|
|
|
fn hash<T: Hash<TypeIdState>>(&self, value: &T) -> u64 {
|
|
|
|
|
let mut state = TypeIdState {
|
|
|
|
|
value: 0,
|
|
|
|
|
};
|
|
|
|
|
value.hash(&mut state);
|
|
|
|
|
state.value
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-06-12 17:29:24 +10:00
|
|
|
|
|
2014-06-12 22:59:03 +10:00
|
|
|
|
/// An extension of `AnyRefExt` allowing unchecked downcasting of trait objects to `&T`.
|
|
|
|
|
trait UncheckedAnyRefExt<'a> {
|
|
|
|
|
/// Returns a reference to the boxed value, assuming that it is of type `T`. This should only be
|
|
|
|
|
/// called if you are ABSOLUTELY CERTAIN of `T` as you will get really wacky output if it’s not.
|
|
|
|
|
unsafe fn as_ref_unchecked<T: 'static>(self) -> &'a T;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'a> UncheckedAnyRefExt<'a> for &'a Any {
|
|
|
|
|
#[inline]
|
|
|
|
|
unsafe fn as_ref_unchecked<T: 'static>(self) -> &'a T {
|
|
|
|
|
// Get the raw representation of the trait object
|
|
|
|
|
let to: TraitObject = transmute_copy(&self);
|
|
|
|
|
|
|
|
|
|
// Extract the data pointer
|
|
|
|
|
transmute(to.data)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// An extension of `AnyMutRefExt` allowing unchecked downcasting of trait objects to `&mut T`.
|
|
|
|
|
trait UncheckedAnyMutRefExt<'a> {
|
|
|
|
|
/// Returns a reference to the boxed value, assuming that it is of type `T`. This should only be
|
|
|
|
|
/// called if you are ABSOLUTELY CERTAIN of `T` as you will get really wacky output if it’s not.
|
|
|
|
|
unsafe fn as_mut_unchecked<T: 'static>(self) -> &'a mut T;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'a> UncheckedAnyMutRefExt<'a> for &'a mut Any {
|
|
|
|
|
#[inline]
|
|
|
|
|
unsafe fn as_mut_unchecked<T: 'static>(self) -> &'a mut T {
|
|
|
|
|
// Get the raw representation of the trait object
|
|
|
|
|
let to: TraitObject = transmute_copy(&self);
|
|
|
|
|
|
|
|
|
|
// Extract the data pointer
|
|
|
|
|
transmute(to.data)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-12 17:29:24 +10:00
|
|
|
|
/// A map containing zero or one values for any given type and allowing convenient,
|
|
|
|
|
/// type-safe access to those values.
|
|
|
|
|
///
|
|
|
|
|
/// ```rust
|
|
|
|
|
/// # use anymap::AnyMap;
|
|
|
|
|
/// let mut data = AnyMap::new();
|
|
|
|
|
/// assert_eq!(data.find(), None::<&int>);
|
|
|
|
|
/// data.insert(42i);
|
|
|
|
|
/// assert_eq!(data.find(), Some(&42i));
|
|
|
|
|
/// data.remove::<int>();
|
|
|
|
|
/// assert_eq!(data.find::<int>(), None);
|
|
|
|
|
///
|
|
|
|
|
/// #[deriving(PartialEq, Show)]
|
|
|
|
|
/// struct Foo {
|
|
|
|
|
/// str: String,
|
|
|
|
|
/// }
|
|
|
|
|
///
|
|
|
|
|
/// assert_eq!(data.find::<Foo>(), None);
|
|
|
|
|
/// data.insert(Foo { str: "foo".to_string() });
|
|
|
|
|
/// assert_eq!(data.find(), Some(&Foo { str: "foo".to_string() }));
|
|
|
|
|
/// data.find_mut::<Foo>().map(|foo| foo.str.push_char('t'));
|
|
|
|
|
/// assert_eq!(data.find::<Foo>().unwrap().str.as_slice(), "foot");
|
|
|
|
|
/// ```
|
|
|
|
|
///
|
|
|
|
|
/// Values containing non-static references are not permitted.
|
|
|
|
|
pub struct AnyMap {
|
2014-06-17 02:59:53 +10:00
|
|
|
|
data: HashMap<TypeId, Box<Any>, TypeIdHasher>,
|
2014-06-12 17:29:24 +10:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl AnyMap {
|
|
|
|
|
/// Construct a new `AnyMap`.
|
|
|
|
|
pub fn new() -> AnyMap {
|
|
|
|
|
AnyMap {
|
2014-06-12 22:29:59 +10:00
|
|
|
|
data: HashMap::with_hasher(TypeIdHasher),
|
2014-06-12 17:29:24 +10:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl AnyMap {
|
|
|
|
|
/// Retrieve the value stored in the map for the type `T`, if it exists.
|
|
|
|
|
pub fn find<'a, T: 'static>(&'a self) -> Option<&'a T> {
|
2014-06-12 22:59:03 +10:00
|
|
|
|
self.data.find(&TypeId::of::<T>()).map(|any| unsafe { any.as_ref_unchecked::<T>() })
|
2014-06-12 17:29:24 +10:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Retrieve a mutable reference to the value stored in the map for the type `T`, if it exists.
|
|
|
|
|
pub fn find_mut<'a, T: 'static>(&'a mut self) -> Option<&'a mut T> {
|
2014-06-12 22:59:03 +10:00
|
|
|
|
self.data.find_mut(&TypeId::of::<T>()).map(|any| unsafe { any.as_mut_unchecked::<T>() })
|
2014-06-12 17:29:24 +10:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Set the value contained in the map for the type `T`.
|
|
|
|
|
/// This will override any previous value stored.
|
|
|
|
|
pub fn insert<T: 'static>(&mut self, value: T) {
|
2014-06-15 08:27:06 +10:00
|
|
|
|
self.data.insert(TypeId::of::<T>(), box value as Box<Any>);
|
2014-06-12 17:29:24 +10:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Remove the value for the type `T` if it existed.
|
|
|
|
|
pub fn remove<T: 'static>(&mut self) {
|
|
|
|
|
self.data.remove(&TypeId::of::<T>());
|
|
|
|
|
}
|
2014-08-14 14:01:19 +10:00
|
|
|
|
|
|
|
|
|
/// Does a value of type `T` exist?
|
|
|
|
|
pub fn contains<T: 'static>(&self) -> bool {
|
|
|
|
|
self.data.contains_key(&TypeId::of::<T>())
|
|
|
|
|
}
|
2014-06-12 17:29:24 +10:00
|
|
|
|
}
|
2014-06-12 22:25:17 +10:00
|
|
|
|
|
2014-06-23 19:23:54 +10:00
|
|
|
|
impl Collection for AnyMap {
|
|
|
|
|
fn len(&self) -> uint {
|
|
|
|
|
self.data.len()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn is_empty(&self) -> bool {
|
|
|
|
|
self.data.is_empty()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Mutable for AnyMap {
|
|
|
|
|
fn clear(&mut self) {
|
|
|
|
|
self.data.clear();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-12 22:25:17 +10:00
|
|
|
|
#[bench]
|
|
|
|
|
fn bench_insertion(b: &mut ::test::Bencher) {
|
|
|
|
|
b.iter(|| {
|
|
|
|
|
let mut data = AnyMap::new();
|
2014-07-19 19:32:17 +10:00
|
|
|
|
for _ in range(0u, 100) {
|
2014-06-12 23:05:02 +10:00
|
|
|
|
data.insert(42i);
|
|
|
|
|
}
|
2014-06-12 22:25:17 +10:00
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
|
fn bench_find_missing(b: &mut ::test::Bencher) {
|
|
|
|
|
b.iter(|| {
|
|
|
|
|
let data = AnyMap::new();
|
2014-07-19 19:32:17 +10:00
|
|
|
|
for _ in range(0u, 100) {
|
2014-06-12 23:05:02 +10:00
|
|
|
|
assert_eq!(data.find(), None::<&int>);
|
|
|
|
|
}
|
2014-06-12 22:25:17 +10:00
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
|
fn bench_find_present(b: &mut ::test::Bencher) {
|
|
|
|
|
b.iter(|| {
|
|
|
|
|
let mut data = AnyMap::new();
|
|
|
|
|
data.insert(42i);
|
2014-06-12 23:05:02 +10:00
|
|
|
|
// These inner loops are a feeble attempt to drown the other factors.
|
2014-07-19 19:32:17 +10:00
|
|
|
|
for _ in range(0u, 100) {
|
2014-06-12 23:05:02 +10:00
|
|
|
|
assert_eq!(data.find(), Some(&42i));
|
|
|
|
|
}
|
2014-06-12 22:25:17 +10:00
|
|
|
|
})
|
|
|
|
|
}
|