From dfcd4dd48825705c326e45f9deb8a4fbb9aa2264 Mon Sep 17 00:00:00 2001 From: Geoffroy Couprie Date: Sun, 5 May 2024 15:05:00 +0200 Subject: [PATCH 1/2] remove the InputLength trait --- src/bytes/complete.rs | 10 +++---- src/bytes/mod.rs | 10 +++---- src/bytes/streaming.rs | 8 +++--- src/bytes/tests.rs | 4 +-- src/character/complete.rs | 4 +-- src/combinator/mod.rs | 10 +++---- src/multi/mod.rs | 60 +++++++++++++++++++-------------------- src/traits.rs | 44 ---------------------------- tests/issues.rs | 2 +- 9 files changed, 53 insertions(+), 99 deletions(-) diff --git a/src/bytes/complete.rs b/src/bytes/complete.rs index a53b2dd64..1d43c7502 100644 --- a/src/bytes/complete.rs +++ b/src/bytes/complete.rs @@ -4,7 +4,7 @@ use core::marker::PhantomData; use crate::error::ParseError; use crate::internal::{IResult, Parser}; -use crate::traits::{Compare, FindSubstring, FindToken, InputLength, ToUsize}; +use crate::traits::{Compare, FindSubstring, FindToken, ToUsize}; use crate::Complete; use crate::Emit; use crate::Input; @@ -32,7 +32,7 @@ use crate::OutputM; pub fn tag>(tag: T) -> impl Fn(I) -> IResult where I: Input + Compare, - T: InputLength + Clone, + T: Input + Clone, { move |i: I| { let mut parser = super::Tag { @@ -68,7 +68,7 @@ where pub fn tag_no_case>(tag: T) -> impl Fn(I) -> IResult where I: Input + Compare, - T: InputLength + Clone, + T: Input + Clone, { move |i: I| { let mut parser = super::TagNoCase { @@ -359,7 +359,7 @@ where pub fn take_until>(tag: T) -> impl FnMut(I) -> IResult where I: Input + FindSubstring, - T: InputLength + Clone, + T: Input + Clone, { let mut parser = super::take_until(tag); @@ -388,7 +388,7 @@ where pub fn take_until1>(tag: T) -> impl FnMut(I) -> IResult where I: Input + FindSubstring, - T: InputLength + Clone, + T: Input + Clone, { let mut parser = super::take_until1(tag); diff --git a/src/bytes/mod.rs b/src/bytes/mod.rs index cf901b9f8..b720df394 100644 --- a/src/bytes/mod.rs +++ b/src/bytes/mod.rs @@ -11,7 +11,7 @@ use crate::error::ErrorKind; use crate::error::ParseError; use crate::internal::{Err, Needed, Parser}; use crate::lib::std::result::Result::*; -use crate::traits::{Compare, CompareResult, InputLength}; +use crate::traits::{Compare, CompareResult}; use crate::AsChar; use crate::Check; use crate::ExtendInto; @@ -45,7 +45,7 @@ use crate::ToUsize; pub fn tag>(tag: T) -> impl Parser where I: Input + Compare, - T: InputLength + Clone, + T: Input + Clone, { Tag { tag, @@ -62,7 +62,7 @@ pub struct Tag { impl, T> Parser for Tag where I: Input + Compare, - T: InputLength + Clone, + T: Input + Clone, { type Output = I; @@ -114,7 +114,7 @@ where pub fn tag_no_case>(tag: T) -> impl Parser where I: Input + Compare, - T: InputLength + Clone, + T: Input + Clone, { TagNoCase { tag, @@ -131,7 +131,7 @@ pub struct TagNoCase { impl, T> Parser for TagNoCase where I: Input + Compare, - T: InputLength + Clone, + T: Input + Clone, { type Output = I; diff --git a/src/bytes/streaming.rs b/src/bytes/streaming.rs index c32e97b85..7d131fa28 100644 --- a/src/bytes/streaming.rs +++ b/src/bytes/streaming.rs @@ -4,7 +4,7 @@ use core::marker::PhantomData; use crate::error::ParseError; use crate::internal::{IResult, Parser}; -use crate::traits::{Compare, FindSubstring, FindToken, InputLength, ToUsize}; +use crate::traits::{Compare, FindSubstring, FindToken, ToUsize}; use crate::Emit; use crate::Input; use crate::OutputM; @@ -31,7 +31,7 @@ use crate::Streaming; pub fn tag>(tag: T) -> impl Fn(I) -> IResult where I: Input + Compare, - T: InputLength + Clone, + T: Input + Clone, { move |i: I| { let mut parser = super::Tag { @@ -65,7 +65,7 @@ where pub fn tag_no_case>(tag: T) -> impl Fn(I) -> IResult where I: Input + Compare, - T: InputLength + Clone, + T: Input + Clone, { move |i: I| { let mut parser = super::TagNoCase { @@ -338,7 +338,7 @@ where /// ``` pub fn take>(count: C) -> impl FnMut(I) -> IResult where - I: Input + InputLength, + I: Input + Input, C: ToUsize, { let mut parser = super::take(count); diff --git a/src/bytes/tests.rs b/src/bytes/tests.rs index 15106bf32..3e76537ff 100644 --- a/src/bytes/tests.rs +++ b/src/bytes/tests.rs @@ -636,10 +636,10 @@ fn tag_fixed_size_array() { use crate::bytes::streaming::tag; fn test(i: &[u8]) -> IResult<&[u8], &[u8]> { - tag([0x42])(i) + tag(&[0x42][..])(i) } fn test2(i: &[u8]) -> IResult<&[u8], &[u8]> { - tag(&[0x42])(i) + tag(&[0x42][..])(i) } let input = [0x42, 0x00]; assert_eq!(test(&input), Ok((&b"\x00"[..], &b"\x42"[..]))); diff --git a/src/character/complete.rs b/src/character/complete.rs index 17b5eb29f..6b7a320dd 100644 --- a/src/character/complete.rs +++ b/src/character/complete.rs @@ -7,7 +7,7 @@ use crate::combinator::opt; use crate::error::ErrorKind; use crate::error::ParseError; use crate::internal::{Err, IResult}; -use crate::traits::{AsChar, FindToken, Input, InputLength}; +use crate::traits::{AsChar, FindToken, Input}; use crate::traits::{Compare, CompareResult}; use crate::Complete; use crate::Emit; @@ -208,7 +208,7 @@ where /// ``` pub fn line_ending>(input: T) -> IResult where - T: Input + InputLength, + T: Input + Input, T: Compare<&'static str>, { match input.compare("\n") { diff --git a/src/combinator/mod.rs b/src/combinator/mod.rs index bfd4a994a..96faa3faa 100644 --- a/src/combinator/mod.rs +++ b/src/combinator/mod.rs @@ -15,7 +15,7 @@ use crate::lib::std::convert::Into; use crate::lib::std::fmt::Debug; use crate::lib::std::mem::transmute; use crate::lib::std::ops::{Range, RangeFrom, RangeTo}; -use crate::traits::{AsChar, Input, InputLength, ParseTo}; +use crate::traits::{AsChar, Input, ParseTo}; use crate::traits::{Compare, CompareResult, Offset}; #[cfg(test)] @@ -48,7 +48,7 @@ where #[inline] pub fn rest_len>(input: T) -> IResult where - T: InputLength, + T: Input, { let len = input.input_len(); Ok((input, len)) @@ -363,7 +363,7 @@ where /// assert_eq!(parser(""), Ok(("", ""))); /// # } /// ``` -pub fn eof>(input: I) -> IResult { +pub fn eof>(input: I) -> IResult { if input.input_len() == 0 { let clone = input.clone(); Ok((input, clone)) @@ -443,7 +443,7 @@ pub fn all_consuming, F>( parser: F, ) -> impl Parser>::Output, Error = E> where - I: InputLength, + I: Input, F: Parser, { AllConsuming { parser } @@ -456,7 +456,7 @@ pub struct AllConsuming { impl Parser for AllConsuming where - I: InputLength, + I: Input, F: Parser, { type Output = >::Output; diff --git a/src/multi/mod.rs b/src/multi/mod.rs index ba67bc180..45168f5b2 100644 --- a/src/multi/mod.rs +++ b/src/multi/mod.rs @@ -12,16 +12,14 @@ use crate::internal::{Err, Needed, Parser}; use crate::lib::std::num::NonZeroUsize; #[cfg(feature = "alloc")] use crate::lib::std::vec::Vec; +use crate::traits::ToUsize; use crate::Check; use crate::Emit; use crate::Input; use crate::Mode; +use crate::NomRange; use crate::OutputM; use crate::OutputMode; -use crate::{ - traits::{InputLength, ToUsize}, - NomRange, -}; /// Don't pre-allocate more than 64KiB when calling `Vec::with_capacity`. /// @@ -66,7 +64,7 @@ pub fn many0( f: F, ) -> impl Parser>::Output>, Error = >::Error> where - I: Clone + InputLength, + I: Clone + Input, F: Parser, { Many0 { parser: f } @@ -81,7 +79,7 @@ pub struct Many0 { #[cfg(feature = "alloc")] impl Parser for Many0 where - I: Clone + InputLength, + I: Clone + Input, F: Parser, { type Output = crate::lib::std::vec::Vec<>::Output>; @@ -153,7 +151,7 @@ pub fn many1( parser: F, ) -> impl Parser>::Output>, Error = >::Error> where - I: Clone + InputLength, + I: Clone + Input, F: Parser, { Many1 { parser } @@ -168,7 +166,7 @@ pub struct Many1 { #[cfg(feature = "alloc")] impl Parser for Many1 where - I: Clone + InputLength, + I: Clone + Input, F: Parser, { type Output = Vec<>::Output>; @@ -255,7 +253,7 @@ pub fn many_till( g: G, ) -> impl Parser>::Output>, >::Output), Error = E> where - I: Clone + InputLength, + I: Clone + Input, F: Parser, G: Parser, E: ParseError, @@ -278,7 +276,7 @@ pub struct ManyTill { #[cfg(feature = "alloc")] impl Parser for ManyTill where - I: Clone + InputLength, + I: Clone + Input, F: Parser, G: Parser, E: ParseError, @@ -337,7 +335,7 @@ where /// [`cut`][crate::combinator::cut]. /// /// # Arguments -/// * `sep` Parses the separator between list elements. Must be consuming. +/// * `sep` Parses the separator between list elements. Must be consuming. /// * `f` Parses the elements of the list. /// /// ```rust @@ -362,7 +360,7 @@ pub fn separated_list0( f: F, ) -> impl Parser>::Output>, Error = E> where - I: Clone + InputLength, + I: Clone + Input, F: Parser, G: Parser, E: ParseError, @@ -383,7 +381,7 @@ pub struct SeparatedList0 { #[cfg(feature = "alloc")] impl, F, G> Parser for SeparatedList0 where - I: Clone + InputLength, + I: Clone + Input, F: Parser, G: Parser, { @@ -482,7 +480,7 @@ pub fn separated_list1( parser: F, ) -> impl Parser>::Output>, Error = E> where - I: Clone + InputLength, + I: Clone + Input, F: Parser, G: Parser, E: ParseError, @@ -500,7 +498,7 @@ pub struct SeparatedList1 { #[cfg(feature = "alloc")] impl, F, G> Parser for SeparatedList1 where - I: Clone + InputLength, + I: Clone + Input, F: Parser, G: Parser, { @@ -599,7 +597,7 @@ pub fn many_m_n( parser: F, ) -> impl Parser>::Output>, Error = E> where - I: Clone + InputLength, + I: Clone + Input, F: Parser, E: ParseError, { @@ -617,7 +615,7 @@ pub struct ManyMN { #[cfg(feature = "alloc")] impl Parser for ManyMN where - I: Clone + InputLength, + I: Clone + Input, F: Parser, { type Output = Vec<>::Output>; @@ -702,7 +700,7 @@ where /// ``` pub fn many0_count(parser: F) -> impl Parser where - I: Clone + InputLength, + I: Clone + Input, F: Parser, E: ParseError, { @@ -716,7 +714,7 @@ pub struct Many0Count { impl Parser for Many0Count where - I: Clone + InputLength, + I: Clone + Input, F: Parser, { type Output = usize; @@ -783,7 +781,7 @@ where /// ``` pub fn many1_count(parser: F) -> impl Parser where - I: Clone + InputLength, + I: Clone + Input, F: Parser, E: ParseError, { @@ -797,7 +795,7 @@ pub struct Many1Count { impl Parser for Many1Count where - I: Clone + InputLength, + I: Clone + Input, F: Parser, { type Output = usize; @@ -1048,7 +1046,7 @@ pub fn fold_many0( g: G, ) -> impl Parser where - I: Clone + InputLength, + I: Clone + Input, F: Parser, G: FnMut(R, >::Output) -> R, H: FnMut() -> R, @@ -1072,7 +1070,7 @@ pub struct FoldMany0 { impl Parser for FoldMany0 where - I: Clone + InputLength, + I: Clone + Input, F: Parser, G: FnMut(R, >::Output) -> R, Init: FnMut() -> R, @@ -1152,7 +1150,7 @@ pub fn fold_many1( g: G, ) -> impl Parser where - I: Clone + InputLength, + I: Clone + Input, F: Parser, G: FnMut(R, >::Output) -> R, H: FnMut() -> R, @@ -1176,7 +1174,7 @@ pub struct FoldMany1 { impl Parser for FoldMany1 where - I: Clone + InputLength, + I: Clone + Input, F: Parser, G: FnMut(R, >::Output) -> R, Init: FnMut() -> R, @@ -1274,7 +1272,7 @@ pub fn fold_many_m_n( g: G, ) -> impl Parser where - I: Clone + InputLength, + I: Clone + Input, F: Parser, G: FnMut(R, >::Output) -> R, H: FnMut() -> R, @@ -1302,7 +1300,7 @@ pub struct FoldManyMN { impl Parser for FoldManyMN where - I: Clone + InputLength, + I: Clone + Input, F: Parser, G: FnMut(R, >::Output) -> R, Init: FnMut() -> R, @@ -1668,7 +1666,7 @@ pub fn many( parser: F, ) -> impl Parser where - I: Clone + InputLength, + I: Clone + Input, F: Parser, Collection: Extend<>::Output> + Default, E: ParseError, @@ -1690,7 +1688,7 @@ pub struct Many { impl Parser for Many where - I: Clone + InputLength, + I: Clone + Input, F: Parser, Collection: Extend<>::Output> + Default, R: NomRange, @@ -1792,7 +1790,7 @@ pub fn fold( fold: G, ) -> impl Parser where - I: Clone + InputLength, + I: Clone + Input, F: Parser, G: FnMut(R, >::Output) -> R, H: FnMut() -> R, @@ -1817,7 +1815,7 @@ pub struct Fold { impl Parser for Fold where - I: Clone + InputLength, + I: Clone + Input, F: Parser, G: FnMut(Res, >::Output) -> Res, H: FnMut() -> Res, diff --git a/src/traits.rs b/src/traits.rs index a7fe99a6f..a49f0c2ff 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -552,36 +552,6 @@ impl<'a> Input for &'a str { } } -/// Abstract method to calculate the input length -pub trait InputLength { - /// Calculates the input length, as indicated by its name, - /// and the name of the trait itself - fn input_len(&self) -> usize; -} - -impl<'a, T> InputLength for &'a [T] { - #[inline] - fn input_len(&self) -> usize { - self.len() - } -} - -impl<'a> InputLength for &'a str { - #[inline] - fn input_len(&self) -> usize { - self.len() - } -} - -impl<'a> InputLength for (&'a [u8], usize) { - #[inline] - fn input_len(&self) -> usize { - //println!("bit input length for ({:?}, {}):", self.0, self.1); - //println!("-> {}", self.0.len() * 8 - self.1); - self.0.len() * 8 - self.1 - } -} - /// Useful functions to calculate the offset between slices and show a hexdump of a slice pub trait Offset { /// Offset between the first byte of self and the first byte of the argument @@ -1079,20 +1049,6 @@ impl<'a, R: FromStr> ParseTo for &'a str { } } -impl InputLength for [u8; N] { - #[inline] - fn input_len(&self) -> usize { - self.len() - } -} - -impl<'a, const N: usize> InputLength for &'a [u8; N] { - #[inline] - fn input_len(&self) -> usize { - self.len() - } -} - impl<'a, const N: usize> Compare<[u8; N]> for &'a [u8] { #[inline(always)] fn compare(&self, t: [u8; N]) -> CompareResult { diff --git a/tests/issues.rs b/tests/issues.rs index fad916c47..ecff7b3a2 100644 --- a/tests/issues.rs +++ b/tests/issues.rs @@ -105,7 +105,7 @@ fn issue_717(i: &[u8]) -> IResult<&[u8], Vec<&[u8]>> { use nom::bytes::complete::{is_not, tag}; use nom::multi::separated_list0; - separated_list0(tag([0x0]), is_not([0x0u8])).parse(i) + separated_list0(tag(&[0x0][..]), is_not([0x0u8])).parse(i) } mod issue_647 { From 76ec478d1261e731580c9dfabd7aba557b39f5bd Mon Sep 17 00:00:00 2001 From: Geoffroy Couprie Date: Sun, 5 May 2024 15:44:04 +0200 Subject: [PATCH 2/2] remove redundant bounds --- src/bytes/streaming.rs | 2 +- src/character/complete.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/bytes/streaming.rs b/src/bytes/streaming.rs index 7d131fa28..081643669 100644 --- a/src/bytes/streaming.rs +++ b/src/bytes/streaming.rs @@ -338,7 +338,7 @@ where /// ``` pub fn take>(count: C) -> impl FnMut(I) -> IResult where - I: Input + Input, + I: Input, C: ToUsize, { let mut parser = super::take(count); diff --git a/src/character/complete.rs b/src/character/complete.rs index 6b7a320dd..6dc334995 100644 --- a/src/character/complete.rs +++ b/src/character/complete.rs @@ -208,7 +208,7 @@ where /// ``` pub fn line_ending>(input: T) -> IResult where - T: Input + Input, + T: Input, T: Compare<&'static str>, { match input.compare("\n") {