Skip to content

Commit

Permalink
Parser associated types (#1626)
Browse files Browse the repository at this point in the history
[BREAKING CHANGE]

* move the Output and Error types of Parser to associated types

this reduces the number of generic types in some combinators, which will
be useful when I reintroduce some complexity there later

* translate combinators to using Output and Error associated types

The output type should now mainly come from associated types instead of
being an explicit generic type. From a few tests here and there, it
appears that the error type could be removed too, but that makes the type
signature harder to read

This slightly increases compilation time, but not in a noticeable way
  • Loading branch information
Geal authored Jan 22, 2023
1 parent 4c6e45e commit bf8cddb
Show file tree
Hide file tree
Showing 16 changed files with 315 additions and 250 deletions.
4 changes: 3 additions & 1 deletion benchmarks/benches/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ fn string(input: &str) -> IResult<&str, String> {
)(input)
}

fn ws<'a, O, E: ParseError<&'a str>, F: Parser<&'a str, O, E>>(f: F) -> impl Parser<&'a str, O, E> {
fn ws<'a, O, E: ParseError<&'a str>, F: Parser<&'a str, Output = O, Error = E>>(
f: F,
) -> impl Parser<&'a str, Output = O, Error = E> {
delimited(multispace0, f, multispace0)
}

Expand Down
4 changes: 3 additions & 1 deletion benchmarks/benches/json_streaming.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ fn string(input: &str) -> IResult<&str, String> {
)(input)
}

fn ws<'a, O, E: ParseError<&'a str>, F: Parser<&'a str, O, E>>(f: F) -> impl Parser<&'a str, O, E> {
fn ws<'a, O, E: ParseError<&'a str>, F: Parser<&'a str, Output = O, Error = E>>(
f: F,
) -> impl Parser<&'a str, Output = O, Error = E> {
delimited(multispace0, f, multispace0)
}

