0.10.3: Rust beta support
This is accomplished at a certain loss of efficiency, sadly. Add the 'nightly' feature to get things back how they were.
This commit is contained in:
parent
d04bde3509
commit
18518214c4
10
.travis.yml
10
.travis.yml
|
@ -3,11 +3,11 @@ env:
|
|||
global:
|
||||
- secure: nR+DJRUQ9v03nNZMpMu1tGKLKBAqdQsTIAr8ffdl+DUEh3b2jvQ+vLLNFLPjsloqhoOXo7cWO7qVpiE4ZOq2lNDURQjdiZGFjh/Y5+xKy2BqFdV7qQ1JoBzsMyx28tQTYz0mtBsACiCYKKb+ddNX5hpwrsjp8cS7htZktA5kbiU=
|
||||
script:
|
||||
- cargo build --verbose --features clone
|
||||
- cargo test --verbose --features clone
|
||||
- cargo build --verbose
|
||||
- cargo test --verbose
|
||||
- cargo doc --verbose
|
||||
- [[ "$(rustc --version)" =~ -dev ]] && cargo test --features nightly || ! cargo test --features nightly
|
||||
- [[ "$(rustc --version)" =~ -dev ]] && cargo test --features 'clone nightly' || ! cargo test --features 'clone nightly'
|
||||
- cargo test --features clone
|
||||
- cargo test
|
||||
- cargo doc
|
||||
after_script:
|
||||
- ln -s target/doc doc
|
||||
- curl -v http://www.rust-ci.org/artifacts/put?t=$RUSTCI_TOKEN > ./upload-docs
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
[package]
|
||||
name = "anymap"
|
||||
version = "0.10.2"
|
||||
version = "0.10.3"
|
||||
authors = ["Chris Morgan <me@chrismorgan.info>"]
|
||||
description = "A safe and convenient store for one value of each type"
|
||||
#documentation = "http://www.rust-ci.org/chris-morgan/anymap/doc/anymap/index.html"
|
||||
|
@ -12,3 +12,4 @@ license = "MIT/Apache-2.0"
|
|||
|
||||
[features]
|
||||
clone = []
|
||||
nightly = []
|
||||
|
|
|
@ -20,6 +20,8 @@ Cargo all the way: it is `anymap` on crates.io.
|
|||
|
||||
There is an optional `clone` feature on the `anymap` crate; if enabled, your `AnyMap` will require contained types to implement `Clone` and will itself satisfy `Clone`.
|
||||
|
||||
For users of the nightly instead of the beta of rustc there are a couple of things behind the `nightly` feature like a `drain` method on the `RawAnyMap` and a more efficient hashing technique which makes lookup in the map a tad faster.
|
||||
|
||||
Author
|
||||
------
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
//! This crate provides the `AnyMap` type, a safe and convenient store for one value of each type.
|
||||
|
||||
#![feature(core, std_misc)]
|
||||
#![cfg_attr(feature = "nightly", feature(core, std_misc))]
|
||||
#![cfg_attr(test, feature(test))]
|
||||
#![warn(missing_docs, unused_results)]
|
||||
|
||||
|
|
27
src/raw.rs
27
src/raw.rs
|
@ -5,12 +5,17 @@
|
|||
use std::any::TypeId;
|
||||
use std::borrow::Borrow;
|
||||
use std::collections::hash_map::{self, HashMap};
|
||||
#[cfg(feature = "nightly")]
|
||||
use std::collections::hash_state::HashState;
|
||||
use std::default::Default;
|
||||
use std::hash::{Hash, Hasher};
|
||||
use std::hash::Hash;
|
||||
#[cfg(feature = "nightly")]
|
||||
use std::hash::Hasher;
|
||||
use std::iter::IntoIterator;
|
||||
#[cfg(feature = "nightly")]
|
||||
use std::mem;
|
||||
use std::ops::{Index, IndexMut};
|
||||
#[cfg(feature = "nightly")]
|
||||
use std::ptr;
|
||||
|
||||
#[cfg(not(feature = "clone"))]
|
||||
|
@ -18,13 +23,16 @@ pub use std::any::Any;
|
|||
#[cfg(feature = "clone")]
|
||||
pub use with_clone::Any;
|
||||
|
||||
#[cfg(feature = "nightly")]
|
||||
struct TypeIdHasher {
|
||||
value: u64,
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "clone", derive(Clone))]
|
||||
#[cfg(feature = "nightly")]
|
||||
struct TypeIdState;
|
||||
|
||||
#[cfg(feature = "nightly")]
|
||||
impl HashState for TypeIdState {
|
||||
type Hasher = TypeIdHasher;
|
||||
|
||||
|
@ -33,6 +41,7 @@ impl HashState for TypeIdState {
|
|||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "nightly")]
|
||||
impl Hasher for TypeIdHasher {
|
||||
#[inline(always)]
|
||||
fn write(&mut self, bytes: &[u8]) {
|
||||
|
@ -58,7 +67,11 @@ impl Hasher for TypeIdHasher {
|
|||
#[derive(Debug)]
|
||||
#[cfg_attr(feature = "clone", derive(Clone))]
|
||||
pub struct RawAnyMap {
|
||||
#[cfg(feature = "nightly")]
|
||||
inner: HashMap<TypeId, Box<Any>, TypeIdState>,
|
||||
|
||||
#[cfg(not(feature = "nightly"))]
|
||||
inner: HashMap<TypeId, Box<Any>>,
|
||||
}
|
||||
|
||||
impl Default for RawAnyMap {
|
||||
|
@ -67,12 +80,20 @@ impl Default for RawAnyMap {
|
|||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "nightly")]
|
||||
impl_common_methods! {
|
||||
field: RawAnyMap.inner;
|
||||
new() => HashMap::with_hash_state(TypeIdState);
|
||||
with_capacity(capacity) => HashMap::with_capacity_and_hash_state(capacity, TypeIdState);
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "nightly"))]
|
||||
impl_common_methods! {
|
||||
field: RawAnyMap.inner;
|
||||
new() => HashMap::new();
|
||||
with_capacity(capacity) => HashMap::with_capacity(capacity);
|
||||
}
|
||||
|
||||
/// RawAnyMap iterator.
|
||||
#[derive(Clone)]
|
||||
pub struct Iter<'a> {
|
||||
|
@ -114,14 +135,17 @@ impl ExactSizeIterator for IntoIter {
|
|||
}
|
||||
|
||||
/// RawAnyMap drain iterator.
|
||||
#[cfg(feature = "nightly")]
|
||||
pub struct Drain<'a> {
|
||||
inner: hash_map::Drain<'a, TypeId, Box<Any>>,
|
||||
}
|
||||
#[cfg(feature = "nightly")]
|
||||
impl<'a> Iterator for Drain<'a> {
|
||||
type Item = Box<Any>;
|
||||
#[inline] fn next(&mut self) -> Option<Box<Any>> { self.inner.next().map(|x| x.1) }
|
||||
#[inline] fn size_hint(&self) -> (usize, Option<usize>) { self.inner.size_hint() }
|
||||
}
|
||||
#[cfg(feature = "nightly")]
|
||||
impl<'a> ExactSizeIterator for Drain<'a> {
|
||||
#[inline] fn len(&self) -> usize { self.inner.len() }
|
||||
}
|
||||
|
@ -165,6 +189,7 @@ impl RawAnyMap {
|
|||
///
|
||||
/// Keeps the allocated memory for reuse.
|
||||
#[inline]
|
||||
#[cfg(feature = "nightly")]
|
||||
pub fn drain(&mut self) -> Drain {
|
||||
Drain {
|
||||
inner: self.inner.drain(),
|
||||
|
|
|
@ -1,7 +1,17 @@
|
|||
use raw::Any;
|
||||
use std::mem;
|
||||
#[cfg(feature = "nightly")]
|
||||
use std::raw::TraitObject;
|
||||
|
||||
#[cfg(not(feature = "nightly"))]
|
||||
#[repr(C)]
|
||||
#[allow(raw_pointer_derive)]
|
||||
#[derive(Copy, Clone)]
|
||||
struct TraitObject {
|
||||
pub data: *mut (),
|
||||
pub vtable: *mut (),
|
||||
}
|
||||
|
||||
#[allow(missing_docs)] // Bogus warning (it’s not public outside the crate), ☹
|
||||
pub trait UncheckedAnyExt {
|
||||
unsafe fn downcast_ref_unchecked<T: 'static>(&self) -> &T;
|
||||
|
|
Loading…
Reference in a new issue