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 #193

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ travis-ci = { repository = "bodil/im-rs" }
version_check = "0.9"

[features]
default = ["no_std"]
debug = []
no_std = ["ahash", "hashbrown"]

[dependencies]
typenum = "1.12"
Expand All @@ -47,6 +49,8 @@ serde = { version = "1", optional = true }
rayon = { version = "1", optional = true }
refpool = { version = "0.4", optional = true }
arbitrary = { version = "0.4", optional = true }
ahash = { version = "0.7.4", optional = true }
hashbrown = { version = "0.11.2", optional = true }

[dev-dependencies]
proptest = "0.10"
Expand Down
3 changes: 3 additions & 0 deletions rc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ travis-ci = { repository = "bodil/im-rs" }
[features]
pool = ["refpool", "sized-chunks/refpool"]
debug = []
no_std = ["ahash", "hashbrown"]

[build-dependencies]
version_check = "0.9"
Expand All @@ -41,6 +42,8 @@ serde = { version = "1", optional = true }
rayon = { version = "1", optional = true }
refpool = { version = "0.4", optional = true }
arbitrary = { version = "0.4", optional = true }
ahash = { version = "0.7.4", optional = true }
hashbrown = { version = "0.11.2", optional = true }

[dev-dependencies]
proptest = "0.10"
Expand Down
20 changes: 10 additions & 10 deletions src/fakepool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

#![allow(dead_code)]

use std::marker::PhantomData;
use std::ops::Deref;
use std::rc::Rc as RRc;
use std::sync::Arc as RArc;
use core::marker::PhantomData;
use core::ops::Deref;
use alloc::rc::Rc as RRc;
use alloc::sync::Arc as RArc;

use crate::nodes::chunk::Chunk;

Expand Down Expand Up @@ -112,12 +112,12 @@ where

impl<A> Eq for Rc<A> where A: Eq {}

impl<A> std::fmt::Debug for Rc<A>
impl<A> core::fmt::Debug for Rc<A>
where
A: std::fmt::Debug,
A: core::fmt::Debug,
{
#[inline(always)]
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> Result<(), core::fmt::Error> {
self.0.fmt(f)
}
}
Expand Down Expand Up @@ -197,12 +197,12 @@ where

impl<A> Eq for Arc<A> where A: Eq {}

impl<A> std::fmt::Debug for Arc<A>
impl<A> core::fmt::Debug for Arc<A>
where
A: std::fmt::Debug,
A: core::fmt::Debug,
{
#[inline(always)]
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> Result<(), core::fmt::Error> {
self.0.fmt(f)
}
}
33 changes: 23 additions & 10 deletions src/hash/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,25 @@
//! [std::hash::Hash]: https://doc.rust-lang.org/std/hash/trait.Hash.html
//! [std::collections::hash_map::RandomState]: https://doc.rust-lang.org/std/collections/hash_map/struct.RandomState.html

use std::borrow::Borrow;
use std::cmp::Ordering;
use core::borrow::Borrow;
use core::cmp::Ordering;
#[cfg(not(feature = "no_std"))]
use std::collections;
#[cfg(feature = "no_std")]
use hashbrown as collections;
#[cfg(feature = "no_std")]
use ahash::RandomState;
#[cfg(not(feature = "no_std"))]
use std::collections::hash_map::RandomState;
use std::fmt::{Debug, Error, Formatter};
use std::hash::{BuildHasher, Hash, Hasher};
use std::iter::{FromIterator, FusedIterator, Sum};
use std::mem;
use std::ops::{Add, Index, IndexMut};
use core::fmt::{Debug, Error, Formatter};
use core::hash::{BuildHasher, Hash, Hasher};
use core::iter::{FromIterator, FusedIterator, Sum};
use core::mem;
use core::ops::{Add, Index, IndexMut};
use alloc::{vec::Vec, vec};
use alloc::borrow::ToOwned;
use alloc::string::String;
use alloc::format;

use crate::nodes::hamt::{
hash_key, Drain as NodeDrain, HashBits, HashValue, Iter as NodeIter, IterMut as NodeIterMut,
Expand Down Expand Up @@ -220,7 +230,7 @@ impl<K, V, S> HashMap<K, V, S> {
///
/// Time: O(1)
pub fn ptr_eq(&self, other: &Self) -> bool {
std::ptr::eq(self, other) || PoolRef::ptr_eq(&self.root, &other.root)
core::ptr::eq(self, other) || PoolRef::ptr_eq(&self.root, &other.root)
}

/// Get a reference to the memory pool used by this map.
Expand Down Expand Up @@ -1779,7 +1789,8 @@ where
S: BuildHasher,
{
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
let mut keys = collections::BTreeSet::new();
#[cfg(not(feature = "no_std"))] let mut keys = collections::BTreeSet::new();
#[cfg(feature = "no_std")] let mut keys = collections::HashSet::new();
keys.extend(self.keys());
let mut d = f.debug_map();
for key in keys {
Expand Down Expand Up @@ -2046,6 +2057,7 @@ where
}
}

#[cfg(not(feature = "no_std"))]
impl<K, V, S> From<collections::BTreeMap<K, V>> for HashMap<K, V, S>
where
K: Hash + Eq + Clone,
Expand All @@ -2057,6 +2069,7 @@ where
}
}

