Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

no_std support #95

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,28 @@ matrix:
# (we only use it in benchmarks anyway...)
- cargo generate-lockfile
- cargo update -p lazy_static --precise 1.0.2
env:
- FEATURES='force_std'
- rust: stable
env:
- FEATURES='serde-1'
- TEST_FEATURES='force_std'
- rust: beta
env:
- TEST_FEATURES='force_std'
- rust: nightly
env:
- TEST_FEATURES='force_std'
- rust: nightly
env:
- FEATURES='test_low_transition_point'
- TEST_FEATURES='force_std'
branches:
only:
- master
script:
- |
cargo build --verbose --features "$FEATURES" &&
cargo test --verbose --features "$FEATURES" &&
cargo test --release --verbose --features "$FEATURES" &&
cargo test --verbose --features "$FEATURES $TEST_FEATURES" &&
cargo test --release --verbose --features "$FEATURES $TEST_FEATURES" &&
cargo doc --verbose --features "$FEATURES"
39 changes: 34 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,20 @@ indexmap.
keywords = ["hashmap"]
categories = ["data-structures"]

[package.metadata.release]
no-dev-version = true

[package.metadata.docs.rs]
features = ["serde-1"]

[lib]
bench = false

[dependencies]
serde = { version = "1.0", optional = true }

[dev-dependencies]
itertools = "0.7.0" # 0.8 not compiles on Rust 1.18
itertools = "0.7.0"
rand = "0.4"
quickcheck = { version = "0.6", default-features = false }
fnv = "1.0"
Expand All @@ -46,11 +52,34 @@ serde-1 = ["serde"]
test_low_transition_point = []
test_debug = []

force_std = []

[profile.bench]
debug = true

[package.metadata.release]
no-dev-version = true
[build-dependencies]
autocfg = "0.1.5"

[package.metadata.docs.rs]
features = ["serde-1"]
[[test]]
name = "tests"
required-features = [ "force_std" ]

[[test]]
name = "serde"
required-features = [ "force_std" ]

[[test]]
name = "quick"
required-features = [ "force_std" ]

[[test]]
name = "equivalent_trait"
required-features = [ "force_std" ]

[[bench]]
name = "bench"
required-features = [ "force_std" ]

