fix: express custom trait with stdlib traits (#101)

This commit is contained in:
sftse
2025-02-09 15:53:53 +01:00
committed by GitHub
parent 209778f080
commit 60fe70d2c5
+4 -38
View File
@@ -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<Message<'x>> {
self.parse_(raw_message.into_byte_slice(), MAX_NESTED_ENCODED, false)
pub fn parse<'x>(&self, raw_message: &'x (impl AsRef<[u8]> + ?Sized)) -> Option<Message<'x>> {
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<Message<'x>> {
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<u8> {
fn into_byte_slice(self) -> &'x [u8] {
self
}
}
#[cfg(test)]
mod tests {
use std::{fs, path::PathBuf};