#[cfg(not(feature = "no_std"))]
impl<'a, K, V, S> From<&'a collections::BTreeMap<K, V>> for HashMap<K, V, S>
where
K: Hash + Eq + Clone,
Expand Down Expand Up @@ -2105,7 +2118,7 @@ mod test {
use crate::test::LolHasher;
use ::proptest::num::{i16, usize};
use ::proptest::{collection, proptest};
use std::hash::BuildHasherDefault;
use core::hash::BuildHasherDefault;

#[test]
fn safe_mutation() {
Expand Down
30 changes: 21 additions & 9 deletions src/hash/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,24 @@
//! [std::hash::Hash]: https://doc.rust-lang.org/std/hash/trait.Hash.html
//! [std::collections::hash_map::RandomState]: https://doc.rust-lang.org/std/collections/hash_map/struct.RandomState.html

use std::borrow::Borrow;
use std::cmp::Ordering;
use core::borrow::Borrow;
use core::cmp::Ordering;
#[cfg(feature = "no_std")]
use ahash::RandomState;
#[cfg(not(feature = "no_std"))]
use std::collections::hash_map::RandomState;
#[cfg(feature = "no_std")]
use hashbrown as collections;
#[cfg(not(feature = "no_std"))]
use std::collections::{self, BTreeSet};
use std::fmt::{Debug, Error, Formatter};
use std::hash::{BuildHasher, Hash, Hasher};
use std::iter::FusedIterator;
use std::iter::{FromIterator, IntoIterator, Sum};
use std::ops::{Add, Deref, Mul};
use core::fmt::{Debug, Error, Formatter};
use core::hash::{BuildHasher, Hash, Hasher};
use core::iter::FusedIterator;
use core::iter::{FromIterator, IntoIterator, Sum};
use core::ops::{Add, Deref, Mul};
use alloc::{vec::Vec, vec};
use alloc::borrow::ToOwned;
use alloc::string::String;

use crate::nodes::hamt::{hash_key, Drain as NodeDrain, HashValue, Iter as NodeIter, Node};
use crate::ordset::OrdSet;
Expand Down Expand Up @@ -217,7 +226,7 @@ impl<A, S> HashSet<A, S> {
///
/// Time: O(1)
pub fn ptr_eq(&self, other: &Self) -> bool {
std::ptr::eq(self, other) || PoolRef::ptr_eq(&self.root, &other.root)
core::ptr::eq(self, other) || PoolRef::ptr_eq(&self.root, &other.root)
}

/// Get a reference to the memory pool used by this set.
Expand Down Expand Up @@ -1015,6 +1024,7 @@ where
}
}

#[cfg(not(feature = "no_std"))]
impl<'a, A, S> From<&'a BTreeSet<A>> for HashSet<A, S>
where
A: Hash + Eq + Clone,
Expand Down Expand Up @@ -1058,12 +1068,14 @@ pub mod proptest {

#[cfg(test)]
mod test {
extern crate std;
use std::println;
use super::proptest::*;
use super::*;
use crate::test::LolHasher;
use ::proptest::num::i16;
use ::proptest::proptest;
use std::hash::BuildHasherDefault;
use core::hash::BuildHasherDefault;

#[test]
fn insert_failing() {
Expand Down
2 changes: 1 addition & 1 deletion src/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ where
F: Fn(S) -> Option<(A, S)>,
{
let mut value = Some(value);
std::iter::from_fn(move || {
core::iter::from_fn(move || {
f(value.take().unwrap()).map(|(next, state)| {
value = Some(state);
next
Expand Down
5 changes: 5 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,11 +335,16 @@
//! [b-tree]: https://en.wikipedia.org/wiki/B-tree
//! [cons-list]: https://en.wikipedia.org/wiki/Cons#Lists

#![no_std]
#![forbid(rust_2018_idioms)]
#![deny(unsafe_code, nonstandard_style)]
#![warn(unreachable_pub, missing_docs)]
#![cfg_attr(has_specialisation, feature(specialization))]

extern crate alloc;
#[cfg(not(feature = "no_std"))]
extern crate std;

#[cfg(test)]
#[macro_use]
extern crate pretty_assertions;
Expand Down
11 changes: 6 additions & 5 deletions src/nodes/btree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

use std::borrow::Borrow;
use std::cmp::Ordering;
use std::mem;
use std::ops::{Bound, RangeBounds};
use core::borrow::Borrow;
use core::cmp::Ordering;
use core::mem;
use core::ops::{Bound, RangeBounds};
use alloc::{vec::Vec, vec};

use sized_chunks::Chunk;
use typenum::{Add1, Unsigned};
Expand Down Expand Up @@ -1303,7 +1304,7 @@ where
},
(Some(old), Some(new)) => match (old, new) {
(IterItem::Consider(old), IterItem::Consider(new)) => {
if !std::ptr::eq(old, new) {
if !core::ptr::eq(old, new) {
match old.keys[0].cmp_values(&new.keys[0]) {
Ordering::Less => {
Self::push(&mut self.old_stack, &old);
Expand Down
13 changes: 7 additions & 6 deletions src/nodes/hamt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

use std::borrow::Borrow;
use std::fmt;
use std::hash::{BuildHasher, Hash, Hasher};
use std::iter::FusedIterator;
use std::slice::{Iter as SliceIter, IterMut as SliceIterMut};
use std::{mem, ptr};
use core::borrow::Borrow;
use core::fmt;
use core::hash::{BuildHasher, Hash, Hasher};
use core::iter::FusedIterator;
use core::slice::{Iter as SliceIter, IterMut as SliceIterMut};
use core::{mem, ptr};
use alloc::{vec::Vec, vec};

use bitmaps::Bits;
use sized_chunks::sparse_chunk::{Iter as ChunkIter, IterMut as ChunkIterMut, SparseChunk};
Expand Down
6 changes: 4 additions & 2 deletions src/nodes/rrb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

use std::mem::replace;
use std::ops::Range;
use core::mem::replace;
use core::ops::Range;
use alloc::format;
use alloc::{vec::Vec, vec};

use crate::nodes::chunk::{Chunk, CHUNK_SIZE};
use crate::util::{
Expand Down
33 changes: 20 additions & 13 deletions src/ord/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,21 @@
//! [hashmap::HashMap]: ../hashmap/struct.HashMap.html
//! [std::cmp::Ord]: https://doc.rust-lang.org/std/cmp/trait.Ord.html

use std::borrow::Borrow;
use std::cmp::Ordering;
use core::borrow::Borrow;
use core::cmp::Ordering;
#[cfg(feature = "no_std")]
use hashbrown as collections;
#[cfg(not(feature = "no_std"))]
use std::collections;
use std::fmt::{Debug, Error, Formatter};
use std::hash::{BuildHasher, Hash, Hasher};
use std::iter::{FromIterator, Iterator, Sum};
use std::mem;
use std::ops::{Add, Index, IndexMut, RangeBounds};
use core::fmt::{Debug, Error, Formatter};
use core::hash::{BuildHasher, Hash, Hasher};
use core::iter::{FromIterator, Iterator, Sum};
use core::mem;
use core::ops::{Add, Index, IndexMut, RangeBounds};
use alloc::{vec::Vec, vec};
use alloc::borrow::ToOwned;
use alloc::string::String;
use alloc::format;

use crate::hashmap::HashMap;
use crate::nodes::btree::{BTreeValue, Insert, Node, Remove};
Expand Down Expand Up @@ -251,7 +258,7 @@ impl<K, V> OrdMap<K, V> {
///
/// Time: O(1)
pub fn ptr_eq(&self, other: &Self) -> bool {
std::ptr::eq(self, other) || PoolRef::ptr_eq(&self.root, &other.root)
core::ptr::eq(self, other) || PoolRef::ptr_eq(&self.root, &other.root)
}

/// Get the size of a map.
Expand Down Expand Up @@ -2117,17 +2124,17 @@ where
}
}

impl<K: Ord, V, RK, RV> From<collections::BTreeMap<RK, RV>> for OrdMap<K, V>
impl<K: Ord, V, RK, RV> From<alloc::collections::BTreeMap<RK, RV>> for OrdMap<K, V>
where
K: Ord + Clone + From<RK>,
V: Clone + From<RV>,
{
fn from(m: collections::BTreeMap<RK, RV>) -> OrdMap<K, V> {
fn from(m: alloc::collections::BTreeMap<RK, RV>) -> OrdMap<K, V> {
m.into_iter().collect()
}
}

impl<'a, K: Ord, V, RK, RV, OK, OV> From<&'a collections::BTreeMap<RK, RV>> for OrdMap<K, V>
impl<'a, K: Ord, V, RK, RV, OK, OV> From<&'a alloc::collections::BTreeMap<RK, RV>> for OrdMap<K, V>
where
K: Ord + Clone + From<OK>,
V: Clone + From<OV>,
Expand All @@ -2136,7 +2143,7 @@ where
RK: Ord + ToOwned<Owned = OK>,
RV: ToOwned<Owned = OV>,
{
fn from(m: &'a collections::BTreeMap<RK, RV>) -> OrdMap<K, V> {
fn from(m: &'a alloc::collections::BTreeMap<RK, RV>) -> OrdMap<K, V> {
m.iter()
.map(|(k, v)| (k.to_owned(), v.to_owned()))
.collect()
Expand Down Expand Up @@ -2443,7 +2450,7 @@ mod test {
ref ops in collection::vec((bool::ANY, usize::ANY, usize::ANY), 1..1000)
) {
let mut map = input.clone();
let mut tree: collections::BTreeMap<usize, usize> = input.iter().map(|(k, v)| (*k, *v)).collect();
let mut tree: alloc::collections::BTreeMap<usize, usize> = input.iter().map(|(k, v)| (*k, *v)).collect();
for (ins, key, val) in ops {
if *ins {
tree.insert(*key, *val);
Expand Down
Loading