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

Update BitVec to 1.0 #3

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ categories = ["parsing"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
bitvec = "0.22.3"
bitvec = "1.0.0"
nom = { version = "7.0.0", default-features = false }

[features]
Expand Down
97 changes: 51 additions & 46 deletions src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,46 +10,51 @@ use crate::lib::std::str::Chars;*/

use crate::BSlice;

impl<'a, O, T> InputLength for BSlice<'a, O, T>
impl<'a, T, O> InputLength for BSlice<'a, T, O>
where
O: BitOrder,
T: 'a + BitStore,
O: BitOrder,
{
#[inline]
fn input_len(&self) -> usize {
self.0.len()
}
}

impl<'a, 'b, O, T> InputLength for &'b BSlice<'a, O, T>
impl<'a, 'b, T, O> InputLength for &'b BSlice<'a, T, O>
where
O: BitOrder,
T: 'a + BitStore,
O: BitOrder,
{
#[inline]
fn input_len(&self) -> usize {
self.0.len()
}
}

impl<'a, O, T> Offset for BSlice<'a, O, T>
impl<'a, T, O> Offset for BSlice<'a, T, O>
where
O: BitOrder,
T: BitStore,
O: BitOrder,
{
#[inline(always)]
fn offset(&self, second: &Self) -> usize {
second.0.offset_from(&self.0) as usize
unsafe { second.0.as_bitptr().offset_from(self.0.as_bitptr()) as usize }
}
}

impl<'a, O> AsBytes for BSlice<'a, O, u8>
impl<'a, O> AsBytes for BSlice<'a, u8, O>
where
O: BitOrder,
{
#[inline(always)]
fn as_bytes(&self) -> &[u8] {
self.0.as_raw_slice()
let domain = self.0.domain();
let region = domain
.region()
.expect("expected memory region from bit slice");

region.1
}
}
/*
Expand Down Expand Up @@ -83,14 +88,14 @@ as_bytes_array_impls! {
30 31 32
}*/

impl<'a, O, T> InputIter for BSlice<'a, O, T>
impl<'a, T, O> InputIter for BSlice<'a, T, O>
where
O: BitOrder,
T: 'a + BitStore,
O: BitOrder,
{
type Item = bool;
type Iter = Enumerate<Self::IterElem>;
type IterElem = BitValIter<'a, O, T>;
type IterElem = BitValIter<'a, T, O>;

#[inline]
fn iter_indices(&self) -> Self::Iter {
Expand All @@ -99,7 +104,7 @@ where

#[inline]
fn iter_elements(&self) -> Self::IterElem {
self.0.iter().by_val()
self.0.iter().by_vals()
}

#[inline]
Expand All @@ -120,10 +125,10 @@ where
}
}

impl<'a, O, T> InputTake for BSlice<'a, O, T>
impl<'a, T, O> InputTake for BSlice<'a, T, O>
where
O: BitOrder,
T: 'a + BitStore,
O: BitOrder,
{
#[inline]
fn take(&self, count: usize) -> Self {
Expand All @@ -138,10 +143,10 @@ where
}

/*
impl<'a, 'b, O, T> InputTake for &'b BSlice<'a, O, T>
impl<'a, 'b, T, O> InputTake for &'b BSlice<'a, T, O>
where
O: BitOrder,
T: 'a + BitStore,
O: BitOrder,
{
#[inline]
fn take(&self, count: usize) -> Self {
Expand All @@ -156,10 +161,10 @@ where
}
*/

impl<'a, O, T> InputTakeAtPosition for BSlice<'a, O, T>
impl<'a, T, O> InputTakeAtPosition for BSlice<'a, T, O>
where
O: BitOrder,
T: 'a + BitStore,
O: BitOrder,
{
type Item = bool;

Expand All @@ -169,7 +174,7 @@ where
{
self.0
.iter()
.by_val()
.by_vals()
.position(predicate)
.map(|i| {
let (a, b) = self.0.split_at(i);
Expand All @@ -186,7 +191,7 @@ where
where
P: Fn(Self::Item) -> bool,
{
match self.0.iter().by_val().position(predicate) {
match self.0.iter().by_vals().position(predicate) {
Some(0) => {
let s = BSlice(self.0.split_at(0).1);
Err(Err::Error(E::from_error_kind(s, e)))
Expand Down Expand Up @@ -228,7 +233,7 @@ where
where
P: Fn(Self::Item) -> bool,
{
match self.0.iter().by_val().position(predicate) {
match self.0.iter().by_vals().position(predicate) {
Some(0) => {
let s = BSlice(self.0.split_at(0).1);
Err(Err::Error(E::from_error_kind(s, e)))
Expand All @@ -250,15 +255,15 @@ where
}
}

impl<'a, 'b, O1, O2, T1, T2> Compare<BSlice<'b, O2, T2>> for BSlice<'a, O1, T1>
impl<'a, 'b, T1, T2, O1, O2> Compare<BSlice<'b, T2, O2>> for BSlice<'a, T1, O1>
where
O1: BitOrder,
O2: BitOrder,
T1: 'a + BitStore,
T2: 'a + BitStore,
O1: BitOrder,
O2: BitOrder,
{
#[inline]
fn compare(&self, other: BSlice<'b, O2, T2>) -> CompareResult {
fn compare(&self, other: BSlice<'b, T2, O2>) -> CompareResult {
match self.0.iter().zip(other.0.iter()).position(|(a, b)| a != b) {
Some(_) => CompareResult::Error,
None => {
Expand All @@ -272,39 +277,39 @@ where
}

#[inline(always)]
fn compare_no_case(&self, other: BSlice<'b, O2, T2>) -> CompareResult {
fn compare_no_case(&self, other: BSlice<'b, T2, O2>) -> CompareResult {
self.compare(other)
}
}

impl<'a, O, T> FindToken<bool> for BSlice<'a, O, T>
impl<'a, T, O> FindToken<bool> for BSlice<'a, T, O>
where
O: BitOrder,
T: 'a + BitStore,
O: BitOrder,
{
fn find_token(&self, token: bool) -> bool {
self.0.iter().by_val().any(|i| i == token)
self.0.iter().by_vals().any(|i| i == token)
}
}

impl<'a, O, T> FindToken<(usize, bool)> for BSlice<'a, O, T>
impl<'a, T, O> FindToken<(usize, bool)> for BSlice<'a, T, O>
where
O: BitOrder,
T: 'a + BitStore,
O: BitOrder,
{
fn find_token(&self, token: (usize, bool)) -> bool {
self.0.iter().by_val().enumerate().any(|i| i == token)
self.0.iter().by_vals().enumerate().any(|i| i == token)
}
}

impl<'a, 'b, O1, O2, T1, T2> FindSubstring<BSlice<'b, O2, T2>> for BSlice<'a, O1, T1>
impl<'a, 'b, T1, T2, O1, O2> FindSubstring<BSlice<'b, T2, O2>> for BSlice<'a, T1, O1>
where
O1: BitOrder,
O2: BitOrder,
T1: 'a + BitStore,
T2: 'b + BitStore,
O1: BitOrder,
O2: BitOrder,
{
fn find_substring(&self, substr: BSlice<O2, T2>) -> Option<usize> {
fn find_substring(&self, substr: BSlice<T2, O2>) -> Option<usize> {
if substr.0.len() > self.0.len() {
return None;
}
Expand All @@ -329,7 +334,7 @@ macro_rules! impl_fn_slice {

macro_rules! slice_range_impl {
( BSlice, $ty:ty ) => {
impl<'a, O, T> Slice<$ty> for BSlice<'a, O, T>
impl<'a, T, O> Slice<$ty> for BSlice<'a, T, O>
where
O: BitOrder,
T: BitStore,
Expand All @@ -351,16 +356,16 @@ macro_rules! slice_ranges_impl {
slice_ranges_impl! {BSlice}

#[cfg(feature = "alloc")]
impl<'a, O, T> ExtendInto for BSlice<'a, O, T>
impl<'a, T, O> ExtendInto for BSlice<'a, T, O>
where
O: BitOrder,
T: BitStore,
O: BitOrder,
{
type Item = bool;
type Extender = BitVec<O, T>;
type Extender = BitVec<T, O>;

#[inline]
fn new_builder(&self) -> BitVec<O, T> {
fn new_builder(&self) -> BitVec<T, O> {
BitVec::new()
}

Expand All @@ -370,10 +375,10 @@ where
}
}

impl<'a, O, T> Index<usize> for BSlice<'a, O, T>
impl<'a, T, O> Index<usize> for BSlice<'a, T, O>
where
O: BitOrder,
T: BitStore,
O: BitOrder,
{
type Output = bool;

Expand All @@ -383,10 +388,10 @@ where
}

/*
impl<'a, O, T> Index<RangeFrom<usize>> for BSlice<'a, O, T>
impl<'a, T, O> Index<RangeFrom<usize>> for BSlice<'a, T, O>
where
O: BitOrder,
T: BitStore,
O: BitOrder,
{
type Output = Self;

Expand Down
6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
//! let data = [0xA5u8, 0x69, 0xF0, 0xC3];
//! let bits = data.view_bits::<Msb0>();
//!
//! fn parser(bits: &BitSlice<Msb0, u8>) -> IResult<&BitSlice<Msb0, u8>, &BitSlice<Msb0, u8>> {
//! fn parser(bits: &BitSlice<u8, Msb0>) -> IResult<&BitSlice<u8, Msb0>, &BitSlice<u8, Msb0>> {
//! tag(bits![1, 0, 1, 0])(bits)
//! }
//!
Expand All @@ -20,5 +20,5 @@ mod input;

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[repr(transparent)]
pub struct BSlice<'a, O: BitOrder, T: BitStore>(pub &'a BitSlice<O, T>);
pub struct BArray<O: BitOrder, T: BitStore>(pub BitArray<O, T>);
pub struct BSlice<'a, T: BitStore, O: BitOrder>(pub &'a BitSlice<T, O>);
pub struct BArray<T: BitStore, O: BitOrder>(pub BitArray<T, O>);
6 changes: 3 additions & 3 deletions tests/bitstream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ fn parse_bitstream() {
let data = [0xA5u8, 0x69, 0xF0, 0xC3];
let bits = data.view_bits::<Msb0>();

fn parser(bits: BSlice<Msb0, u8>) -> IResult<BSlice<Msb0, u8>, BSlice<Msb0, u8>> {
fn parser(bits: BSlice<u8, Msb0>) -> IResult<BSlice<u8, Msb0>, BSlice<u8, Msb0>> {
tag(BSlice(bits![1, 0, 1, 0]))(bits)
}

Expand All @@ -26,8 +26,8 @@ fn parse_bitstream_map() {
let data = [0b1000_0000];
let bits = data.view_bits::<Msb0>();

fn parser(bits: BSlice<Msb0, u8>) -> IResult<BSlice<Msb0, u8>, bool> {
map(take(1_u8), |val: BSlice<Msb0, u8>| val[0])(bits)
fn parser(bits: BSlice<u8, Msb0>) -> IResult<BSlice<u8, Msb0>, bool> {
map(take(1_u8), |val: BSlice<u8, Msb0>| val[0])(bits)
}

assert_eq!(parser(BSlice(bits)), Ok((BSlice(&bits[1..]), true)));
Expand Down