From 981b5c08d07e01e6a0f4c7f59c36045e97055cc2 Mon Sep 17 00:00:00 2001 From: Greg Shuflin Date: Sun, 16 Oct 2022 00:54:41 -0700 Subject: [PATCH] Make map a method --- src/lib.rs | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 602ce57..16da3dd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,16 +6,28 @@ type ParseResult = Result<(O, I), E>; trait Parser { fn parse(&self, input: I) -> ParseResult; + + fn map<'a, F, O2>(self, map_fn: F) -> BoxedParser<'a, I, O2, E> + where + Self: Sized + 'a, + I: 'a, + E: 'a, + O: 'a, + O2: 'a, + F: Fn(O) -> O2 + 'a, + { + BoxedParser::new(map(self, map_fn)) + } } -struct BoxedParser { - inner: Box>, +struct BoxedParser<'a, I, O, E> { + inner: Box + 'a>, } -impl BoxedParser { +impl<'a, I, O, E> BoxedParser<'a, I, O, E> { fn new

(inner: P) -> Self where - P: Parser + 'static, + P: Parser + 'a, { BoxedParser { inner: Box::new(inner), @@ -23,7 +35,7 @@ impl BoxedParser { } } -impl Parser for BoxedParser { +impl<'a, I, O, E> Parser for BoxedParser<'a, I, O, E> { fn parse(&self, input: I) -> ParseResult { self.inner.parse(input) } @@ -178,7 +190,7 @@ mod tests { #[test] fn test_map() { let lit_a = literal("a"); - let output = map(lit_a, |s| s.to_uppercase()).parse("a yolo"); + let output = lit_a.map(|s| s.to_uppercase()).parse("a yolo"); assert_matches!(output.unwrap(), (s, " yolo") if s == "A"); }