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

Merge InputIter, InputTakeAtPosition, InputLength and InputTake #1612

Merged
merged 5 commits into from
Jan 7, 2023
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
12 changes: 6 additions & 6 deletions src/bits/complete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

use crate::error::{ErrorKind, ParseError};
use crate::internal::{Err, IResult};
use crate::lib::std::ops::{AddAssign, Div, RangeFrom, Shl, Shr};
use crate::traits::{InputIter, InputLength, Slice, ToUsize};
use crate::lib::std::ops::{AddAssign, Div, Shl, Shr};
use crate::traits::{Input, ToUsize};

/// Generates a parser taking `count` bits
///
Expand Down Expand Up @@ -34,7 +34,7 @@ pub fn take<I, O, C, E: ParseError<(I, usize)>>(
count: C,
) -> impl Fn((I, usize)) -> IResult<(I, usize), O, E>
where
I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
I: Input<Item = u8>,
C: ToUsize,
O: From<u8> + AddAssign + Shl<usize, Output = O> + Shr<usize, Output = O>,
{
Expand Down Expand Up @@ -75,7 +75,7 @@ where
offset = 0;
}
}
Ok(((input.slice(cnt..), end_offset), acc))
Ok(((input.take_from(cnt), end_offset), acc))
}
}
}
Expand All @@ -87,7 +87,7 @@ pub fn tag<I, O, C, E: ParseError<(I, usize)>>(
count: C,
) -> impl Fn((I, usize)) -> IResult<(I, usize), O, E>
where
I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength + Clone,
I: Input<Item = u8> + Clone,
C: ToUsize,
O: From<u8> + AddAssign + Shl<usize, Output = O> + Shr<usize, Output = O> + PartialEq,
{
Expand Down Expand Up @@ -122,7 +122,7 @@ where
/// ```
pub fn bool<I, E: ParseError<(I, usize)>>(input: (I, usize)) -> IResult<(I, usize), bool, E>
where
I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
I: Input<Item = u8>,
{
let (res, bit): (_, u32) = take(1usize)(input)?;
Ok((res, bit != 0))
Expand Down
14 changes: 7 additions & 7 deletions src/bits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ pub mod streaming;

use crate::error::{ErrorKind, ParseError};
use crate::internal::{Err, IResult, Needed, Parser};
use crate::lib::std::ops::RangeFrom;
use crate::traits::{ErrorConvert, Slice};
use crate::traits::ErrorConvert;
use crate::Input;

/// Converts a byte-level input to a bit-level input, for consumption by a parser that uses bits.
///
Expand Down Expand Up @@ -40,7 +40,7 @@ pub fn bits<I, O, E1, E2, P>(mut parser: P) -> impl FnMut(I) -> IResult<I, O, E2
where
E1: ParseError<(I, usize)> + ErrorConvert<E2>,
E2: ParseError<I>,
I: Slice<RangeFrom<usize>>,
I: Input,
P: Parser<(I, usize), O, E1>,
{
move |input: I| match parser.parse((input, 0)) {
Expand All @@ -49,7 +49,7 @@ where
// The parser functions might already slice away all fully read bytes.
// That's why `offset / 8` isn't necessarily needed at all times.
let remaining_bytes_index = offset / 8 + if offset % 8 == 0 { 0 } else { 1 };
Ok((rest.slice(remaining_bytes_index..), result))
Ok((rest.take_from(remaining_bytes_index), result))
}
Err(Err::Incomplete(n)) => Err(Err::Incomplete(n.map(|u| u.get() / 8 + 1))),
Err(Err::Error(e)) => Err(Err::Error(e.convert())),
Expand Down Expand Up @@ -85,14 +85,14 @@ pub fn bytes<I, O, E1, E2, P>(mut parser: P) -> impl FnMut((I, usize)) -> IResul
where
E1: ParseError<I> + ErrorConvert<E2>,
E2: ParseError<(I, usize)>,
I: Slice<RangeFrom<usize>> + Clone,
I: Input + Clone,
P: Parser<I, O, E1>,
{
move |(input, offset): (I, usize)| {
let inner = if offset % 8 != 0 {
input.slice((1 + offset / 8)..)
input.take_from(1 + offset / 8)
} else {
input.slice((offset / 8)..)
input.take_from(offset / 8)
};
let i = (input, offset);
match parser.parse(inner) {
Expand Down
12 changes: 6 additions & 6 deletions src/bits/streaming.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@

use crate::error::{ErrorKind, ParseError};
use crate::internal::{Err, IResult, Needed};
use crate::lib::std::ops::{AddAssign, Div, RangeFrom, Shl, Shr};
use crate::traits::{InputIter, InputLength, Slice, ToUsize};
use crate::lib::std::ops::{AddAssign, Div, Shl, Shr};
use crate::traits::{Input, ToUsize};

/// Generates a parser taking `count` bits
pub fn take<I, O, C, E: ParseError<(I, usize)>>(
count: C,
) -> impl Fn((I, usize)) -> IResult<(I, usize), O, E>
where
I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
I: Input<Item = u8>,
C: ToUsize,
O: From<u8> + AddAssign + Shl<usize, Output = O> + Shr<usize, Output = O>,
{
Expand Down Expand Up @@ -49,7 +49,7 @@ where
offset = 0;
}
}
Ok(((input.slice(cnt..), end_offset), acc))
Ok(((input.take_from(cnt), end_offset), acc))
}
}
}
Expand All @@ -61,7 +61,7 @@ pub fn tag<I, O, C, E: ParseError<(I, usize)>>(
count: C,
) -> impl Fn((I, usize)) -> IResult<(I, usize), O, E>
where
I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength + Clone,
I: Input<Item = u8> + Clone,
C: ToUsize,
O: From<u8> + AddAssign + Shl<usize, Output = O> + Shr<usize, Output = O> + PartialEq,
{
Expand Down Expand Up @@ -96,7 +96,7 @@ where
/// ```
pub fn bool<I, E: ParseError<(I, usize)>>(input: (I, usize)) -> IResult<(I, usize), bool, E>
where
I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
I: Input<Item = u8>,
{
let (res, bit): (_, u32) = take(1usize)(input)?;
Ok((res, bit != 0))
Expand Down
Loading