From 5ad0e472523af32f11c737d9023b94e921f187bd Mon Sep 17 00:00:00 2001 From: gramar Date: Tue, 25 Oct 2022 20:45:41 +0200 Subject: [PATCH] Enable no_std --- Cargo.toml | 4 +++- src/entry.rs | 2 +- src/iter.rs | 24 ++++++++++++------------ src/lib.rs | 25 ++++++++++++++----------- src/page_impls.rs | 2 +- src/primitive.rs | 2 +- src/space.rs | 4 ++-- 7 files changed, 34 insertions(+), 29 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 55c0b5f..5f5cf5b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,10 +17,12 @@ panic = "unwind" strip=true [features] +default = ["std"] +std = ["serde/std"] # TODO: maybe add an FFI feature, to allow C projects to use it? idk if that's worth it really... [dependencies] -serde = {version = "1.0.116", features = ["derive"], optional = true} +serde = {version = "1.0.116", features = ["derive", "alloc"], optional = true} # TODO: optional smallvec feature: instead of heap-allocating the first page, it can be placed on the stack. diff --git a/src/entry.rs b/src/entry.rs index 459505c..0d02091 100644 --- a/src/entry.rs +++ b/src/entry.rs @@ -33,7 +33,7 @@ where K: Collapse /// Replace the held value with another, yielding the old one pub fn insert(&mut self, value: V) -> V { - std::mem::replace(&mut self.0.as_mut().unwrap().1, value) + core::mem::replace(&mut self.0.as_mut().unwrap().1, value) } /// Remove this entry from the `Map`, yielding the removed value pub fn remove(self) -> V diff --git a/src/iter.rs b/src/iter.rs index 909e94d..7ac302e 100644 --- a/src/iter.rs +++ b/src/iter.rs @@ -2,7 +2,7 @@ use super::*; /// An iterator over `Page`s -pub struct Pages<'a, K, V>(pub(crate) std::slice::Iter<'a, Page>); +pub struct Pages<'a, K, V>(pub(crate) core::slice::Iter<'a, Page>); impl<'a, K, V> Iterator for Pages<'a,K,V> { @@ -18,7 +18,7 @@ impl<'a, K, V> Iterator for Pages<'a,K,V> } /// A mutable iterator over `Page`s -pub struct PagesMut<'a, K, V>(pub(crate) std::slice::IterMut<'a, Page>); +pub struct PagesMut<'a, K, V>(pub(crate) core::slice::IterMut<'a, Page>); impl<'a, K, V> Iterator for PagesMut<'a,K,V> { @@ -34,10 +34,10 @@ impl<'a, K, V> Iterator for PagesMut<'a,K,V> } impl<'a, K, V> ExactSizeIterator for PagesMut<'a,K,V>{} -impl<'a, K, V> std::iter::FusedIterator for PagesMut<'a,K,V>{} +impl<'a, K, V> core::iter::FusedIterator for PagesMut<'a,K,V>{} /// An iterator over elements in a `Page`. -pub struct PageElements<'a, K, V>(pub(crate) std::slice::Iter<'a, Option<(K,V)>>); +pub struct PageElements<'a, K, V>(pub(crate) core::slice::Iter<'a, Option<(K,V)>>); impl<'a, K, V> Iterator for PageElements<'a,K,V> { @@ -57,10 +57,10 @@ impl<'a, K, V> Iterator for PageElements<'a,K,V> (0, self.0.size_hint().1) } } -impl<'a, K, V> std::iter::FusedIterator for PageElements<'a,K,V>{} +impl<'a, K, V> core::iter::FusedIterator for PageElements<'a,K,V>{} /// A mutable iterator over elements in a `Page`. -pub struct PageElementsMut<'a, K, V>(pub(crate) std::slice::IterMut<'a, Option<(K,V)>>); +pub struct PageElementsMut<'a, K, V>(pub(crate) core::slice::IterMut<'a, Option<(K,V)>>); impl<'a, K, V> Iterator for PageElementsMut<'a,K,V> { @@ -79,7 +79,7 @@ impl<'a, K, V> Iterator for PageElementsMut<'a,K,V> (0, self.0.size_hint().1) } } -impl<'a, K, V> std::iter::FusedIterator for PageElementsMut<'a,K,V>{} +impl<'a, K, V> core::iter::FusedIterator for PageElementsMut<'a,K,V>{} /// A consuming iterator over elements in a `Page`. pub struct IntoPageElements(pub(crate) [Option<(K,V)>; MAX], pub(crate) usize); @@ -104,7 +104,7 @@ impl Iterator for IntoPageElements (0, Some(self.0.len())) } } -impl std::iter::FusedIterator for IntoPageElements{} +impl core::iter::FusedIterator for IntoPageElements{} /// An iterator over entries in a `Map`. pub struct Iter<'a, K, V>(pub(crate) Option>, pub(crate) Pages<'a, K,V>); @@ -131,7 +131,7 @@ where K: Collapse (0, self.1.size_hint().1.map(|x| x * MAX)) } } -impl<'a, K: Collapse, V> std::iter::FusedIterator for Iter<'a, K,V>{} +impl<'a, K: Collapse, V> core::iter::FusedIterator for Iter<'a, K,V>{} /// A mutable iterator over entries in a `Map`. pub struct IterMut<'a, K, V>(pub(crate) Option>, pub(crate) PagesMut<'a, K,V>); @@ -158,10 +158,10 @@ where K: Collapse (0, self.1.size_hint().1.map(|x| x * MAX)) } } -impl<'a, K: Collapse, V> std::iter::FusedIterator for IterMut<'a, K,V>{} +impl<'a, K: Collapse, V> core::iter::FusedIterator for IterMut<'a, K,V>{} /// A consuming iterator over entries in a `Map`. -pub struct IntoIter(pub(crate) Option>, pub(crate) std::vec::IntoIter>); +pub struct IntoIter(pub(crate) Option>, pub(crate) vec::IntoIter>); impl Iterator for IntoIter where K: Collapse @@ -187,7 +187,7 @@ where K: Collapse } } -impl std::iter::FusedIterator for IntoIter{} +impl core::iter::FusedIterator for IntoIter{} #[cfg(test)] mod tests diff --git a/src/lib.rs b/src/lib.rs index 2f4b1a8..9924f47 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -26,16 +26,19 @@ //! //! ### When not to use //! Generally don't use this if your key would have a lot of collisions being represents in 8 bits, otherwise it might be a faster alternative to hash-based maps. You should check yourself before sticking with this crate instead of `std`'s vectorised map implementations. - +#![cfg_attr(not(test), no_std)] #![cfg_attr(nightly, feature(test))] #![cfg_attr(nightly, feature(drain_filter))] #![cfg_attr(nightly, feature(never_type))] #[cfg(nightly)] extern crate test; +extern crate alloc; const MAX: usize = 256; -use std::borrow::Borrow; +use alloc::vec; +use alloc::vec::Vec; +use core::borrow::Borrow; pub mod iter; use iter::*; @@ -138,7 +141,7 @@ where K: Collapse /// This is a count that iterates over all slots, if possible store it in a temporary instead of re-calling it many times. pub fn len(&self) -> usize { - self.0.iter().map(Option::as_ref).filter_map(std::convert::identity).count() + self.0.iter().map(Option::as_ref).filter_map(core::convert::identity).count() } /// An iterator over all entries currently in this page @@ -166,11 +169,11 @@ where K: Collapse fn replace(&mut self, k: K, v: V) -> Option<(K,V)> { - std::mem::replace(&mut self.0[usize::from(k.collapse())], Some((k,v))) + core::mem::replace(&mut self.0[usize::from(k.collapse())], Some((k,v))) } } -impl std::iter::FromIterator<(K, V)> for Map +impl core::iter::FromIterator<(K, V)> for Map { fn from_iter>(iter: I) -> Self { @@ -221,8 +224,8 @@ impl Map #[allow(dead_code)] // Used in test cases, but compiler still warns about it pub(crate) fn internal_size_bytes(&self) -> usize { - self.0.capacity() * std::mem::size_of::>() - //self.0.iter().map(std::mem::size_of_val).sum::() + self.0.capacity() * core::mem::size_of::>() + //self.0.iter().map(core::mem::size_of_val).sum::() } } @@ -432,7 +435,7 @@ where K: Collapse { match page.search_mut(&key) { Some((ref ok, ov)) if ok.eq(&key) => { - return Some(std::mem::replace(ov, value)); + return Some(core::mem::replace(ov, value)); }, empty @ None => { return empty.replace((key, value)) @@ -484,7 +487,7 @@ impl IntoIterator for Map } } -impl std::iter::Extend<(K,V)> for Map +impl core::iter::Extend<(K,V)> for Map { fn extend>(&mut self, iter: T) { // we can probably optimise this better, right? @@ -495,8 +498,8 @@ impl std::iter::Extend<(K,V)> for Map } } -use std::hash::{Hash, Hasher,}; -use std::ops::{Index, IndexMut}; +use core::hash::{Hash, Hasher,}; +use core::ops::{Index, IndexMut}; impl Collapse for T { diff --git a/src/page_impls.rs b/src/page_impls.rs index 9842a07..6ac4b0a 100644 --- a/src/page_impls.rs +++ b/src/page_impls.rs @@ -1,6 +1,6 @@ //! Because we can't #derive big arrays on stable smh use super::*; -use std::{ +use core::{ fmt::{self, Debug,}, hash, }; diff --git a/src/primitive.rs b/src/primitive.rs index d78220c..79e1385 100644 --- a/src/primitive.rs +++ b/src/primitive.rs @@ -6,7 +6,7 @@ //! //! If/when Rust gets specialisation, this will be unneeded. use super::*; -use std::num::*; +use core::num::*; /// Sealed trait allowing for wrapping primitive types with a more efficient implemntation for the `Collapse` trait. /// This should not be used for much directly, instead use the newtype shim `Primitive`. diff --git a/src/space.rs b/src/space.rs index 4b1bd18..2cb2548 100644 --- a/src/space.rs +++ b/src/space.rs @@ -12,12 +12,12 @@ use super::*; /// A set of only non-zero bytes. /// /// This type is entirely space efficient and will only ever allocate `256` bytes of memory. -pub type NonZeroByteSet = Set; +pub type NonZeroByteSet = Set; /// A set of non-zero signed 8-bit integers. /// /// This type is entirely space efficient and will only ever allocate `256` bytes of memory. -pub type NonZeroI8Set = Set; +pub type NonZeroI8Set = Set; /// A set of non-zero unsigned 8-bit integers. ///