From 404642f7bb6c1c45cf85e699c4a31edd011fa7ca Mon Sep 17 00:00:00 2001 From: ambiso Date: Sat, 10 Dec 2022 16:37:48 +0100 Subject: [PATCH 1/2] Implement Iterator for owned ParserIterator Also test and fix doctests and examples --- examples/iterator.rs | 1 + src/combinator/mod.rs | 4 ++-- tests/issues.rs | 19 +++++++++++++++++++ 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/examples/iterator.rs b/examples/iterator.rs index 64a7ddcad..9c778a9db 100644 --- a/examples/iterator.rs +++ b/examples/iterator.rs @@ -71,6 +71,7 @@ fn main() { ); let res = nom_it + .by_ref() .map(|(k, v)| (k.to_uppercase(), v)) .collect::>(); diff --git a/src/combinator/mod.rs b/src/combinator/mod.rs index 7071cc7f3..7ed622470 100644 --- a/src/combinator/mod.rs +++ b/src/combinator/mod.rs @@ -650,7 +650,7 @@ where /// let data = "abc|defg|hijkl|mnopqr|123"; /// let mut it = iterator(data, terminated(alpha1, tag("|"))); /// -/// let parsed = it.map(|v| (v, v.len())).collect::>(); +/// let parsed = it.by_ref().map(|v| (v, v.len())).collect::>(); /// let res: IResult<_,_> = it.finish(); /// /// assert_eq!(parsed, [("abc", 3usize), ("defg", 4), ("hijkl", 5), ("mnopqr", 6)].iter().cloned().collect()); @@ -686,7 +686,7 @@ impl ParserIterator { } } -impl<'a, Input, Output, Error, F> core::iter::Iterator for &'a mut ParserIterator +impl core::iter::Iterator for ParserIterator where F: FnMut(Input) -> IResult, Input: Clone, diff --git a/tests/issues.rs b/tests/issues.rs index 4f7eaad18..d4823cc4f 100644 --- a/tests/issues.rs +++ b/tests/issues.rs @@ -229,3 +229,22 @@ fn issue_1459_clamp_capacity() { let mut parser = count::<_, _, (), _>(char('a'), usize::MAX); assert_eq!(parser("a"), Err(nom::Err::Error(()))); } + +#[test] +fn issue_1586_parser_iterator_impl() { + use nom::{ + character::complete::{digit1, newline}, + combinator::{iterator, opt}, + sequence::terminated, + IResult, + }; + fn parse_line(i: &str) -> IResult<&str, &str> { + terminated(digit1, opt(newline))(i) + } + + fn parse_input(i: &str) -> impl Iterator + '_ { + iterator(i, parse_line).map(|x| x.parse::().unwrap()) + } + + assert_eq!(parse_input("123\n456").collect::>(), vec![123, 456]); +} \ No newline at end of file From 0325f448f7a8db0c5cb8fd3c5ff4370488a6fb9a Mon Sep 17 00:00:00 2001 From: Geoffroy Couprie Date: Sat, 21 Oct 2023 14:55:21 +0200 Subject: [PATCH 2/2] fix test --- tests/issues.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/issues.rs b/tests/issues.rs index ef7e89082..fad916c47 100644 --- a/tests/issues.rs +++ b/tests/issues.rs @@ -281,7 +281,7 @@ fn issue_1586_parser_iterator_impl() { IResult, }; fn parse_line(i: &str) -> IResult<&str, &str> { - terminated(digit1, opt(newline))(i) + terminated(digit1, opt(newline)).parse(i) } fn parse_input(i: &str) -> impl Iterator + '_ { @@ -289,4 +289,4 @@ fn issue_1586_parser_iterator_impl() { } assert_eq!(parse_input("123\n456").collect::>(), vec![123, 456]); -} \ No newline at end of file +}