From 60fe70d2c5b99b5713ecfe4a1efa01b04f25fb05 Mon Sep 17 00:00:00 2001 From: sftse Date: Sun, 9 Feb 2025 15:53:53 +0100 Subject: [PATCH] fix: express custom trait with stdlib traits (#101) --- src/parsers/message.rs | 42 ++++-------------------------------------- 1 file changed, 4 insertions(+), 38 deletions(-) diff --git a/src/parsers/message.rs b/src/parsers/message.rs index 926edd9..8e8c6ac 100644 --- a/src/parsers/message.rs +++ b/src/parsers/message.rs @@ -108,17 +108,17 @@ impl MessageParser { /// This function never panics, a best-effort is made to parse the message and /// if no headers are found None is returned. /// - pub fn parse<'x>(&self, raw_message: impl IntoByteSlice<'x>) -> Option> { - self.parse_(raw_message.into_byte_slice(), MAX_NESTED_ENCODED, false) + pub fn parse<'x>(&self, raw_message: &'x (impl AsRef<[u8]> + ?Sized)) -> Option> { + self.parse_(raw_message.as_ref(), MAX_NESTED_ENCODED, false) } /// Parses a byte slice containing the RFC5322 raw message and returns a /// `Message` struct containing only the headers. pub fn parse_headers<'x>( &self, - raw_message: impl IntoByteSlice<'x> + 'x, + raw_message: &'x (impl AsRef<[u8]> + ?Sized), ) -> Option> { - self.parse_(raw_message.into_byte_slice(), MAX_NESTED_ENCODED, true) + self.parse_(raw_message.as_ref(), MAX_NESTED_ENCODED, true) } fn parse_<'x>( @@ -549,40 +549,6 @@ impl<'x> Message<'x> { } } -pub trait IntoByteSlice<'x> { - fn into_byte_slice(self) -> &'x [u8]; -} - -impl<'x> IntoByteSlice<'x> for &'x [u8] { - fn into_byte_slice(self) -> &'x [u8] { - self - } -} - -impl<'x, const N: usize> IntoByteSlice<'x> for &'x [u8; N] { - fn into_byte_slice(self) -> &'x [u8] { - self - } -} - -impl<'x> IntoByteSlice<'x> for &'x str { - fn into_byte_slice(self) -> &'x [u8] { - self.as_bytes() - } -} - -impl<'x> IntoByteSlice<'x> for &'x String { - fn into_byte_slice(self) -> &'x [u8] { - self.as_bytes() - } -} - -impl<'x> IntoByteSlice<'x> for &'x Vec { - fn into_byte_slice(self) -> &'x [u8] { - self - } -} - #[cfg(test)] mod tests { use std::{fs, path::PathBuf};