diff --git a/src/equivalent.rs b/src/equivalent.rs index 649d809..ad6635f 100644 --- a/src/equivalent.rs +++ b/src/equivalent.rs @@ -1,4 +1,4 @@ -use std::borrow::Borrow; +use core::borrow::Borrow; /// Key equivalence trait. /// diff --git a/src/lib.rs b/src/lib.rs index 3def5d6..a5ebc06 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,7 +1,7 @@ // We *mostly* avoid unsafe code, but `map::core::raw` allows it to use `RawTable` buckets. #![deny(unsafe_code)] #![doc(html_root_url = "https://docs.rs/indexmap/1/")] -#![cfg_attr(not(has_std), no_std)] +#![no_std] //! [`IndexMap`] is a hash table where the iteration order of the key-value //! pairs is independent of the hash values of the keys. @@ -82,22 +82,17 @@ #[cfg(not(has_std))] extern crate alloc; +#[cfg(has_std)] +#[macro_use] +extern crate std; + extern crate hashbrown; #[cfg(not(has_std))] -pub(crate) mod std { - pub use core::*; - pub mod alloc { - pub use alloc::*; - } - pub mod collections { - pub use alloc::collections::*; - } - pub use alloc::vec; -} +use alloc::vec::{self, Vec}; -#[cfg(not(has_std))] -use std::vec::Vec; +#[cfg(has_std)] +use std::vec::{self, Vec}; #[macro_use] mod macros; diff --git a/src/map.rs b/src/map.rs index c851844..a904daf 100644 --- a/src/map.rs +++ b/src/map.rs @@ -3,26 +3,22 @@ mod core; -#[cfg(not(has_std))] -use std::vec::Vec; - pub use mutable_keys::MutableKeys; #[cfg(feature = "rayon")] pub use rayon::map as rayon; -use std::hash::BuildHasher; -use std::hash::Hash; -use std::hash::Hasher; -use std::iter::FromIterator; -use std::ops::RangeFull; +use core::cmp::Ordering; +use core::fmt; +use core::hash::{BuildHasher, Hash, Hasher}; +use core::iter::FromIterator; +use core::ops::{Index, IndexMut, RangeFull}; +use core::slice::{Iter as SliceIter, IterMut as SliceIterMut}; +use vec::{self, Vec}; #[cfg(has_std)] use std::collections::hash_map::RandomState; -use std::cmp::Ordering; -use std::fmt; - use self::core::IndexMapCore; use equivalent::Equivalent; use util::third; @@ -720,10 +716,6 @@ impl IndexMap { } } -use std::slice::Iter as SliceIter; -use std::slice::IterMut as SliceIterMut; -use std::vec::IntoIter as VecIntoIter; - /// An iterator over the keys of a `IndexMap`. /// /// This `struct` is created by the [`keys`] method on [`IndexMap`]. See its @@ -922,7 +914,7 @@ impl<'a, K, V> ExactSizeIterator for IterMut<'a, K, V> { /// [`into_iter`]: struct.IndexMap.html#method.into_iter /// [`IndexMap`]: struct.IndexMap.html pub struct IntoIter { - pub(crate) iter: VecIntoIter>, + pub(crate) iter: vec::IntoIter>, } impl Iterator for IntoIter { @@ -962,7 +954,7 @@ where K: 'a, V: 'a, { - pub(crate) iter: ::std::vec::Drain<'a, Bucket>, + pub(crate) iter: vec::Drain<'a, Bucket>, } impl<'a, K, V> Iterator for Drain<'a, K, V> { @@ -1013,8 +1005,6 @@ where } } -use std::ops::{Index, IndexMut}; - impl<'a, K, V, Q: ?Sized, S> Index<&'a Q> for IndexMap where Q: Hash + Equivalent, @@ -1149,6 +1139,7 @@ where #[cfg(test)] mod tests { use super::*; + use std::string::String; use util::enumerate; #[test] @@ -1405,8 +1396,7 @@ mod tests { map_b.swap_remove(&1); assert_ne!(map_a, map_b); - let map_c: IndexMap<_, String> = - map_b.into_iter().map(|(k, v)| (k, v.to_owned())).collect(); + let map_c: IndexMap<_, String> = map_b.into_iter().map(|(k, v)| (k, v.into())).collect(); assert_ne!(map_a, map_c); assert_ne!(map_c, map_a); } diff --git a/src/map/core.rs b/src/map/core.rs index 89c20ff..7de69ac 100644 --- a/src/map/core.rs +++ b/src/map/core.rs @@ -9,16 +9,13 @@ mod raw; -#[cfg(not(has_std))] -use std::vec::Vec; - use hashbrown::raw::RawTable; -use std::cmp; -use std::fmt; -use std::mem::replace; -use std::ops::RangeFull; -use std::vec::Drain; +use core::cmp; +use core::fmt; +use core::mem::replace; +use core::ops::RangeFull; +use vec::{Drain, Vec}; use equivalent::Equivalent; use util::enumerate; diff --git a/src/map/core/raw.rs b/src/map/core/raw.rs index c06f8c3..bf3e258 100644 --- a/src/map/core/raw.rs +++ b/src/map/core/raw.rs @@ -3,9 +3,9 @@ //! mostly in dealing with its bucket "pointers". use super::{Entry, Equivalent, HashValue, IndexMapCore, VacantEntry}; +use core::fmt; +use core::mem::replace; use hashbrown::raw::RawTable; -use std::fmt; -use std::mem::replace; type RawBucket = hashbrown::raw::Bucket; diff --git a/src/mutable_keys.rs b/src/mutable_keys.rs index 19df504..0688441 100644 --- a/src/mutable_keys.rs +++ b/src/mutable_keys.rs @@ -1,5 +1,4 @@ -use std::hash::BuildHasher; -use std::hash::Hash; +use core::hash::{BuildHasher, Hash}; use super::{Equivalent, IndexMap}; diff --git a/src/rayon/map.rs b/src/rayon/map.rs index c0a5899..bace670 100644 --- a/src/rayon/map.rs +++ b/src/rayon/map.rs @@ -9,10 +9,10 @@ use super::collect; use super::rayon::iter::plumbing::{Consumer, ProducerCallback, UnindexedConsumer}; use super::rayon::prelude::*; -use std::cmp::Ordering; -use std::fmt; -use std::hash::BuildHasher; -use std::hash::Hash; +use core::cmp::Ordering; +use core::fmt; +use core::hash::{BuildHasher, Hash}; +use vec::Vec; use Bucket; use Entries; @@ -398,6 +398,7 @@ where #[cfg(test)] mod tests { use super::*; + use std::string::String; #[test] fn insert_order() { @@ -433,10 +434,8 @@ mod tests { map_b.insert(3, "3"); assert!(!map_a.par_eq(&map_b)); - let map_c: IndexMap<_, String> = map_b - .into_par_iter() - .map(|(k, v)| (k, v.to_owned())) - .collect(); + let map_c: IndexMap<_, String> = + map_b.into_par_iter().map(|(k, v)| (k, v.into())).collect(); assert!(!map_a.par_eq(&map_c)); assert!(!map_c.par_eq(&map_a)); } diff --git a/src/rayon/mod.rs b/src/rayon/mod.rs index 76386ff..254c6bb 100644 --- a/src/rayon/mod.rs +++ b/src/rayon/mod.rs @@ -2,8 +2,14 @@ extern crate rayon; use self::rayon::prelude::*; +#[cfg(not(has_std))] +use alloc::collections::LinkedList; + +#[cfg(has_std)] use std::collections::LinkedList; +use vec::Vec; + // generate `ParallelIterator` methods by just forwarding to the underlying // self.entries and mapping its elements. macro_rules! parallel_iterator_methods { diff --git a/src/rayon/set.rs b/src/rayon/set.rs index 9e0bbe5..f8c3e06 100644 --- a/src/rayon/set.rs +++ b/src/rayon/set.rs @@ -9,10 +9,10 @@ use super::collect; use super::rayon::iter::plumbing::{Consumer, ProducerCallback, UnindexedConsumer}; use super::rayon::prelude::*; -use std::cmp::Ordering; -use std::fmt; -use std::hash::BuildHasher; -use std::hash::Hash; +use core::cmp::Ordering; +use core::fmt; +use core::hash::{BuildHasher, Hash}; +use vec::Vec; use Entries; use IndexSet; diff --git a/src/serde.rs b/src/serde.rs index 9199a8d..3405473 100644 --- a/src/serde.rs +++ b/src/serde.rs @@ -6,9 +6,9 @@ use self::serde::de::{ }; use self::serde::ser::{Serialize, SerializeMap, SerializeSeq, Serializer}; -use std::fmt::{self, Formatter}; -use std::hash::{BuildHasher, Hash}; -use std::marker::PhantomData; +use core::fmt::{self, Formatter}; +use core::hash::{BuildHasher, Hash}; +use core::marker::PhantomData; use IndexMap; diff --git a/src/set.rs b/src/set.rs index 28ee864..52bba26 100644 --- a/src/set.rs +++ b/src/set.rs @@ -3,20 +3,16 @@ #[cfg(feature = "rayon")] pub use rayon::set as rayon; -#[cfg(not(has_std))] -use std::vec::Vec; - #[cfg(has_std)] use std::collections::hash_map::RandomState; -use std::cmp::Ordering; -use std::fmt; -use std::hash::{BuildHasher, Hash}; -use std::iter::{Chain, FromIterator}; -use std::ops::RangeFull; -use std::ops::{BitAnd, BitOr, BitXor, Sub}; -use std::slice; -use std::vec; +use core::cmp::Ordering; +use core::fmt; +use core::hash::{BuildHasher, Hash}; +use core::iter::{Chain, FromIterator}; +use core::ops::{BitAnd, BitOr, BitXor, RangeFull, Sub}; +use core::slice; +use vec::{self, Vec}; use super::{Entries, Equivalent, IndexMap}; @@ -1163,6 +1159,7 @@ where #[cfg(test)] mod tests { use super::*; + use std::string::String; use util::enumerate; #[test] diff --git a/src/util.rs b/src/util.rs index 4174316..e7bb0e1 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,4 +1,4 @@ -use std::iter::Enumerate; +use core::iter::Enumerate; pub(crate) fn third(t: (A, B, C)) -> C { t.2