From 7b83de1336eb651a17f2bf5765a6952f50c2694f Mon Sep 17 00:00:00 2001 From: Marijn Schouten Date: Tue, 22 Oct 2024 22:27:14 +0200 Subject: [PATCH] Inline peek_ahead to fix 186 and remove iter::peek_ahead as it is now unused. --- src/iter.rs | 13 ------------- src/lib.rs | 6 +++++- 2 files changed, 5 insertions(+), 14 deletions(-) diff --git a/src/iter.rs b/src/iter.rs index 85d9e79..2dde2e7 100644 --- a/src/iter.rs +++ b/src/iter.rs @@ -40,19 +40,6 @@ impl<'a> Bytes<'a> { None } } - - #[inline] - pub fn peek_ahead(&self, n: usize) -> Option { - // SAFETY: obtain a potentially OOB pointer that is later compared against the `self.end` - // pointer. - let ptr = self.cursor.wrapping_add(n); - if ptr < self.end { - // SAFETY: bounds checked pointer dereference is safe - Some(unsafe { *ptr }) - } else { - None - } - } #[inline] pub fn peek_n<'b: 'a, U: TryFrom<&'a [u8]>>(&'b self, n: usize) -> Option { diff --git a/src/lib.rs b/src/lib.rs index 4ccd783..1eb9724 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -853,7 +853,11 @@ pub fn parse_method<'a>(bytes: &mut Bytes<'a>) -> Result<&'a str> { }; Ok(Status::Complete(method)) } - Some(POST) if bytes.peek_ahead(4) == Some(b' ') => { + Some(POST) + if unsafe { + (bytes.cursor.add(4) < bytes.end).then(|| *bytes.cursor.add(4)) == Some(b' ') + } => + { // SAFETY: matched the ASCII string and boundary checked let method = unsafe { bytes.advance(5);