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

feat: support no_std #266

Merged
merged 8 commits into from
Feb 13, 2024
Merged
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
13 changes: 8 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,19 @@ edition = "2021"
license = "MIT OR Apache-2.0"

[dependencies]
bytemuck = "1.7.3"
byteorder = "1.4.3"
serde = { version = "1.0.139", optional = true }
bytemuck = { version = "1.14.3", optional = true }
byteorder = { version = "1.5.0", optional = true }
serde = { version = "1.0.196", optional = true }

[features]
default = ["std"]
serde = ["dep:serde", "std"]
simd = []
std = ["dep:bytemuck", "dep:byteorder"]

[dev-dependencies]
proptest = "1.2.0"
serde_json = "1.0.85"
proptest = "1.4.0"
serde_json = "1.0.113"
bincode = "1.3.3"

[profile.test]
Expand Down
10 changes: 6 additions & 4 deletions src/bitmap/arbitrary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ mod test {
use crate::bitmap::container::Container;
use crate::bitmap::store::{ArrayStore, BitmapStore, Store};
use crate::RoaringBitmap;
use alloc::boxed::Box;
use alloc::vec::Vec;
use core::fmt::{Debug, Formatter};
use proptest::bits::{BitSetLike, BitSetStrategy, SampledBitSetStrategy};
use proptest::collection::{vec, SizeRange};
use proptest::prelude::*;
use std::fmt::{Debug, Formatter};

impl Debug for BitmapStore {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
if self.len() < 16 {
write!(f, "BitmapStore<{:?}>", self.iter().collect::<Vec<u16>>())
} else {
Expand Down Expand Up @@ -82,7 +84,7 @@ mod test {
}

impl Debug for ArrayStore {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
if self.len() < 16 {
write!(f, "ArrayStore<{:?}>", self.as_slice())
} else {
Expand Down Expand Up @@ -151,7 +153,7 @@ mod test {
}

impl Debug for Store {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
match self {
Store::Array(a) => write!(f, "Store({:?})", a),
Store::Bitmap(b) => write!(f, "Store({:?})", b),
Expand Down
6 changes: 3 additions & 3 deletions src/bitmap/cmp.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::borrow::Borrow;
use std::cmp::Ordering;
use std::iter::Peekable;
use core::borrow::Borrow;
use core::cmp::Ordering;
use core::iter::Peekable;

use super::container::Container;
use crate::RoaringBitmap;
Expand Down
6 changes: 4 additions & 2 deletions src/bitmap/container.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use std::fmt;
use std::ops::{
use core::fmt;
use core::ops::{
BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, RangeInclusive, Sub, SubAssign,
};

use alloc::vec::Vec;

use super::store::{self, Store};
use super::util;

Expand Down
4 changes: 3 additions & 1 deletion src/bitmap/fmt.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use std::fmt;
use core::fmt;

use alloc::vec::Vec;

use crate::RoaringBitmap;

Expand Down
6 changes: 4 additions & 2 deletions src/bitmap/inherent.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::cmp::Ordering;
use std::ops::RangeBounds;
use core::cmp::Ordering;
use core::ops::RangeBounds;

use alloc::vec::Vec;

use crate::RoaringBitmap;

Expand Down
7 changes: 4 additions & 3 deletions src/bitmap/iter.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::iter::{self, FromIterator};
use std::{slice, vec};
use alloc::vec::{self, Vec};
use core::iter::{self, FromIterator};
use core::slice;

use super::container::Container;
use crate::{NonSortedIntegers, RoaringBitmap};
Expand Down Expand Up @@ -99,7 +100,7 @@ impl RoaringBitmap {
///
/// ```rust
/// use roaring::RoaringBitmap;
/// use std::iter::FromIterator;
/// use core::iter::FromIterator;
///
/// let bitmap = (1..3).collect::<RoaringBitmap>();
/// let mut iter = bitmap.iter();
Expand Down
3 changes: 3 additions & 0 deletions src/bitmap/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ mod iter;
mod ops;
#[cfg(feature = "serde")]
mod serde;
#[cfg(feature = "std")]
mod serialization;

use alloc::vec::Vec;

use self::cmp::Pairs;
pub use self::iter::IntoIter;
pub use self::iter::Iter;
Expand Down
5 changes: 3 additions & 2 deletions src/bitmap/multiops.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use std::{
borrow::Cow,
use core::{
cmp::Reverse,
convert::Infallible,
mem,
ops::{BitOrAssign, BitXorAssign},
};

use alloc::{borrow::Cow, vec::Vec};

use crate::{MultiOps, RoaringBitmap};

use super::{container::Container, store::Store};
Expand Down
8 changes: 5 additions & 3 deletions src/bitmap/ops.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::mem;
use std::ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Sub, SubAssign};
use core::mem;
use core::ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Sub, SubAssign};

use alloc::vec::Vec;

use crate::bitmap::container::Container;
use crate::bitmap::Pairs;
Expand Down Expand Up @@ -440,8 +442,8 @@ impl BitXorAssign<&RoaringBitmap> for RoaringBitmap {
#[cfg(test)]
mod test {
use crate::{MultiOps, RoaringBitmap};
use core::convert::Infallible;
use proptest::prelude::*;
use std::convert::Infallible;

// fast count tests
proptest! {
Expand Down
2 changes: 1 addition & 1 deletion src/bitmap/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ impl<'de> Deserialize<'de> for RoaringBitmap {
impl<'de> Visitor<'de> for BitmapVisitor {
type Value = RoaringBitmap;

fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
fn expecting(&self, formatter: &mut core::fmt::Formatter) -> core::fmt::Result {
formatter.write_str("roaring bitmap")
}

Expand Down
4 changes: 2 additions & 2 deletions src/bitmap/serialization.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use bytemuck::cast_slice_mut;
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
use std::convert::{Infallible, TryFrom};
use core::convert::{Infallible, TryFrom};
use core::ops::RangeInclusive;
use std::error::Error;
use std::io;
use std::ops::RangeInclusive;

use crate::bitmap::container::{Container, ARRAY_LIMIT};
use crate::bitmap::store::{ArrayStore, BitmapStore, Store, BITMAP_LENGTH};
Expand Down
20 changes: 12 additions & 8 deletions src/bitmap/store/array_store/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ mod scalar;
mod vector;
mod visitor;

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

use crate::bitmap::store::array_store::visitor::{CardinalityCounter, VecWriter};
use std::cmp::Ordering;
use std::cmp::Ordering::*;
use std::convert::{TryFrom, TryInto};
use std::fmt::{Display, Formatter};
use std::ops::{BitAnd, BitAndAssign, BitOr, BitXor, RangeInclusive, Sub, SubAssign};
use core::cmp::Ordering;
use core::cmp::Ordering::*;
use core::convert::TryFrom;
use core::fmt::{Display, Formatter};
use core::ops::{BitAnd, BitAndAssign, BitOr, BitXor, RangeInclusive, Sub, SubAssign};

use super::bitmap_store::{bit, key, BitmapStore, BITMAP_LENGTH};

Expand Down Expand Up @@ -214,11 +217,11 @@ impl ArrayStore {
self.vec.get(n as usize).cloned()
}

pub fn iter(&self) -> std::slice::Iter<u16> {
pub fn iter(&self) -> core::slice::Iter<u16> {
self.vec.iter()
}

pub fn into_iter(self) -> std::vec::IntoIter<u16> {
pub fn into_iter(self) -> alloc::vec::IntoIter<u16> {
self.vec.into_iter()
}

Expand Down Expand Up @@ -263,7 +266,7 @@ pub enum ErrorKind {
}

impl Display for Error {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
match self.kind {
ErrorKind::Duplicate => {
write!(f, "Duplicate element found at index: {}", self.index)
Expand All @@ -275,6 +278,7 @@ impl Display for Error {
}
}

#[cfg(feature = "std")]
impl std::error::Error for Error {}

impl TryFrom<Vec<u16>> for ArrayStore {
Expand Down
2 changes: 1 addition & 1 deletion src/bitmap/store/array_store/scalar.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Scalar arithmetic binary set operations on `ArrayStore`'s inner types

use crate::bitmap::store::array_store::visitor::BinaryOperationVisitor;
use std::cmp::Ordering::*;
use core::cmp::Ordering::*;

#[inline]
pub fn or(lhs: &[u16], rhs: &[u16], visitor: &mut impl BinaryOperationVisitor) {
Expand Down
4 changes: 2 additions & 2 deletions src/bitmap/store/array_store/vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ where
U: SimdElement + PartialOrd,
LaneCount<LANES>: SupportedLaneCount,
{
unsafe { std::ptr::read_unaligned(src as *const _ as *const Simd<U, LANES>) }
unsafe { core::ptr::read_unaligned(src as *const _ as *const Simd<U, LANES>) }
}

/// write `v` to slice `out`
Expand All @@ -416,7 +416,7 @@ where
U: SimdElement + PartialOrd,
LaneCount<LANES>: SupportedLaneCount,
{
unsafe { std::ptr::write_unaligned(out as *mut _ as *mut Simd<U, LANES>, v) }
unsafe { core::ptr::write_unaligned(out as *mut _ as *mut Simd<U, LANES>, v) }
}

/// Compare all lanes in `a` to all lanes in `b`
Expand Down
2 changes: 2 additions & 0 deletions src/bitmap/store/array_store/visitor.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use alloc::vec::Vec;

#[cfg(feature = "simd")]
use crate::bitmap::store::array_store::vector::swizzle_to_front;

Expand Down
14 changes: 9 additions & 5 deletions src/bitmap/store/bitmap_store.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use std::borrow::Borrow;
use std::cmp::Ordering;
use std::fmt::{Display, Formatter};
use std::ops::{BitAndAssign, BitOrAssign, BitXorAssign, RangeInclusive, SubAssign};
use core::borrow::Borrow;
use core::cmp::Ordering;
use core::fmt::{Display, Formatter};
use core::ops::{BitAndAssign, BitOrAssign, BitXorAssign, RangeInclusive, SubAssign};

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

use super::ArrayStore;

Expand Down Expand Up @@ -382,7 +385,7 @@ pub enum ErrorKind {
}

impl Display for Error {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
match self.kind {
ErrorKind::Cardinality { expected, actual } => {
write!(f, "Expected cardinality was {} but was {}", expected, actual)
Expand All @@ -391,6 +394,7 @@ impl Display for Error {
}
}

#[cfg(feature = "std")]
impl std::error::Error for Error {}

pub struct BitmapIter<B: Borrow<[u64; BITMAP_LENGTH]>> {
Expand Down
7 changes: 4 additions & 3 deletions src/bitmap/store/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
mod array_store;
mod bitmap_store;

use std::mem;
use std::ops::{
use alloc::{boxed::Box, vec};
use core::mem;
use core::ops::{
BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, RangeInclusive, Sub, SubAssign,
};
use std::{slice, vec};
use core::slice;

pub use self::bitmap_store::BITMAP_LENGTH;
use self::Store::{Array, Bitmap};
Expand Down
4 changes: 2 additions & 2 deletions src/bitmap/util.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::ops::{Bound, RangeBounds, RangeInclusive};
use core::ops::{Bound, RangeBounds, RangeInclusive};

/// Returns the container key and the index
/// in this container for a given integer.
Expand Down Expand Up @@ -38,7 +38,7 @@ where
#[cfg(test)]
mod test {
use super::{convert_range_to_inclusive, join, split};
use std::ops::Bound;
use core::ops::Bound;

#[test]
fn test_split_u32() {
Expand Down
11 changes: 8 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,20 @@
//! [roaring-java]: https://github.com/lemire/RoaringBitmap
//! [roaring-paper]: https://arxiv.org/pdf/1402.6407v4

#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(feature = "simd", feature(portable_simd))]
#![warn(missing_docs)]
#![warn(unsafe_op_in_unsafe_fn)]
#![warn(variant_size_differences)]
#![allow(unknown_lints)] // For clippy

#[cfg(feature = "std")]
extern crate byteorder;

use std::error::Error;
use std::fmt;
#[macro_use]
extern crate alloc;

use core::fmt;

/// A compressed bitmap using the [Roaring bitmap compression scheme](https://roaringbitmap.org/).
pub mod bitmap;
Expand Down Expand Up @@ -46,7 +50,8 @@ impl fmt::Display for NonSortedIntegers {
}
}

impl Error for NonSortedIntegers {}
#[cfg(feature = "std")]
impl std::error::Error for NonSortedIntegers {}

/// A [`Iterator::collect`] blanket implementation that provides extra methods for [`RoaringBitmap`]
/// and [`RoaringTreemap`].
Expand Down
4 changes: 2 additions & 2 deletions src/treemap/cmp.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::collections::btree_map;
use std::iter::Peekable;
use alloc::collections::btree_map;
use core::iter::Peekable;

use crate::RoaringBitmap;
use crate::RoaringTreemap;
Expand Down
4 changes: 3 additions & 1 deletion src/treemap/fmt.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use std::fmt;
use core::fmt;

use alloc::vec::Vec;

use crate::RoaringTreemap;

Expand Down
7 changes: 4 additions & 3 deletions src/treemap/inherent.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::collections::btree_map::{BTreeMap, Entry};
use std::iter;
use std::ops::RangeBounds;
use alloc::collections::btree_map::{BTreeMap, Entry};
use alloc::vec::Vec;
use core::iter;
use core::ops::RangeBounds;

use crate::RoaringBitmap;
use crate::RoaringTreemap;
Expand Down
Loading
Loading