Skip to content

Instantly share code, notes, and snippets.

@Centril
Created December 22, 2016 21:33
Show Gist options
  • Save Centril/946b6d226d626ec76c6507da056188c6 to your computer and use it in GitHub Desktop.
Save Centril/946b6d226d626ec76c6507da056188c6 to your computer and use it in GitHub Desktop.
nom::IResult::map_rem
// For debugging purposes:
impl<I, O, E=u32> IResult<I, O, E> {
fn map_rem<R, F: FnOnce(I) -> R>(self, f: F) -> IResult<R, O, E> {
use IResult::*;
match s {
Done(i, o) => Done(f(i), o),
Error(e) => Error(e),
Incomplete(n) => Incomplete(n),
}
}
}
// The purpose of this is mainly to get away from b"..." while testing
// since the results are not human readable in a terminal...
fn utf8(s: &[u8]) -> &str {
std::str::from_utf8(s).unwrap()
}
let d = Done(b"remaining", b"matched");
assert_eq!(d.map(utf8).map_rem(utf8), Done("remaining", "matched"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment