diff --git a/src/stream/mod.rs b/src/stream/mod.rs index 82acf7331..00ec8a6ab 100644 --- a/src/stream/mod.rs +++ b/src/stream/mod.rs @@ -2186,15 +2186,37 @@ pub trait Compare { fn compare(&self, t: T) -> CompareResult; } -impl<'a, 'b> Compare<&'b [u8]> for &'a [u8] { - #[inline] - fn compare(&self, t: &'b [u8]) -> CompareResult { - if t.iter().zip(*self).any(|(a, b)| a != b) { +impl Compare<&str> for T +where + T: AsRef<[u8]>, +{ + fn compare(&self, t: &str) -> CompareResult { + let self_bytes = self.as_ref(); + let t_bytes = t.as_bytes(); + + if t_bytes.iter().zip(self_bytes).any(|(a, b)| a != b) { CompareResult::Error - } else if self.len() < t.slice_len() { + } else if self_bytes.len() < t_bytes.len() { CompareResult::Incomplete } else { - CompareResult::Ok(t.slice_len()) + CompareResult::Ok(t_bytes.len()) + } + } +} + +impl Compare<&[u8]> for T +where + T: AsRef<[u8]>, +{ + fn compare(&self, t: &[u8]) -> CompareResult { + let self_bytes = self.as_ref(); + + if t.iter().zip(self_bytes).any(|(a, b)| a != b) { + CompareResult::Error + } else if self_bytes.len() < t.len() { + CompareResult::Incomplete + } else { + CompareResult::Ok(t.len()) } } } @@ -2244,13 +2266,6 @@ impl<'a, 'b, const LEN: usize> Compare> for &'a [u8 } } -impl<'a, 'b> Compare<&'b str> for &'a [u8] { - #[inline(always)] - fn compare(&self, t: &'b str) -> CompareResult { - self.compare(t.as_bytes()) - } -} - impl<'a, 'b> Compare> for &'a [u8] { #[inline(always)] fn compare(&self, t: AsciiCaseless<&'b str>) -> CompareResult { @@ -2294,13 +2309,6 @@ impl<'a> Compare> for &'a [u8] { } } -impl<'a, 'b> Compare<&'b str> for &'a str { - #[inline(always)] - fn compare(&self, t: &'b str) -> CompareResult { - self.as_bytes().compare(t.as_bytes()) - } -} - impl<'a, 'b> Compare> for &'a str { #[inline(always)] fn compare(&self, t: AsciiCaseless<&'b str>) -> CompareResult { @@ -2322,28 +2330,6 @@ impl<'a> Compare> for &'a str { } } -impl<'a, T> Compare for &'a Bytes -where - &'a [u8]: Compare, -{ - #[inline(always)] - fn compare(&self, t: T) -> CompareResult { - let bytes = (*self).as_bytes(); - bytes.compare(t) - } -} - -impl<'a, T> Compare for &'a BStr -where - &'a [u8]: Compare, -{ - #[inline(always)] - fn compare(&self, t: T) -> CompareResult { - let bytes = (*self).as_bytes(); - bytes.compare(t) - } -} - impl Compare for Located where I: Compare,