no_std support
[anymap] / src / lib.rs
index 3f656e3084d693493cb7f0d86ff9d4e313f0aea2..6366c70b5a3781fda251734893cf2585d7f196bc 100644 (file)
@@ -1,13 +1,26 @@
-//! This crate provides the `AnyMap` type, a safe and convenient store for one value of each type.
+//! This crate provides a safe and convenient store for one value of each type.
+//!
+//! Your starting point is [`Map`]. It has an example.
 
 #![warn(missing_docs, unused_results)]
 
-use std::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};
-pub use any::{Any, CloneAny};
+pub use any::CloneAny;
 
 macro_rules! impl_common_methods {
     (
@@ -93,14 +106,29 @@ 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 `anymap::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.
+/// - You can add on `+ Send` or `+ Send + Sync` (e.g. `Map<dyn Any + Send>`) to add those auto
+///   traits.
+///
+/// Cumulatively, there are thus six forms of map:
+///
+/// - <code>[Map]&lt;dyn [core::any::Any]&gt;</code>, also spelled [`AnyMap`] for convenience.
+/// - <code>[Map]&lt;dyn [core::any::Any] + Send&gt;</code>
+/// - <code>[Map]&lt;dyn [core::any::Any] + Send + Sync&gt;</code>
+/// - <code>[Map]&lt;dyn [CloneAny]&gt;</code>
+/// - <code>[Map]&lt;dyn [CloneAny] + Send&gt;</code>
+/// - <code>[Map]&lt;dyn [CloneAny] + Send + Sync&gt;</code>
+///
+/// ## Example
 ///
-/// - If you want the entire map to be cloneable, use `CloneAny` instead of `Any`.
-/// - You can add on `+ Send` and/or `+ Sync` (e.g. `Map<dyn Any + Send>`) to add those bounds.
+/// (Here using the [`AnyMap`] convenience alias; the first line could use
+/// <code>[anymap::Map][Map]::&lt;[core::any::Any]&gt;::new()</code> instead if desired.)
 ///
 /// ```rust
-/// # use anymap::AnyMap;
-/// let mut data = AnyMap::new();
+/// let mut data = anymap::AnyMap::new();
 /// assert_eq!(data.get(), None::<&i32>);
 /// data.insert(42i32);
 /// assert_eq!(data.get(), Some(&42i32));
@@ -135,7 +163,7 @@ impl<A: ?Sized + UncheckedAnyExt> Clone for Map<A> where Box<A>: Clone {
     }
 }
 
-/// The most common type of `Map`: just using `Any`.
+/// The most common type of `Map`: just using `Any`; <code>[Map]&lt;dyn [Any]&gt;</code>.
 ///
 /// Why is this a separate type alias rather than a default value for `Map<A>`? `Map::new()`
 /// doesn’t seem to be happy to infer that it should go with the default value.
@@ -219,10 +247,10 @@ impl<A: ?Sized + UncheckedAnyExt> AsMut<RawMap<A>> for Map<A> {
     }
 }
 
-impl<A: ?Sized + UncheckedAnyExt> Into<RawMap<A>> for Map<A> {
+impl<A: ?Sized + UncheckedAnyExt> From<Map<A>> for RawMap<A> {
     #[inline]
-    fn into(self) -> RawMap<A> {
-        self.raw
+    fn from(map: Map<A>) -> RawMap<A> {
+        map.raw
     }
 }
 
@@ -312,8 +340,7 @@ impl<'a, A: ?Sized + UncheckedAnyExt, V: IntoBox<A>> VacantEntry<'a, A, V> {
 
 #[cfg(test)]
 mod tests {
-    use {Map, AnyMap, Entry};
-    use any::{Any, CloneAny};
+    use super::*;
 
     #[derive(Clone, Debug, PartialEq)] struct A(i32);
     #[derive(Clone, Debug, PartialEq)] struct B(i32);
@@ -428,26 +455,21 @@ mod tests {
         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 + Sync>>();
         assert_sync::<Map<dyn Any + Send + Sync>>();
         assert_debug::<Map<dyn Any>>();
         assert_debug::<Map<dyn Any + Send>>();
-        assert_debug::<Map<dyn Any + Sync>>();
         assert_debug::<Map<dyn Any + Send + Sync>>();
         assert_send::<Map<dyn CloneAny + Send>>();
         assert_send::<Map<dyn CloneAny + Send + Sync>>();
-        assert_sync::<Map<dyn CloneAny + Sync>>();
         assert_sync::<Map<dyn CloneAny + Send + Sync>>();
         assert_clone::<Map<dyn CloneAny + Send>>();
         assert_clone::<Map<dyn CloneAny + Send + Sync>>();
-        assert_clone::<Map<dyn CloneAny + Sync>>();
         assert_clone::<Map<dyn CloneAny + Send + Sync>>();
         assert_debug::<Map<dyn CloneAny>>();
         assert_debug::<Map<dyn CloneAny + Send>>();
-        assert_debug::<Map<dyn CloneAny + Sync>>();
         assert_debug::<Map<dyn CloneAny + Send + Sync>>();
     }
 }