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

fix(parser)!: Improve type inference for Parser::by_ref, Parser::complete_err #669

Merged
merged 2 commits into from
Jan 4, 2025
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
40 changes: 28 additions & 12 deletions src/combinator/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,26 @@ use crate::stream::{Location, Stream};
use crate::*;

/// Implementation of [`Parser::by_ref`]
pub struct ByRef<'p, P> {
pub struct ByRef<'p, P, I, O, E> {
p: &'p mut P,
i: core::marker::PhantomData<I>,
o: core::marker::PhantomData<O>,
e: core::marker::PhantomData<E>,
}

impl<'p, P> ByRef<'p, P> {
impl<'p, P, I, O, E> ByRef<'p, P, I, O, E> {
#[inline(always)]
pub(crate) fn new(p: &'p mut P) -> Self {
Self { p }
Self {
p,
i: Default::default(),
o: Default::default(),
e: Default::default(),
}
}
}

impl<I, O, E, P> Parser<I, O, E> for ByRef<'_, P>
impl<I, O, E, P> Parser<I, O, E> for ByRef<'_, P, I, O, E>
where
P: Parser<I, O, E>,
{
Expand Down Expand Up @@ -356,27 +364,35 @@ where
}

/// Implementation of [`Parser::complete_err`]
pub struct CompleteErr<F> {
f: F,
pub struct CompleteErr<P, I, O, E> {
p: P,
i: core::marker::PhantomData<I>,
o: core::marker::PhantomData<O>,
e: core::marker::PhantomData<E>,
}

impl<F> CompleteErr<F> {
impl<P, I, O, E> CompleteErr<P, I, O, E> {
#[inline(always)]
pub(crate) fn new(f: F) -> Self {
Self { f }
pub(crate) fn new(p: P) -> Self {
Self {
p,
i: Default::default(),
o: Default::default(),
e: Default::default(),
}
}
}

impl<F, I, O, E> Parser<I, O, E> for CompleteErr<F>
impl<P, I, O, E> Parser<I, O, E> for CompleteErr<P, I, O, E>
where
P: Parser<I, O, E>,
I: Stream,
F: Parser<I, O, E>,
E: ParserError<I>,
{
#[inline]
fn parse_next(&mut self, input: &mut I) -> PResult<O, E> {
trace("complete_err", |input: &mut I| {
match (self.f).parse_next(input) {
match (self.p).parse_next(input) {
Err(ErrMode::Incomplete(_)) => {
Err(ErrMode::from_error_kind(input, ErrorKind::Complete))
}
Expand Down
4 changes: 2 additions & 2 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ pub trait Parser<I, O, E> {
/// }
/// ```
#[inline(always)]
fn by_ref(&mut self) -> ByRef<'_, Self>
fn by_ref(&mut self) -> ByRef<'_, Self, I, O, E>
where
Self: core::marker::Sized,
{
Expand Down Expand Up @@ -686,7 +686,7 @@ pub trait Parser<I, O, E> {
/// # }
/// ```
#[inline(always)]
fn complete_err(self) -> CompleteErr<Self>
fn complete_err(self) -> CompleteErr<Self, I, O, E>
where
Self: core::marker::Sized,
{
Expand Down
Loading