Expand Down
2 changes: 1 addition & 1 deletion examples/s_expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ fn parse_constant(i: &str) -> IResult<&str, Expr, VerboseError<&str>> {
/// takes a parsing function and returns a new parsing function.
fn s_exp<'a, O1, F>(inner: F) -> impl FnMut(&'a str) -> IResult<&'a str, O1, VerboseError<&'a str>>
where
F: Parser<&'a str, O1, VerboseError<&'a str>>,
F: Parser<&'a str, Output = O1, Error = VerboseError<&'a str>>,
{
delimited(
char('('),
Expand Down
4 changes: 2 additions & 2 deletions src/bits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ where
E1: ParseError<(I, usize)> + ErrorConvert<E2>,
E2: ParseError<I>,
I: Input,
P: Parser<(I, usize), O, E1>,
P: Parser<(I, usize), Output = O, Error = E1>,
{
move |input: I| match parser.parse((input, 0)) {
Ok(((rest, offset), result)) => {
Expand Down Expand Up @@ -86,7 +86,7 @@ where
E1: ParseError<I> + ErrorConvert<E2>,
E2: ParseError<(I, usize)>,
I: Input + Clone,
P: Parser<I, O, E1>,
P: Parser<I, Output = O, Error = E1>,
{
move |(input, offset): (I, usize)| {
let inner = if offset % 8 != 0 {
Expand Down
6 changes: 3 additions & 3 deletions src/branch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ macro_rules! alt_trait_impl(
($($id:ident)+) => (
impl<
Input: Clone, Output, Error: ParseError<Input>,
$($id: Parser<Input, Output, Error>),+
$($id: Parser<Input, Output = Output, Error = Error>),+
> Alt<Input, Output, Error> for ( $($id),+ ) {

fn choice(&mut self, input: Input) -> IResult<Input, Output, Error> {
Expand Down Expand Up @@ -159,7 +159,7 @@ macro_rules! alt_trait_inner(
alt_trait!(A B C D E F G H I J K L M N O P Q R S T U);

// Manually implement Alt for (A,), the 1-tuple type
impl<Input, Output, Error: ParseError<Input>, A: Parser<Input, Output, Error>>
impl<Input, Output, Error: ParseError<Input>, A: Parser<Input, Output = Output, Error = Error>>
Alt<Input, Output, Error> for (A,)
{
fn choice(&mut self, input: Input) -> IResult<Input, Output, Error> {
Expand Down Expand Up @@ -191,7 +191,7 @@ macro_rules! permutation_trait_impl(
($($name:ident $ty:ident $item:ident),+) => (
impl<
Input: Clone, $($ty),+ , Error: ParseError<Input>,
$($name: Parser<Input, $ty, Error>),+
$($name: Parser<Input, Output = $ty, Error = Error>),+
> Permutation<Input, ( $($ty),+ ), Error> for ( $($name),+ ) {

fn permutation(&mut self, mut input: Input) -> IResult<Input, ( $($ty),+ ), Error> {
Expand Down
10 changes: 5 additions & 5 deletions src/bytes/complete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -483,16 +483,16 @@ where
/// assert_eq!(esc(r#"12\"34;"#), Ok((";", r#"12\"34"#)));
/// ```
///
pub fn escaped<'a, I: 'a, Error, F, G, O1, O2>(
pub fn escaped<'a, I: 'a, Error, F, G>(
mut normal: F,
control_char: char,
mut escapable: G,
) -> impl FnMut(I) -> IResult<I, I, Error>
where
I: Clone + crate::traits::Offset + Input,
<I as Input>::Item: crate::traits::AsChar,
F: Parser<I, O1, Error>,
G: Parser<I, O2, Error>,
F: Parser<I, Error = Error>,
G: Parser<I, Error = Error>,
Error: ParseError<I>,
{
use crate::traits::AsChar;
Expand Down Expand Up @@ -602,8 +602,8 @@ where
O1: crate::traits::ExtendInto<Item = ExtendItem, Extender = Output>,
O2: crate::traits::ExtendInto<Item = ExtendItem, Extender = Output>,
<I as Input>::Item: crate::traits::AsChar,
F: Parser<I, O1, Error>,
G: Parser<I, O2, Error>,
F: Parser<I, Output = O1, Error = Error>,
G: Parser<I, Output = O2, Error = Error>,
Error: ParseError<I>,
{
use crate::traits::AsChar;
Expand Down
10 changes: 5 additions & 5 deletions src/bytes/streaming.rs
Original file line number Diff line number Diff line change
Expand Up @@ -499,16 +499,16 @@ where
/// assert_eq!(esc("12\\\"34;"), Ok((";", "12\\\"34")));
/// ```
///
pub fn escaped<I, Error, F, G, O1, O2>(
pub fn escaped<I, Error, F, G>(
mut normal: F,
control_char: char,
mut escapable: G,
) -> impl FnMut(I) -> IResult<I, I, Error>
where
I: Input + Clone + crate::traits::Offset,
<I as Input>::Item: crate::traits::AsChar,
F: Parser<I, O1, Error>,
G: Parser<I, O2, Error>,
F: Parser<I, Error = Error>,
G: Parser<I, Error = Error>,
Error: ParseError<I>,
{
use crate::traits::AsChar;
Expand Down Expand Up @@ -606,8 +606,8 @@ where
O1: crate::traits::ExtendInto<Item = ExtendItem, Extender = Output>,
O2: crate::traits::ExtendInto<Item = ExtendItem, Extender = Output>,
<I as Input>::Item: crate::traits::AsChar,
F: Parser<I, O1, Error>,
G: Parser<I, O2, Error>,
F: Parser<I, Output = O1, Error = Error>,
G: Parser<I, Output = O2, Error = Error>,
Error: ParseError<I>,
{
use crate::traits::AsChar;
Expand Down
Loading

0 comments on commit bf8cddb

Please sign in to comment.