Skip to content

Instantly share code, notes, and snippets.

@CJKay
Created September 13, 2019 16:56
Show Gist options
  • Save CJKay/2764f1e02adab4342ddfa281bd4f584d to your computer and use it in GitHub Desktop.
Save CJKay/2764f1e02adab4342ddfa281bd4f584d to your computer and use it in GitHub Desktop.
use crate::Result;
use nom::number::streaming::le_u32;
use std::io::Read;
fn stream<Reader, Output, Parser, Error>(mut reader: Reader, parser: Parser) -> Result<<Output as std::borrow::ToOwned>::Owned>
where
Reader: Read,
Output: ToOwned,
Parser: for<'input> Fn(&'input [u8]) -> nom::IResult<&'input [u8], Output, Error>,
Error: for<'input> nom::error::ParseError<&'input [u8]>,
{
let mut buffer = Vec::new();
loop {
let n = match parser(&buffer) {
Err(nom::Err::Incomplete(need)) => match need {
nom::Needed::Size(n) => n,
nom::Needed::Unknown => 1,
},
Err(error) => return Err(error.into()),
Ok((_, result)) => return Ok(result.to_owned()),
};
(&mut reader).take(n as u64).read_to_end(&mut buffer)?;
}
}
pub fn parse<Reader>(reader: Reader) -> Result<()>
where
Reader: Read,
{
let _magic = stream(reader, le_u32)?;
Ok(())
}
// error[E0631]: type mismatch in function arguments
// --> src\parser.rs:33:18
// |
// 33 | let _magic = stream(reader, le_u32)?;
// | ^^^^^^
// | |
// | expected signature of `for<'input> fn(&'input [u8]) -> _`
// | found signature of `fn(&[u8]) -> _`
// |
// note: required by `parser::stream`
// --> src\parser.rs:5:1
// |
// 5 | / fn stream<Reader, Output, Parser, Error>(mut reader: Reader, parser: Parser) -> Result<<Output as std::borrow::ToOwned>::Owned>
// 6 | | where
// 7 | | Reader: Read,
// 8 | | Output: ToOwned,
// ... |
// 26 | | }
// 27 | | }
// | |_^
//
// error[E0271]: type mismatch resolving `for<'input> <fn(&[u8]) -> std::result::Result<(&[u8], u32), nom::internal::Err<_>> {nom::number::streaming::le_u32::<'_, _>} as std::ops::FnOnce<(&'input [u8],)>>::Output == std::result::Result<(&'input [u8], _), nom::internal::Err<_>>`
// --> src\parser.rs:33:18
// |
// 33 | let _magic = stream(reader, le_u32)?;
// | ^^^^^^ expected bound lifetime parameter 'input, found concrete lifetime
// |
// note: required by `parser::stream`
// --> src\parser.rs:5:1
// |
// 5 | / fn stream<Reader, Output, Parser, Error>(mut reader: Reader, parser: Parser) -> Result<<Output as std::borrow::ToOwned>::Owned>
// 6 | | where
// 7 | | Reader: Read,
// 8 | | Output: ToOwned,
// ... |
// 26 | | }
// 27 | | }
// | |_^
//
// error: aborting due to 2 previous errors
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment