#![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};
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 {
/// 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.
///
/// Cumulatively, there are thus six forms of map:
///
-/// - <code>[Map]<dyn [std::any::Any]></code>, also spelled [`AnyMap`] for convenience.
-/// - <code>[Map]<dyn [std::any::Any] + Send></code>
-/// - <code>[Map]<dyn [std::any::Any] + Send + Sync></code>
+/// - <code>[Map]<dyn [core::any::Any]></code>, also spelled [`AnyMap`] for convenience.
+/// - <code>[Map]<dyn [core::any::Any] + Send></code>
+/// - <code>[Map]<dyn [core::any::Any] + Send + Sync></code>
/// - <code>[Map]<dyn [CloneAny]></code>
/// - <code>[Map]<dyn [CloneAny] + Send></code>
/// - <code>[Map]<dyn [CloneAny] + Send + Sync></code>
/// ## Example
///
/// (Here using the [`AnyMap`] convenience alias; the first line could use
-/// <code>[anymap::Map][Map]::<[std::any::Any]>::new()</code> instead if desired.)
+/// <code>[anymap::Map][Map]::<[core::any::Any]>::new()</code> instead if desired.)
///
/// ```rust
/// let mut data = anymap::AnyMap::new();
}
}
+ // 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]
}
}
+impl<A: ?Sized + UncheckedAnyExt> Extend<Box<A>> for Map<A> {
+ #[inline]
+ fn extend<T: IntoIterator<Item = Box<A>>>(&mut self, iter: T) {
+ for item in iter {
+ let _ = unsafe { self.raw.insert(item.type_id(), item) };
+ }
+ }
+}
+
impl<A: ?Sized + UncheckedAnyExt> AsRef<RawMap<A>> for Map<A> {
#[inline]
fn as_ref(&self) -> &RawMap<A> {
fn assert_send<T: Send>() { }
fn assert_sync<T: Sync>() { }
fn assert_clone<T: Clone>() { }
- fn assert_debug<T: ::std::fmt::Debug>() { }
+ fn assert_debug<T: ::core::fmt::Debug>() { }
assert_send::<Map<dyn Any + Send>>();
assert_send::<Map<dyn Any + Send + Sync>>();
assert_sync::<Map<dyn Any + Send + Sync>>();