@@ -14,6 +14,7 @@ pub type ParseResult<'i, T> = Result<(T, &'i [u8]), (ParseError, &'i [u8])>;
1414/// Parser trait.
1515///
1616/// Implementations should avoid allocating where possible.
17+ #[ must_use]
1718pub trait Parser < ' i > : Sized {
1819 /// Type of the value produced by [`parse`](Self::parse) when successful.
1920 type Output ;
@@ -54,6 +55,7 @@ pub trait Parser<'i>: Sized {
5455 /// Ok(((123, -123), &b""[..]))
5556 /// );
5657 /// ```
58+ #[ inline]
5759 fn then < T : Parser < ' i > > ( self , next : T ) -> Self :: Then < T > {
5860 Then :: then ( self , next)
5961 }
@@ -81,6 +83,7 @@ pub trait Parser<'i>: Sized {
8183 /// Ok((1000, &b""[..]))
8284 /// );
8385 /// ```
86+ #[ inline]
8487 fn or < T : Parser < ' i , Output = Self :: Output > > ( self , alternative : T ) -> Or < Self , T > {
8588 Or {
8689 first : self ,
@@ -112,6 +115,7 @@ pub trait Parser<'i>: Sized {
112115 /// Ok(((&b"123"[..], &[1, 2, 3][..]), &b""[..]))
113116 /// );
114117 /// ```
118+ #[ inline]
115119 fn map < O , F : Fn ( Self :: Output ) -> O > ( self , f : F ) -> Map < Self , F > {
116120 Map {
117121 parser : self ,
@@ -155,6 +159,7 @@ pub trait Parser<'i>: Sized {
155159 /// Ok(((&b"123"[..], &[1, 2, 3][..]), &b""[..]))
156160 /// );
157161 /// ```
162+ #[ inline]
158163 fn map_res < O , F : Fn ( Self :: Output ) -> Result < O , & ' static str > > (
159164 self ,
160165 f : F ,
@@ -181,6 +186,7 @@ pub trait Parser<'i>: Sized {
181186 /// Ok((None, &b"abc"[..]))
182187 /// );
183188 /// ```
189+ #[ inline]
184190 fn optional ( self ) -> Optional < Self > {
185191 Optional { parser : self }
186192 }
@@ -200,6 +206,7 @@ pub trait Parser<'i>: Sized {
200206 /// Ok(([12, 34, 56], &b""[..]))
201207 /// );
202208 /// ```
209+ #[ inline]
203210 fn repeat_n < const N : usize , S : Parser < ' i > > ( self , separator : S ) -> RepeatN < N , Self , S >
204211 where
205212 Self :: Output : Copy + Default ,
@@ -225,6 +232,7 @@ pub trait Parser<'i>: Sized {
225232 /// assert_eq!(parser.parse(b"12,34,56,78"), Ok((vec![12, 34, 56, 78], &b""[..])));
226233 /// assert!(parser.parse(b"12,34").is_err());
227234 /// ```
235+ #[ inline]
228236 fn repeat_arrayvec < const N : usize , S : Parser < ' i > > (
229237 self ,
230238 separator : S ,
@@ -254,6 +262,7 @@ pub trait Parser<'i>: Sized {
254262 /// assert_eq!(parser.parse(b"12,34,56,78"), Ok((vec![12, 34, 56, 78], &b""[..])));
255263 /// assert!(parser.parse(b"12,34").is_err());
256264 /// ```
265+ #[ inline]
257266 fn repeat < S : Parser < ' i > > ( self , separator : S , min_elements : usize ) -> RepeatVec < Self , S > {
258267 RepeatVec {
259268 parser : self ,
@@ -275,6 +284,7 @@ pub trait Parser<'i>: Sized {
275284 /// Ok(((12, &b"012"[..]), &b",345,678"[..]))
276285 /// );
277286 /// ```
287+ #[ inline]
278288 fn with_consumed ( self ) -> WithConsumed < Self > {
279289 WithConsumed { parser : self }
280290 }
@@ -293,6 +303,7 @@ pub trait Parser<'i>: Sized {
293303 /// Ok((123, &b""[..]))
294304 /// );
295305 /// ```
306+ #[ inline]
296307 fn with_prefix < T : Parser < ' i > > ( self , prefix : T ) -> WithPrefix < Self , T > {
297308 WithPrefix {
298309 parser : self ,
@@ -314,13 +325,35 @@ pub trait Parser<'i>: Sized {
314325 /// Ok((123, &b""[..]))
315326 /// );
316327 /// ```
328+ #[ inline]
317329 fn with_suffix < T : Parser < ' i > > ( self , suffix : T ) -> WithSuffix < Self , T > {
318330 WithSuffix {
319331 parser : self ,
320332 suffix,
321333 }
322334 }
323335
336+ /// Parse a end of line (or end of string) after this parser.
337+ ///
338+ /// Equivalent to [`parser.with_suffix`](Parser::with_suffix)`(`[`parser::eol()`](super::eol)`)`.
339+ ///
340+ /// # Examples
341+ /// ```
342+ /// # use utils::parser::{self, Parser};
343+ /// assert_eq!(
344+ /// parser::u32().with_eol()
345+ /// .parse(b"123\nabc"),
346+ /// Ok((123, &b"abc"[..]))
347+ /// );
348+ /// ```
349+ #[ inline]
350+ fn with_eol ( self ) -> WithSuffix < Self , Eol > {
351+ WithSuffix {
352+ parser : self ,
353+ suffix : Eol ( ) ,
354+ }
355+ }
356+
324357 /// Replace this parser's error message with the provided string.
325358 ///
326359 /// # Examples
@@ -337,6 +370,7 @@ pub trait Parser<'i>: Sized {
337370 /// Err((ParseError::Custom("expected power level"), &b"abc"[..]))
338371 /// );
339372 /// ```
373+ #[ inline]
340374 fn error_msg ( self , message : & ' static str ) -> WithErrorMsg < Self > {
341375 WithErrorMsg {
342376 parser : self ,
@@ -352,6 +386,7 @@ pub trait Parser<'i>: Sized {
352386 /// assert_eq!(parser::u32().parse_complete("1234").unwrap(), 1234);
353387 /// assert!(parser::u32().parse_complete("1234abc").is_err());
354388 /// ```
389+ #[ inline]
355390 fn parse_complete ( & self , input : & ' i str ) -> Result < Self :: Output , InputError > {
356391 match self . parse ( input. as_bytes ( ) ) . map_with_input ( input) ? {
357392 ( v, [ ] ) => Ok ( v) ,
@@ -379,6 +414,7 @@ pub trait Parser<'i>: Sized {
379414 /// ]
380415 /// );
381416 /// ```
417+ #[ inline]
382418 fn parse_all ( & self , input : & ' i str ) -> Result < Vec < Self :: Output > , InputError > {
383419 ParserRef ( self )
384420 . repeat ( Constant ( ( ) ) , 0 )
@@ -387,7 +423,7 @@ pub trait Parser<'i>: Sized {
387423
388424 /// Similar to [`parse_all`](Self::parse_all) but expects a newline after each item.
389425 ///
390- /// Equivalent to `parser.with_suffix(`[`parser::eol( )`](super::eol)`) .parse_all(input)`.
426+ /// Equivalent to [ `parser.with_eol( )`](Parser::with_eol)` .parse_all(input)`.
391427 ///
392428 /// # Examples
393429 /// ```
@@ -404,6 +440,7 @@ pub trait Parser<'i>: Sized {
404440 /// ]
405441 /// );
406442 /// ```
443+ #[ inline]
407444 fn parse_lines ( & self , input : & ' i str ) -> Result < Vec < Self :: Output > , InputError > {
408445 ParserRef ( self )
409446 . with_suffix ( Eol ( ) )
@@ -423,7 +460,7 @@ pub trait Parser<'i>: Sized {
423460 /// # use utils::input::InputError;
424461 /// # use utils::parser::{self, Parser};
425462 /// let iterator = parser::u32()
426- /// .with_suffix(parser::eol() )
463+ /// .with_eol( )
427464 /// .parse_iterator("12\n34\n56\n78");
428465 /// for item in iterator {
429466 /// println!("{}", item?);
@@ -434,7 +471,7 @@ pub trait Parser<'i>: Sized {
434471 /// ```
435472 /// # use utils::parser::{self, Parser};
436473 /// let mut iterator = parser::u32()
437- /// .with_suffix(parser::eol() )
474+ /// .with_eol( )
438475 /// .parse_iterator("12\n34\nnot a integer");
439476 /// assert_eq!(iterator.next().unwrap().unwrap(), 12);
440477 /// assert_eq!(iterator.next().unwrap().unwrap(), 34);
@@ -446,13 +483,14 @@ pub trait Parser<'i>: Sized {
446483 /// # use utils::input::InputError;
447484 /// # use utils::parser::{self, Parser};
448485 /// let filtered = parser::u32()
449- /// .with_suffix(parser::eol() )
486+ /// .with_eol( )
450487 /// .parse_iterator("11\n22\n33\n44\n55")
451488 /// .filter(|r| r.is_err() || r.as_ref().is_ok_and(|v| v % 2 == 0))
452489 /// .collect::<Result<Vec<u32>, InputError>>()?;
453490 /// assert_eq!(filtered, vec![22, 44]);
454491 /// # Ok::<(), InputError>(())
455492 /// ```
493+ #[ inline]
456494 fn parse_iterator ( self , input : & str ) -> ParserIterator < ' _ , Self > {
457495 ParserIterator {
458496 input,
@@ -477,6 +515,7 @@ pub trait Parser<'i>: Sized {
477515 /// vec![123, 456, 7, 8, 9]
478516 /// );
479517 /// ```
518+ #[ inline]
480519 fn matches_iterator ( self , input : & str ) -> ParserMatchesIterator < ' _ , Self > {
481520 ParserMatchesIterator {
482521 remaining : input. as_bytes ( ) ,
0 commit comments