+//! This crate provides a safe and convenient store for one value of each type.
+//!
+//! Your starting point is [`Map`]. It has an example.
+//!
+//! # Cargo features
+//!
+//! This crate has two independent features, each of which provides an implementation providing
+//! types `Map`, `AnyMap`, `OccupiedEntry`, `VacantEntry`, `Entry` and `RawMap`:
+//!
+#![cfg_attr(feature = "std", doc = " - **std** (default, *enabled* in this build):")]
+#![cfg_attr(not(feature = "std"), doc = " - **std** (default, *disabled* in this build):")]
+//! an implementation using `std::collections::hash_map`, placed in the crate root
+//! (e.g. `anymap::AnyMap`).
+//!
+#![cfg_attr(feature = "hashbrown", doc = " - **hashbrown** (optional; *enabled* in this build):")]
+#![cfg_attr(not(feature = "hashbrown"), doc = " - **hashbrown** (optional; *disabled* in this build):")]
+//! an implementation using `alloc` and `hashbrown::hash_map`, placed in a module `hashbrown`
+//! (e.g. `anymap::hashbrown::AnyMap`).
+
+#![warn(missing_docs, unused_results)]
+
+#![cfg_attr(not(feature = "std"), no_std)]
+
+use core::convert::TryInto;
+use core::hash::Hasher;
+
+#[cfg(not(feature = "std"))]
+extern crate alloc;
+
+pub use crate::any::CloneAny;
+
+mod any;
+
+#[cfg(any(feature = "std", feature = "hashbrown"))]
+macro_rules! everything {
+ ($example_init:literal, $($parent:ident)::+ $(, $entry_generics:ty)?) => {
+ use core::any::{Any, TypeId};
+ use core::hash::BuildHasherDefault;
+ use core::marker::PhantomData;
+
+ #[cfg(not(feature = "std"))]
+ use alloc::boxed::Box;
+
+ use ::$($parent)::+::hash_map::{self, HashMap};
+
+ use crate::any::{Downcast, IntoBox};
+
+ /// Raw access to the underlying `HashMap`.
+ ///
+ /// This alias is provided for convenience because of the ugly third generic parameter.
+ pub type RawMap<A> = HashMap<TypeId, Box<A>, BuildHasherDefault<TypeIdHasher>>;
+
+ /// A collection containing zero or one values for any given type and allowing convenient,
+ /// 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 `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]<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]::<[core::any::Any]>::new()</code> instead if desired.)
+ ///
+ /// ```rust
+ #[doc = $example_init]
+ /// assert_eq!(data.get(), None::<&i32>);
+ /// data.insert(42i32);
+ /// assert_eq!(data.get(), Some(&42i32));
+ /// data.remove::<i32>();
+ /// assert_eq!(data.get::<i32>(), None);
+ ///
+ /// #[derive(Clone, PartialEq, Debug)]
+ /// struct Foo {
+ /// str: String,
+ /// }
+ ///
+ /// assert_eq!(data.get::<Foo>(), None);
+ /// data.insert(Foo { str: format!("foo") });
+ /// assert_eq!(data.get(), Some(&Foo { str: format!("foo") }));
+ /// data.get_mut::<Foo>().map(|foo| foo.str.push('t'));
+ /// assert_eq!(&*data.get::<Foo>().unwrap().str, "foot");
+ /// ```
+ ///
+ /// Values containing non-static references are not permitted.
+ #[derive(Debug)]
+ pub struct Map<A: ?Sized + Downcast = dyn Any> {
+ raw: RawMap<A>,
+ }
+
+ // #[derive(Clone)] would want A to implement Clone, but in reality only Box<A> can.
+ impl<A: ?Sized + Downcast> Clone for Map<A> where Box<A>: Clone {
+ #[inline]
+ fn clone(&self) -> Map<A> {
+ Map {
+ raw: self.raw.clone(),
+ }
+ }
+ }
+
+ /// The most common type of `Map`: just using `Any`; <code>[Map]<dyn [Any]></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. It’s a bit sad, really. Ah well, I guess this approach will do.
+ pub type AnyMap = Map<dyn Any>;
+
+ impl<A: ?Sized + Downcast> Default for Map<A> {
+ #[inline]
+ fn default() -> Map<A> {
+ Map::new()
+ }
+ }
+
+ impl<A: ?Sized + Downcast> Map<A> {