diff --git a/Cargo.toml b/Cargo.toml index e57690e..2491f22 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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] diff --git a/src/input.rs b/src/input.rs index 47bdce9..a20f702 100644 --- a/src/input.rs +++ b/src/input.rs @@ -10,10 +10,10 @@ 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 { @@ -21,10 +21,10 @@ where } } -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 { @@ -32,24 +32,29 @@ where } } -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 } } /* @@ -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; - type IterElem = BitValIter<'a, O, T>; + type IterElem = BitValIter<'a, T, O>; #[inline] fn iter_indices(&self) -> Self::Iter { @@ -99,7 +104,7 @@ where #[inline] fn iter_elements(&self) -> Self::IterElem { - self.0.iter().by_val() + self.0.iter().by_vals() } #[inline] @@ -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 { @@ -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 { @@ -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; @@ -169,7 +174,7 @@ where { self.0 .iter() - .by_val() + .by_vals() .position(predicate) .map(|i| { let (a, b) = self.0.split_at(i); @@ -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))) @@ -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))) @@ -250,15 +255,15 @@ where } } -impl<'a, 'b, O1, O2, T1, T2> Compare> for BSlice<'a, O1, T1> +impl<'a, 'b, T1, T2, O1, O2> Compare> 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 => { @@ -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 for BSlice<'a, O, T> +impl<'a, T, O> FindToken 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> for BSlice<'a, O1, T1> +impl<'a, 'b, T1, T2, O1, O2> FindSubstring> 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) -> Option { + fn find_substring(&self, substr: BSlice) -> Option { if substr.0.len() > self.0.len() { return None; } @@ -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, @@ -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; + type Extender = BitVec; #[inline] - fn new_builder(&self) -> BitVec { + fn new_builder(&self) -> BitVec { BitVec::new() } @@ -370,10 +375,10 @@ where } } -impl<'a, O, T> Index for BSlice<'a, O, T> +impl<'a, T, O> Index for BSlice<'a, T, O> where - O: BitOrder, T: BitStore, + O: BitOrder, { type Output = bool; @@ -383,10 +388,10 @@ where } /* -impl<'a, O, T> Index> for BSlice<'a, O, T> +impl<'a, T, O> Index> for BSlice<'a, T, O> where - O: BitOrder, T: BitStore, + O: BitOrder, { type Output = Self; diff --git a/src/lib.rs b/src/lib.rs index 861aecb..4966a2d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,7 +8,7 @@ //! let data = [0xA5u8, 0x69, 0xF0, 0xC3]; //! let bits = data.view_bits::(); //! -//! fn parser(bits: &BitSlice) -> IResult<&BitSlice, &BitSlice> { +//! fn parser(bits: &BitSlice) -> IResult<&BitSlice, &BitSlice> { //! tag(bits![1, 0, 1, 0])(bits) //! } //! @@ -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); -pub struct BArray(pub BitArray); +pub struct BSlice<'a, T: BitStore, O: BitOrder>(pub &'a BitSlice); +pub struct BArray(pub BitArray); diff --git a/tests/bitstream.rs b/tests/bitstream.rs index 2eb1899..d2f51fb 100644 --- a/tests/bitstream.rs +++ b/tests/bitstream.rs @@ -11,7 +11,7 @@ fn parse_bitstream() { let data = [0xA5u8, 0x69, 0xF0, 0xC3]; let bits = data.view_bits::(); - fn parser(bits: BSlice) -> IResult, BSlice> { + fn parser(bits: BSlice) -> IResult, BSlice> { tag(BSlice(bits![1, 0, 1, 0]))(bits) } @@ -26,8 +26,8 @@ fn parse_bitstream_map() { let data = [0b1000_0000]; let bits = data.view_bits::(); - fn parser(bits: BSlice) -> IResult, bool> { - map(take(1_u8), |val: BSlice| val[0])(bits) + fn parser(bits: BSlice) -> IResult, bool> { + map(take(1_u8), |val: BSlice| val[0])(bits) } assert_eq!(parser(BSlice(bits)), Ok((BSlice(&bits[1..]), true)));