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

only declared function parsers can be used #3

Open
Geal opened this issue Aug 6, 2021 · 4 comments
Open

only declared function parsers can be used #3

Geal opened this issue Aug 6, 2021 · 4 comments

Comments

@Geal
Copy link
Contributor

Geal commented Aug 6, 2021

Apparently I cannot write this:

let _ = i.parse( tag("abcd").map(|_| ()) )?;

As it would result in this error:

error[E0308]: mismatched types
  --> examples/sync_http.rs:35:15
   |
35 |     let _ = i.parse(tag("abcd").map(|_| ()))?;
   |               ^^^^^ one type is more general than the other
   | 
  ::: /home/geal/.cargo/registry/src/github.com-1ecc6299db9ec823/nom-6.2.1/src/bytes/streaming.rs:34:6
   |
34 | ) -> impl Fn(Input) -> IResult<Input, Input, Error>
   |      ----------------------------------------------
   |      |
   |      the expected opaque type
   |      the found opaque type
   |
   = note: expected associated type `<impl Fn<(&[u8],)> as FnOnce<(&[u8],)>>::Output`
              found associated type `<impl Fn<(&[u8],)> as FnOnce<(&'a [u8],)>>::Output`

For more information about this error, try `rustc --explain E0308`.
error: could not compile `nom-bufreader` due to previous error
@cheako
Copy link

cheako commented Nov 16, 2021

Perhaps related too: rust-lang/rust#30867

@cheako
Copy link

cheako commented Nov 16, 2021

Also, even declared functions have this problem????
archive.ar.txt

@jobtijhuis
Copy link

To summarize from the two archives @cheako posted:

Making the error manually owned works as a workaround for now:

use nom_bufreader::bufreader::BufReader;
use nom_bufreader::Parse;
use nom::error::{ContextError, Error as NomError, ErrorKind, FromExternalError, ParseError};

#[derive(Debug, PartialEq)]
struct Error(NomError<Vec<u8>>);

impl ParseError<&'_ [u8]> for Error {
    fn from_error_kind(input: &'_ [u8], kind: ErrorKind) -> Self {
        Error(NomError::from_error_kind(input.to_owned(), kind))
    }

    fn append(_: &'_ [u8], _: ErrorKind, other: Self) -> Self {
        other
    }
}
impl ContextError<&'_ [u8]> for Error {}

impl<E> FromExternalError<&'_ [u8], E> for Error {
    fn from_external_error(input: &'_ [u8], kind: ErrorKind, _e: E) -> Self {
        Error(NomError::from_external_error(input.to_owned(), kind, _e))
    }
}

fn list_ints(input: &[u8]) -> IResult<&[u8], Vec<u8>, Error> {
    separated_list0(tag(","), u8)(input)
}

But to actually fix this usability problem for declared functions see #9

Disclaimer: quite new to Rust but loving the nom parser thus far

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