From bfae0b8642879af279ee4d2c4969189110c8a739 Mon Sep 17 00:00:00 2001 From: John Schug Date: Fri, 15 Dec 2023 20:44:15 -0800 Subject: [PATCH] Add various TryFrom impls for Data --- src/data.rs | 138 +++++++++++++++++++++++++++++++++++----------------- 1 file changed, 94 insertions(+), 44 deletions(-) diff --git a/src/data.rs b/src/data.rs index c07f44e4..a97d3685 100644 --- a/src/data.rs +++ b/src/data.rs @@ -561,90 +561,140 @@ impl<'a> IntoData<'a> for &mut Data<'a> { } } -impl<'a> IntoData<'a> for Data<'a> { - type Output = Self; +impl<'a, T> IntoData<'a> for T +where + T: TryInto, Error = Error>, +{ + type Output = Data<'a>; - fn into_data(self) -> Result { - Ok(self) + fn into_data(self) -> Result { + self.try_into() } } -impl<'a> IntoData<'a> for &'a [u8] { - type Output = Data<'a>; +impl<'a> TryFrom<&'a [u8]> for Data<'a> { + type Error = Error; - fn into_data(self) -> Result> { - Data::from_seekable_reader(Cursor::new(self)).map_err(|e| e.error()) + #[inline] + fn try_from(value: &'a [u8]) -> Result { + Self::from_buffer(value) } } -impl<'a> IntoData<'a> for &'a mut [u8] { - type Output = Data<'a>; +impl<'a> TryFrom<&'a mut [u8]> for Data<'a> { + type Error = Error; - fn into_data(self) -> Result> { - Data::from_seekable_stream(Cursor::new(self)).map_err(|e| e.error()) + #[inline] + fn try_from(value: &'a mut [u8]) -> Result { + Self::from_seekable_stream(Cursor::new(value)).map_err(|e| e.error()) } } -impl<'a> IntoData<'a> for &'a Vec { - type Output = Data<'a>; +impl<'a> TryFrom<&'a str> for Data<'a> { + type Error = Error; - fn into_data(self) -> Result> { - self.as_slice().into_data() + #[inline] + fn try_from(value: &'a str) -> Result { + value.as_bytes().try_into() } } -impl<'a> IntoData<'a> for &'a mut Vec { - type Output = Data<'a>; +impl<'a> TryFrom<&'a Vec> for Data<'a> { + type Error = Error; - fn into_data(self) -> Result> { - Data::from_seekable_stream(Cursor::new(self)).map_err(|e| e.error()) + #[inline] + fn try_from(value: &'a Vec) -> Result { + value.as_slice().try_into() } } -impl IntoData<'static> for Vec { - type Output = Data<'static>; +impl<'a> TryFrom<&'a mut Vec> for Data<'a> { + type Error = Error; - fn into_data(self) -> Result> { - Data::from_seekable_stream(Cursor::new(self)).map_err(|e| e.error()) + #[inline] + fn try_from(value: &'a mut Vec) -> Result { + Self::from_seekable_stream(Cursor::new(value)).map_err(|e| e.error()) } } -impl<'a> IntoData<'a> for &'a str { - type Output = Data<'a>; +impl<'a> TryFrom> for Data<'a> { + type Error = Error; - fn into_data(self) -> Result> { - self.as_bytes().into_data() + #[inline] + fn try_from(value: Vec) -> Result { + Self::from_seekable_stream(Cursor::new(value)).map_err(|e| e.error()) } } -impl IntoData<'static> for String { - type Output = Data<'static>; +impl<'a> TryFrom for Data<'a> { + type Error = Error; - fn into_data(self) -> Result> { - self.into_bytes().into_data() + #[inline] + fn try_from(value: String) -> Result { + value.into_bytes().try_into() } } -impl<'a> IntoData<'a> for &'a File { - type Output = Data<'a>; +impl<'a> TryFrom<&'a File> for Data<'a> { + type Error = Error; - fn into_data(self) -> Result> { - Data::from_seekable_stream(self).map_err(|e| e.error()) + #[inline] + fn try_from(value: &'a File) -> Result { + Self::from_seekable_stream(value).map_err(|e| e.error()) } } -impl<'a> IntoData<'a> for &'a mut File { - type Output = Data<'a>; +impl<'a> TryFrom<&'a mut File> for Data<'a> { + type Error = Error; + + #[inline] + fn try_from(value: &'a mut File) -> Result { + Self::try_from(&*value) + } +} + +impl<'a> TryFrom for Data<'a> { + type Error = Error; - fn into_data(self) -> Result> { - Data::from_seekable_stream(self).map_err(|e| e.error()) + #[inline] + fn try_from(value: File) -> Result { + Self::from_seekable_stream(value).map_err(|e| e.error()) } } -impl IntoData<'static> for File { - type Output = Data<'static>; +impl TryFrom for Data<'static> { + type Error = Error; - fn into_data(self) -> Result> { - Data::from_seekable_stream(self).map_err(|e| e.error()) + #[inline] + fn try_from(value: io::Stdout) -> Result { + Self::from_writer(value).map_err(|e| e.error()) + } +} + +impl TryFrom for Data<'static> { + type Error = Error; + + #[inline] + fn try_from(value: io::Stderr) -> Result { + Self::from_writer(value).map_err(|e| e.error()) + } +} + +impl TryFrom for Data<'static> { + type Error = Error; + + #[inline] + fn try_from(value: io::Stdin) -> Result { + Self::from_reader(value).map_err(|e| e.error()) + } +} + +#[cfg(unix)] +impl<'a> TryFrom> for Data<'a> { + type Error = Error; + + #[inline] + fn try_from(value: BorrowedFd<'a>) -> Result { + Data::from_borrowed_fd(value) } }