[[bench]]
name = "faststring"
required-features = [ "force_std" ]
2 changes: 1 addition & 1 deletion benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,7 @@ fn remove_ordermap_100_000(b: &mut Bencher) {
b.iter(|| {
let mut map = map.clone();
for key in &keys {
map.remove(key).is_some();
let _ = map.remove(key).is_some();
}
assert_eq!(map.len(), 0);
map
Expand Down
9 changes: 9 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
extern crate autocfg;

fn main() {
//let ac = autocfg::new();
//ac.emit_sysroot_crate("std");
//ac.emit_sysroot_crate("alloc");
autocfg::emit("std");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this do? Commit logs don't explain it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would blindly emit a std config value. I suppose they were doing that as a stopgap until the commented crate tests were available -- that's now in autocfg 0.1.6.

//autocfg::emit("alloc");
}
2 changes: 1 addition & 1 deletion src/equivalent.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

use std::borrow::Borrow;
use core::borrow::Borrow;

/// Key equivalence trait.
///
Expand Down
13 changes: 13 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,19 @@
//! upgrade policy, where in a later 1.x version, we will raise the minimum
//! required Rust version.

#![cfg_attr(all(not(test), has_alloc, not(feature = "force_std")), no_std)]

extern crate core;

#[cfg(all(has_alloc, not(feature = "force_std")))]
extern crate alloc;

#[cfg(all(has_alloc, has_std))]
extern crate std;

#[cfg(any(not(has_alloc), feature = "force_std"))]
extern crate std as alloc;

#[macro_use]
mod macros;
#[cfg(feature = "serde-1")]
Expand Down
6 changes: 4 additions & 2 deletions src/macros.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@

#[macro_export]
/// Create an `IndexMap` from a list of key-value pairs
/// Create an `IndexMap` from a list of key-value pairs. Requires `std` to be available.
///
/// ## Example
///
/// ```
///
/// #[macro_use] extern crate indexmap;
/// # fn main() {
///
Expand All @@ -20,6 +20,7 @@
/// assert_eq!(map.keys().next(), Some(&"a"));
/// # }
/// ```
#[cfg(any(has_std, feature = "force_std", test))]
macro_rules! indexmap {
(@single $($x:tt)*) => (());
(@count $($rest:expr),*) => (<[()]>::len(&[$(indexmap!(@single $rest)),*]));
Expand Down Expand Up @@ -58,6 +59,7 @@ macro_rules! indexmap {
/// assert_eq!(set.iter().next(), Some(&"a"));
/// # }
/// ```
#[cfg(any(has_std, feature = "force_std", test))]
macro_rules! indexset {
(@single $($x:tt)*) => (());
(@count $($rest:expr),*) => (<[()]>::len(&[$(indexset!(@single $rest)),*]));
Expand Down
49 changes: 31 additions & 18 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@

pub use mutable_keys::MutableKeys;

use std::hash::Hash;
use std::hash::BuildHasher;
use std::hash::Hasher;
use std::iter::FromIterator;
use std::collections::hash_map::RandomState;
use std::ops::RangeFull;

use std::cmp::{max, Ordering};
use std::fmt;
use std::mem::{replace};
use std::marker::PhantomData;
use core::hash::Hash;
use core::hash::BuildHasher;
use core::hash::Hasher;
use core::iter::FromIterator;
use core::ops::RangeFull;

use core::cmp::{max, Ordering};
use core::fmt;
use core::mem::{replace};
use core::marker::PhantomData;

use alloc::vec::Vec;
use alloc::boxed::Box;
use alloc::vec;

use util::{third, ptrdistance, enumerate};
use equivalent::Equivalent;
Expand Down Expand Up @@ -246,6 +249,7 @@ impl<Sz> ShortHashProxy<Sz>
/// # Examples
///
/// ```
/// # extern crate indexmap;
/// use indexmap::IndexMap;
///
/// // count the frequency of each letter in a sentence.
Expand All @@ -260,7 +264,15 @@ impl<Sz> ShortHashProxy<Sz>
/// assert_eq!(letters.get(&'y'), None);
/// ```
#[derive(Clone)]
pub struct IndexMap<K, V, S = RandomState> {
#[cfg(any(has_std, feature = "force_std", test))]
pub struct IndexMap<K, V, S = ::std::collections::hash_map::RandomState> {
core: OrderMapCore<K, V>,
hash_builder: S,
}

#[derive(Clone)]
#[cfg(not(any(has_std, feature = "force_std", test)))]
pub struct IndexMap<K, V, S> {
core: OrderMapCore<K, V>,
hash_builder: S,
}
Expand Down Expand Up @@ -351,7 +363,8 @@ macro_rules! probe_loop {
}
}

impl<K, V> IndexMap<K, V> {
#[cfg(any(has_std, feature = "force_std", test))]
impl<K, V> IndexMap<K, V, ::std::collections::hash_map::RandomState> {
/// Create a new map. (Does not allocate.)
pub fn new() -> Self {
Self::with_capacity(0)
Expand Down Expand Up @@ -1492,9 +1505,9 @@ fn find_existing_entry_at<Sz>(indices: &[Pos], hash: HashValue,
});
}

use std::slice::Iter as SliceIter;
use std::slice::IterMut as SliceIterMut;
use std::vec::IntoIter as VecIntoIter;
use core::slice::Iter as SliceIter;
use core::slice::IterMut as SliceIterMut;
use alloc::vec::IntoIter as VecIntoIter;

/// An iterator over the keys of a `IndexMap`.
///
Expand Down Expand Up @@ -1730,7 +1743,7 @@ impl<K: fmt::Debug, V: fmt::Debug> fmt::Debug for IntoIter<K, V> {
/// [`drain`]: struct.IndexMap.html#method.drain
/// [`IndexMap`]: struct.IndexMap.html
pub struct Drain<'a, K, V> where K: 'a, V: 'a {
pub(crate) iter: ::std::vec::Drain<'a, Bucket<K, V>>
pub(crate) iter: ::alloc::vec::Drain<'a, Bucket<K, V>>
}

impl<'a, K, V> Iterator for Drain<'a, K, V> {
Expand Down Expand Up @@ -1779,7 +1792,7 @@ impl<K, V, S> IntoIterator for IndexMap<K, V, S>
}
}

use std::ops::{Index, IndexMut};
use core::ops::{Index, IndexMut};

impl<'a, K, V, Q: ?Sized, S> Index<&'a Q> for IndexMap<K, V, S>
where Q: Hash + Equivalent<K>,
Expand Down
4 changes: 2 additions & 2 deletions src/mutable_keys.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

use std::hash::Hash;
use std::hash::BuildHasher;
use core::hash::Hash;
use core::hash::BuildHasher;

use super::{IndexMap, Equivalent};

Expand Down
28 changes: 18 additions & 10 deletions src/set.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
//! A hash set implemented using `IndexMap`

use std::cmp::Ordering;
use std::collections::hash_map::RandomState;
use std::fmt;
use std::iter::{FromIterator, Chain};
use std::hash::{Hash, BuildHasher};
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::iter::{FromIterator, Chain};
use core::hash::{Hash, BuildHasher};
use core::ops::RangeFull;
use core::ops::{BitAnd, BitOr, BitXor, Sub};
use core::slice;
use alloc::vec;

use super::{IndexMap, Equivalent};

Expand Down Expand Up @@ -45,6 +44,7 @@ type Bucket<T> = super::Bucket<T, ()>;
/// # Examples
///
/// ```
/// # extern crate indexmap;
/// use indexmap::IndexSet;
///
/// // Collects which letters appear in a sentence.
Expand All @@ -56,7 +56,14 @@ type Bucket<T> = super::Bucket<T, ()>;
/// assert!(!letters.contains(&'y'));
/// ```
#[derive(Clone)]
pub struct IndexSet<T, S = RandomState> {
#[cfg(any(has_std, feature = "force_std", test))]
pub struct IndexSet<T, S = ::std::collections::hash_map::RandomState> {
map: IndexMap<T, (), S>,
}

#[derive(Clone)]
#[cfg(not(any(has_std, feature = "force_std", test)))]
pub struct IndexSet<T, S> {
map: IndexMap<T, (), S>,
}

Expand All @@ -74,6 +81,7 @@ impl<T, S> fmt::Debug for IndexSet<T, S>
}
}

#[cfg(any(has_std, feature = "force_std", test))]
impl<T> IndexSet<T> {
/// Create a new set. (Does not allocate.)
pub fn new() -> Self {
Expand Down
4 changes: 2 additions & 2 deletions src/util.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

use std::iter::Enumerate;
use std::mem::size_of;
use core::iter::Enumerate;
use core::mem::size_of;

pub fn third<A, B, C>(t: (A, B, C)) -> C { t.2 }

Expand Down