From 18518214c4609e182be5277b14c7c510c8fa9505 Mon Sep 17 00:00:00 2001 From: Chris Morgan Date: Sat, 18 Apr 2015 10:44:34 +1000 Subject: [PATCH] 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. --- .travis.yml | 10 +++++----- Cargo.toml | 3 ++- README.md | 2 ++ src/lib.rs | 2 +- src/raw.rs | 27 ++++++++++++++++++++++++++- src/unchecked_any.rs | 10 ++++++++++ 6 files changed, 46 insertions(+), 8 deletions(-) diff --git a/.travis.yml b/.travis.yml index 42f2b63..2248e9b 100644 --- a/.travis.yml +++ b/.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 diff --git a/Cargo.toml b/Cargo.toml index 4975bd0..5ffe510 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "anymap" -version = "0.10.2" +version = "0.10.3" authors = ["Chris Morgan "] 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 = [] diff --git a/README.md b/README.md index da5510e..a815caf 100644 --- a/README.md +++ b/README.md @@ -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 ------ diff --git a/src/lib.rs b/src/lib.rs index a40da70..9c0ff30 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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)] diff --git a/src/raw.rs b/src/raw.rs index e75de32..0ed8886 100644 --- a/src/raw.rs +++ b/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, TypeIdState>, + + #[cfg(not(feature = "nightly"))] + inner: HashMap>, } 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>, } +#[cfg(feature = "nightly")] impl<'a> Iterator for Drain<'a> { type Item = Box; #[inline] fn next(&mut self) -> Option> { self.inner.next().map(|x| x.1) } #[inline] fn size_hint(&self) -> (usize, Option) { 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(), diff --git a/src/unchecked_any.rs b/src/unchecked_any.rs index bfd7d95..52dee67 100644 --- a/src/unchecked_any.rs +++ b/src/unchecked_any.rs @@ -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(&self) -> &T;