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

try_fold support #1411

Open
epage opened this issue Sep 27, 2021 · 4 comments
Open

try_fold support #1411

epage opened this issue Sep 27, 2021 · 4 comments
Milestone

Comments

@epage
Copy link
Contributor

epage commented Sep 27, 2021

Prerequisites

Here are a few things you should provide to help me understand the issue:

  • Rust version : 1.55.0
  • nom version : 7.0.0
  • nom compilation features used: default

Split out of #1409

fold_many_* doesn't support coming across errors. When folding the elements of a TOML table, we need to error if keys are duplicated. I think it'd be useful to take inspiration from try_fold in the stdlib. This uses an unstable trait but we can for now constrain what types we can Try on until Try becomes stable.

@epage
Copy link
Contributor Author

epage commented Sep 27, 2021

We should wait until #1393 so we can leverage it for only having 1-2 versions, rather than 4+

@Geal
Copy link
Collaborator

Geal commented Oct 10, 2021

good idea, and yes, it'd make more sense after we get ranges

@Geal Geal added this to the 8.0 milestone Oct 10, 2021
@Stargateur
Copy link
Contributor

This uses an unstable trait but we can for now constrain what types we can Try on until Try becomes stable.

So I try...

And it very very very very very very very very very complicated.

  1. I add a nightly feature
  2. I go for fn try_fold => IResult<Input, Try<Output = Acc, Error> problem this make an result inside a result so I didn't push it
  3. I go for fn try_fold => IResult<Input, Acc, Error> where F: FnMut(Acc, Output) -> Try, Try: std::ops::Try<Output = Acc, Residual = Error>, merging the error... problem big problem I let you read the new very short RFC

The problem is that all nom parser return an Result and the Residual type for impl Try for Result is Result<Infallible, E>. This make any generic instantly fail. The user can't use it cause

the trait `error::ParseError<_>` is not implemented for `Result<Infallible, error::Error<&str>>`

While maybe we could add it, I think this will further add problem. I think using Try would make sense if we return the Try inside try_fold. This mean maybe nom would completely rewrite its error type, for example a NomResult that implement Try allowing a lot of thing.

So, for now I think simple require the function to return a Result<Acc, Err> is much better. (Err of nom allowing to return failure error for allocation failure for example)

@epage
Copy link
Contributor Author

epage commented Oct 21, 2021

So, for now I think simple require the function to return a Result<Acc, Err> is much better. (Err of nom allowing to return failure error for allocation failure for example)

This was my intention, at least until Try became stable, and then we could re-evaluate. Sounds like that evaluation has happened now :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants