X-Git-Url: https://git.chrismorgan.info/anymap/blobdiff_plain/8bc7c76088639ab3a65a642f5d79ce20ef20e2f4..2bcbd9c551d256e06e3e3044a2a99f79a7946449:/src/lib.rs
diff --git a/src/lib.rs b/src/lib.rs
index 1ad840b..d4f227a 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -4,8 +4,19 @@
#![warn(missing_docs, unused_results)]
-use std::any::{Any, TypeId};
-use std::marker::PhantomData;
+#![cfg_attr(not(feature = "std"), no_std)]
+
+use core::any::{Any, TypeId};
+use core::marker::PhantomData;
+
+#[cfg(not(any(feature = "std", feature = "hashbrown")))]
+compile_error!("anymap: you must enable the 'std' feature or the 'hashbrown' feature");
+
+#[cfg(not(feature = "std"))]
+extern crate alloc;
+
+#[cfg(not(feature = "std"))]
+use alloc::boxed::Box;
use raw::RawMap;
use any::{UncheckedAnyExt, IntoBox};
@@ -60,6 +71,10 @@ macro_rules! impl_common_methods {
self.$field.shrink_to_fit()
}
+ // Additional stable methods (as of 1.60.0-nightly) that could be added:
+ // try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError> (1.57.0)
+ // shrink_to(&mut self, min_capacity: usize) (1.56.0)
+
/// Returns the number of items in the collection.
#[inline]
pub fn len(&self) -> usize {
@@ -95,7 +110,7 @@ pub mod raw;
/// type-safe access to those values.
///
/// The type parameter `A` allows you to use a different value type; normally you will want it to
-/// be `std::any::Any`, but there are other choices:
+/// be `core::any::Any` (also known as `std::any::Any`), but there are other choices:
///
/// - If you want the entire map to be cloneable, use `CloneAny` instead of `Any`; with that, you
/// can only add types that implement `Clone` to the map.
@@ -104,9 +119,9 @@ pub mod raw;
///
/// Cumulatively, there are thus six forms of map:
///
-/// - [Map]<dyn [std::any::Any]>
, also spelled [`AnyMap`] for convenience.
-/// - [Map]<dyn [std::any::Any] + Send>
-/// - [Map]<dyn [std::any::Any] + Send + Sync>
+/// - [Map]<dyn [core::any::Any]>
, also spelled [`AnyMap`] for convenience.
+/// - [Map]<dyn [core::any::Any] + Send>
+/// - [Map]<dyn [core::any::Any] + Send + Sync>
/// - [Map]<dyn [CloneAny]>
/// - [Map]<dyn [CloneAny] + Send>
/// - [Map]<dyn [CloneAny] + Send + Sync>
@@ -114,7 +129,7 @@ pub mod raw;
/// ## Example
///
/// (Here using the [`AnyMap`] convenience alias; the first line could use
-/// [anymap::Map][Map]::<[std::any::Any]>::new()
instead if desired.)
+/// [anymap::Map][Map]::<[core::any::Any]>::new()
instead if desired.)
///
/// ```rust
/// let mut data = anymap::AnyMap::new();
@@ -192,6 +207,8 @@ impl Map {
}
}
+ // rustc 1.60.0-nightly has another method try_insert that would be nice to add when stable.
+
/// Removes the `T` value from the collection,
/// returning it if there was one or `None` if there was not.
#[inline]
@@ -222,6 +239,15 @@ impl Map {
}
}
+impl Extend> for Map {
+ #[inline]
+ fn extend>>(&mut self, iter: T) {
+ for item in iter {
+ let _ = unsafe { self.raw.insert(item.type_id(), item) };
+ }
+ }
+}
+
impl AsRef> for Map {
#[inline]
fn as_ref(&self) -> &RawMap {
@@ -444,7 +470,7 @@ mod tests {
fn assert_send() { }
fn assert_sync() { }
fn assert_clone() { }
- fn assert_debug() { }
+ fn assert_debug() { }
assert_send::