Skip to content
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
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@ license-file = "LICENSE.txt"
repository = "https://github.com/myrrlyn/bitvec"

[dependencies]

[features]
default = ["std"]
std = []
6 changes: 3 additions & 3 deletions src/bits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ storage of a primitive, and is the constraint for the storage type of a
`BitVec`.
!*/

use std::{
use core::{
cmp::Eq,
convert::From,
default::Default,
Expand Down Expand Up @@ -80,7 +80,7 @@ pub trait Bits:
const MASK: u8 = Self::WIDTH - 1;

/// The maximum number of this type that can be held in a `BitVec`.
const MAX_ELT: usize = std::usize::MAX >> Self::BITS;
const MAX_ELT: usize = core::usize::MAX >> Self::BITS;

/// Set a specific bit in an element to a given value.
fn set(&mut self, place: u8, value: bool) {
Expand Down Expand Up @@ -114,7 +114,7 @@ pub trait Bits:
/// Joins a `usize` element cursor and `u8` bit cursor into a single
/// `usize` cursor.
fn join(elt: usize, bit: u8) -> usize {
assert!(elt <= std::usize::MAX >> Self::BITS, "Element count out of range!");
assert!(elt <= core::usize::MAX >> Self::BITS, "Element count out of range!");
assert!(bit <= Self::MASK, "Bit count out of range!");
(elt << Self::BITS) | bit as usize
}
Expand Down
9 changes: 9 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ iteration in both directions, bit shifts, and, of course, access to the
underlying storage as a slice.
!*/

#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(not(feature = "std"), feature(alloc))]

#[macro_use]
mod macros;

Expand All @@ -41,6 +44,12 @@ mod endian;
mod slice;
mod vec;

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

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

pub use crate::{
bits::Bits,
endian::{
Expand Down
30 changes: 15 additions & 15 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ macro_rules! bitvec {
}};

( __bv_impl__ $endian:ident , $bits:ty ; $element:expr; $rep:expr ) => {{
std::iter::repeat( $element as u8 > 0 )
core::iter::repeat( $element as u8 > 0 )
.take( $rep )
.collect ::< $crate :: BitVec < $endian , $bits > > ()
}};
Expand All @@ -99,20 +99,20 @@ macro_rules! bitvec {
macro_rules! __bitslice_shift {
( $( $t:ty ),+ ) => { $(
#[doc(hidden)]
impl<E: $crate :: Endian, T: $crate :: Bits> std::ops::ShlAssign< $t >
impl<E: $crate :: Endian, T: $crate :: Bits> core::ops::ShlAssign< $t >
for crate::BitSlice<E, T>
{
fn shl_assign(&mut self, shamt: $t ) {
std::ops::ShlAssign::<usize>::shl_assign(self, shamt as usize);
core::ops::ShlAssign::<usize>::shl_assign(self, shamt as usize);
}
}

#[doc(hidden)]
impl<E: $crate :: Endian, T: $crate :: Bits> std::ops::ShrAssign< $t >
impl<E: $crate :: Endian, T: $crate :: Bits> core::ops::ShrAssign< $t >
for crate::BitSlice<E, T>
{
fn shr_assign(&mut self, shamt: $t ) {
std::ops::ShrAssign::<usize>::shr_assign(self, shamt as usize);
core::ops::ShrAssign::<usize>::shr_assign(self, shamt as usize);
}
}
)+ };
Expand All @@ -122,42 +122,42 @@ macro_rules! __bitslice_shift {
macro_rules! __bitvec_shift {
( $( $t:ty ),+ ) => { $(
#[doc(hidden)]
impl<E: $crate :: Endian, T: $crate :: Bits> std::ops::Shl< $t >
impl<E: $crate :: Endian, T: $crate :: Bits> core::ops::Shl< $t >
for $crate :: BitVec<E, T>
{
type Output = <Self as std::ops::Shl<usize>>::Output;
type Output = <Self as core::ops::Shl<usize>>::Output;

fn shl(self, shamt: $t ) -> Self::Output {
std::ops::Shl::<usize>::shl(self, shamt as usize)
core::ops::Shl::<usize>::shl(self, shamt as usize)
}
}

#[doc(hidden)]
impl<E: $crate :: Endian, T: $crate :: Bits> std::ops::ShlAssign< $t >
impl<E: $crate :: Endian, T: $crate :: Bits> core::ops::ShlAssign< $t >
for $crate :: BitVec<E, T>
{
fn shl_assign(&mut self, shamt: $t ) {
std::ops::ShlAssign::<usize>::shl_assign(self, shamt as usize)
core::ops::ShlAssign::<usize>::shl_assign(self, shamt as usize)
}
}

#[doc(hidden)]
impl<E: $crate :: Endian, T: $crate :: Bits> std::ops::Shr< $t >
impl<E: $crate :: Endian, T: $crate :: Bits> core::ops::Shr< $t >
for $crate :: BitVec<E, T>
{
type Output = <Self as std::ops::Shr<usize>>::Output;
type Output = <Self as core::ops::Shr<usize>>::Output;

fn shr(self, shamt: $t ) -> Self::Output {
std::ops::Shr::<usize>::shr(self, shamt as usize)
core::ops::Shr::<usize>::shr(self, shamt as usize)
}
}

#[doc(hidden)]
impl<E: $crate :: Endian, T: $crate :: Bits> std::ops::ShrAssign< $t >
impl<E: $crate :: Endian, T: $crate :: Bits> core::ops::ShrAssign< $t >
for $crate :: BitVec<E, T>
{
fn shr_assign(&mut self, shamt: $t ) {
std::ops::ShrAssign::<usize>::shr_assign(self, shamt as usize)
core::ops::ShrAssign::<usize>::shr_assign(self, shamt as usize)
}
}
)+ };
Expand Down
21 changes: 15 additions & 6 deletions src/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ reference or mutable reference, and has the advantage that now it can be a
count bits using `.into()`.
!*/

use std::{
borrow::ToOwned,
use core::{
cmp::{
Eq,
Ord,
Expand Down Expand Up @@ -82,6 +81,12 @@ use std::{
slice,
};

#[cfg(feature = "std")]
use std::borrow::ToOwned;

#[cfg(not(feature = "std"))]
use alloc::borrow::ToOwned;

/** A compact slice of bits, whose cursor and storage type can be customized.

`BitSlice` is a newtype wrapper over `[T]`, and as such can only be held by
Expand Down Expand Up @@ -556,7 +561,11 @@ where E: crate::Endian, T: crate::Bits {

/// Formats a partial element of the data slice.
pub(crate) fn fmt_bits(fmt: &mut Formatter, elt: &T, bits: u8) -> fmt::Result {
use std::fmt::Write;
use core::fmt::Write;

#[cfg(not(feature = "std"))]
use alloc::string::String;

let mut out = String::with_capacity(bits as usize);
for bit in 0 .. bits {
let cur = E::curr::<T>(bit);
Expand Down Expand Up @@ -927,7 +936,7 @@ where E: crate::Endian, T: crate::Bits {
/// assert_eq!(numr, &nums[2] as &BitSlice);
/// ```
fn add_assign(&mut self, addend: &'a BitSlice<E, T>) {
use std::iter::repeat;
use core::iter::repeat;
// zero-extend the addend if it’s shorter than self
let mut addend_iter = addend.into_iter().rev().chain(repeat(false));
let mut c = false;
Expand Down Expand Up @@ -962,7 +971,7 @@ where E: crate::Endian, T: crate::Bits, I: IntoIterator<Item=bool> {
/// assert_eq!("000100", &format!("{}", lhs));
/// ```
fn bitand_assign(&mut self, rhs: I) {
use std::iter::repeat;
use core::iter::repeat;
for (idx, other) in (0 .. self.len()).zip(rhs.into_iter().chain(repeat(false))) {
let val = self.get(idx) & other;
self.set(idx, val);
Expand Down Expand Up @@ -1011,7 +1020,7 @@ where E: crate::Endian, T: crate::Bits, I: IntoIterator<Item=bool> {
/// assert_eq!("011001", &format!("{}", lhs));
/// ```
fn bitxor_assign(&mut self, rhs: I) {
use std::iter::repeat;
use core::iter::repeat;
for (idx, other) in (0 .. self.len()).zip(rhs.into_iter().chain(repeat(false))) {
let val = self.get(idx) ^ other;
self.set(idx, val);
Expand Down
14 changes: 10 additions & 4 deletions src/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ The `BitSlice` module discusses the design decisions for the separation between
slice and vector types.
!*/

use std::{
use core::{
borrow::{
Borrow,
BorrowMut,
Expand Down Expand Up @@ -71,6 +71,12 @@ use std::{
ptr,
};

#[cfg(feature = "std")]
use std::borrow::ToOwned;

#[cfg(not(feature = "std"))]
use alloc::{borrow::ToOwned, boxed::Box, vec::Vec};

/** A compact `Vec` of bits, whose cursor and storage type can be customized.

`BitVec` is a newtype wrapper over `Vec`, and as such is exactly three words in
Expand Down Expand Up @@ -174,7 +180,7 @@ where E: crate::Endian, T: crate::Bits {
/// assert!(bv[0]);
/// ```
pub fn push(&mut self, value: bool) {
assert!(self.len() < std::usize::MAX, "Vector will overflow!");
assert!(self.len() < core::usize::MAX, "Vector will overflow!");
let bit = self.bits();
// Get a cursor to the bit that matches the semantic count.
let cursor = E::curr::<T>(bit);
Expand Down Expand Up @@ -1061,7 +1067,7 @@ where E: crate::Endian, T: crate::Bits {
/// assert_eq!(a, bitvec![1, 0, 0, 0, 0]);
/// ```
fn add_assign(&mut self, mut addend: Self) {
use std::iter::repeat;
use core::iter::repeat;
// If the other vec is longer, swap them and try again.
if addend.len() > self.len() {
mem::swap(self, &mut addend);
Expand Down Expand Up @@ -1579,7 +1585,7 @@ where E: crate::Endian, T: crate::Bits {
let buf = self.as_mut();
let ptr = buf.as_mut_ptr();
let len = buf.len();
unsafe { std::ptr::write_bytes(ptr, 0, len); }
unsafe { core::ptr::write_bytes(ptr, 0, len); }
return;
}
for idx in shamt .. len {
Expand Down