no_std support
[anymap] / src / raw.rs
index e6e989f7e6b7d89779bc8a144d5b30f7ee24167c..081edd4bd2845400f1f3ad00ea23e9d33167362c 100644 (file)
@@ -2,15 +2,18 @@
 //!
 //! All relevant details are in the `RawMap` struct.
 
-use std::any::{Any, TypeId};
-use std::borrow::Borrow;
+use core::any::{Any, TypeId};
+use core::borrow::Borrow;
+#[cfg(all(feature = "std", not(feature = "hashbrown")))]
 use std::collections::hash_map::{self, HashMap};
-use std::convert::TryInto;
-use std::hash::Hash;
-use std::hash::{Hasher, BuildHasherDefault};
-#[cfg(test)]
-use std::mem;
-use std::ops::{Index, IndexMut};
+#[cfg(feature = "hashbrown")]
+use hashbrown::hash_map::{self, HashMap};
+#[cfg(not(feature = "std"))]
+use alloc::boxed::Box;
+use core::convert::TryInto;
+use core::hash::Hash;
+use core::hash::{Hasher, BuildHasherDefault};
+use core::ops::{Index, IndexMut};
 
 use crate::any::UncheckedAnyExt;
 
@@ -37,11 +40,13 @@ impl Hasher for TypeIdHasher {
 
 #[test]
 fn type_id_hasher() {
+    #[cfg(not(feature = "std"))]
+    use alloc::vec::Vec;
     fn verify_hashing_with(type_id: TypeId) {
         let mut hasher = TypeIdHasher::default();
         type_id.hash(&mut hasher);
         // SAFETY: u64 is valid for all bit patterns.
-        assert_eq!(hasher.finish(), unsafe { mem::transmute::<TypeId, u64>(type_id) });
+        assert_eq!(hasher.finish(), unsafe { core::mem::transmute::<TypeId, u64>(type_id) });
     }
     // Pick a variety of types, just to demonstrate it’s all sane. Normal, zero-sized, unsized, &c.
     verify_hashing_with(TypeId::of::<usize>());
@@ -262,12 +267,18 @@ impl<A: ?Sized + UncheckedAnyExt> IntoIterator for RawMap<A> {
 
 /// A view into a single occupied location in a `RawMap`.
 pub struct OccupiedEntry<'a, A: ?Sized + UncheckedAnyExt> {
+    #[cfg(all(feature = "std", not(feature = "hashbrown")))]
     inner: hash_map::OccupiedEntry<'a, TypeId, Box<A>>,
+    #[cfg(feature = "hashbrown")]
+    inner: hash_map::OccupiedEntry<'a, TypeId, Box<A>, BuildHasherDefault<TypeIdHasher>>,
 }
 
 /// A view into a single empty location in a `RawMap`.
 pub struct VacantEntry<'a, A: ?Sized + UncheckedAnyExt> {
+    #[cfg(all(feature = "std", not(feature = "hashbrown")))]
     inner: hash_map::VacantEntry<'a, TypeId, Box<A>>,
+    #[cfg(feature = "hashbrown")]
+    inner: hash_map::VacantEntry<'a, TypeId, Box<A>, BuildHasherDefault<TypeIdHasher>>,
 }
 
 /// A view into a single location in a `RawMap`, which may be vacant or occupied.