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

Alt trait for array #1269

Closed
wants to merge 2 commits into from
Closed

Conversation

snylonue
Copy link

Currently the branch::alt() only deals with tuples with at most 21 elements(#1144, #994) , so I write this pr trying to change it.

Trait Alt has been implemented for &'a mut [P] where P: Parser as a result, but due to the limition of my ability and rust compiler, I failed to implement the trait for [P] and P must have the same type.
For example, the code as follows failed to be compiled:

use nom::branch::alt;
use nom::character::complete::{alpha1, digit1};
fn main() {
fn parser(input: &str) -> IResult<&str, &str> {
  alt([alpha1, digit1])(input) // using an array
};

I tried to implement it for AsMut<Parser> but the compiler stops me
@coveralls
Copy link

Coverage Status

Coverage decreased (-0.2%) to 82.52% when pulling 28265cb on snylonue:alt_trait_for_array into c5090ef on Geal:master.

@Geal
Copy link
Collaborator

Geal commented Jan 23, 2021

I did not implement that trait for arrays for that exact reason. Even if some parsers have the same input, output and error types, their actual type might be different due to heavy use of closures and impl trait returns

@snylonue snylonue closed this Jan 24, 2021
@snylonue
Copy link
Author

I don't think it is a good idea now

@LoganDark
Copy link
Contributor

LoganDark commented May 12, 2022

I did not implement that trait for arrays for that exact reason. Even if some parsers have the same input, output and error types, their actual type might be different due to heavy use of closures and impl trait returns

What about for parsers that have no impl types and are completely identical? You are forbidding a use case just because it is uncommon. :(

I just tested implementing Alt for all arrays using const generics, and it is definitely possible (tests compile when converted to arrays, etc), but it would take me hours of effort to read the tuple macros to understand exactly what the error behaviors are, so I'm going to call it here.

Just know that the alternative, having to implement "abuse alt associativity" in macros, is also very difficult and annoying. :/

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

Successfully merging this pull request may close these issues.

4 participants