Boxed parser

This commit is contained in:
Greg Shuflin 2022-10-16 00:44:47 -07:00
parent 0c6c59da07
commit d707249904

View File

@ -8,6 +8,27 @@ trait Parser<I, O, E> {
fn parse(&self, input: I) -> ParseResult<I, O, E>; fn parse(&self, input: I) -> ParseResult<I, O, E>;
} }
struct BoxedParser<I, O, E> {
inner: Box<dyn Parser<I, O, E>>,
}
impl<I, O, E> BoxedParser<I, O, E> {
fn new<P>(inner: P) -> Self
where
P: Parser<I, O, E> + 'static,
{
BoxedParser {
inner: Box::new(inner),
}
}
}
impl<I, O, E> Parser<I, O, E> for BoxedParser<I, O, E> {
fn parse(&self, input: I) -> ParseResult<I, O, E> {
self.inner.parse(input)
}
}
impl<I, O, E, F> Parser<I, O, E> for F impl<I, O, E, F> Parser<I, O, E> for F
where where
F: Fn(I) -> ParseResult<I, O, E>, F: Fn(I) -> ParseResult<I, O, E>,