diff --git a/src/lib.rs b/src/lib.rs index a0155b7..3ccc9fc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -36,6 +36,13 @@ //! are no methods on [`u32`] for encoding. Tests exist to validate the //! implementation in this crate. //! +//! # Related Crates +//! +//! - [print_bytes] - +//! Assists in writing the stored bytes to an output stream, since some +//! terminals require unicode. Internally, it uses this crate for some of its +//! conversions. +//! //! # Examples //! //! ``` @@ -76,6 +83,7 @@ //! [`OsString`]: https://doc.rust-lang.org/std/ffi/struct.OsString.html //! [`OsStringBytes::from_bytes`]: trait.OsStringBytes.html#tymethod.from_bytes //! [`OsStringBytes::from_vec`]: trait.OsStringBytes.html#tymethod.from_vec +//! [print_bytes]: https://crates.io/crates/print_bytes //! [`u32`]: https://doc.rust-lang.org/std/primitive.u32.html //! [`Vec`]: https://doc.rust-lang.org/std/vec/struct.Vec.html diff --git a/src/unix.rs b/src/unix.rs index bf06b63..62cd0bc 100644 --- a/src/unix.rs +++ b/src/unix.rs @@ -1,6 +1,8 @@ use std::borrow::Cow; use std::ffi::OsStr; use std::ffi::OsString; +use std::os::unix::ffi::OsStrExt; +use std::os::unix::ffi::OsStringExt; use crate::EncodingError; use crate::OsStrBytes; @@ -8,12 +10,12 @@ use crate::OsStringBytes; #[inline] fn from_bytes(string: &[u8]) -> Cow<'_, OsStr> { - Cow::Borrowed(::std::os::unix::ffi::OsStrExt::from_bytes(string)) + Cow::Borrowed(OsStrExt::from_bytes(string)) } #[inline] fn from_vec(string: Vec) -> OsString { - ::std::os::unix::ffi::OsStringExt::from_vec(string) + OsStringExt::from_vec(string) } impl OsStrBytes for OsStr { @@ -29,7 +31,7 @@ impl OsStrBytes for OsStr { #[inline] fn to_bytes(&self) -> Cow<'_, [u8]> { - Cow::Borrowed(::std::os::unix::ffi::OsStrExt::as_bytes(self)) + Cow::Borrowed(OsStrExt::as_bytes(self)) } } @@ -62,6 +64,6 @@ impl OsStringBytes for OsString { #[inline] fn into_vec(self) -> Vec { - ::std::os::unix::ffi::OsStringExt::into_vec(self) + OsStringExt::into_vec(self) } } diff --git a/src/windows/mod.rs b/src/windows/mod.rs index 903d8a1..ce08817 100644 --- a/src/windows/mod.rs +++ b/src/windows/mod.rs @@ -59,17 +59,17 @@ fn encode_utf16(string: &[u8]) -> Vec { impl OsStrBytes for OsStr { #[inline] fn from_bytes(string: &[u8]) -> Result, EncodingError> { - Ok(Cow::Owned(OsString::from_bytes(string)?)) + Ok(Cow::Owned(OsStringBytes::from_bytes(string)?)) } #[inline] unsafe fn from_bytes_unchecked(string: &[u8]) -> Cow<'_, Self> { - Cow::Owned(OsString::from_bytes_unchecked(string)) + Cow::Owned(OsStringBytes::from_bytes_unchecked(string)) } #[inline] fn to_bytes(&self) -> Cow<'_, [u8]> { - Cow::Owned(decode_utf16(self.encode_wide(), self.len())) + Cow::Owned(decode_utf16(OsStrExt::encode_wide(self), self.len())) } } @@ -85,7 +85,7 @@ impl OsStringBytes for OsString { if decode_utf16(encoded_string.iter().map(|&x| x), string.len()) == string { - Ok(Self::from_wide(&encoded_string)) + Ok(OsStringExt::from_wide(&encoded_string)) } else { Err(EncodingError(())) } @@ -96,22 +96,22 @@ impl OsStringBytes for OsString { where TString: AsRef<[u8]>, { - Self::from_wide(&encode_utf16(string.as_ref())) + OsStringExt::from_wide(&encode_utf16(string.as_ref())) } #[inline] fn from_vec(string: Vec) -> Result { - Self::from_bytes(string) + OsStringBytes::from_bytes(string) } #[inline] unsafe fn from_vec_unchecked(string: Vec) -> Self { - Self::from_bytes_unchecked(string) + OsStringBytes::from_bytes_unchecked(string) } #[inline] fn into_vec(self) -> Vec { - self.to_bytes().into_owned() + OsStrBytes::to_bytes(self.as_os_str()).into_owned() } } diff --git a/tests/random.rs b/tests/random.rs index a995b55..2ab67d0 100644 --- a/tests/random.rs +++ b/tests/random.rs @@ -17,17 +17,22 @@ fn random_os_string(buffer_length: usize) -> Result { let mut buffer = vec![0; buffer_length]; #[cfg(unix)] { + use std::os::unix::ffi::OsStringExt; + getrandom(&mut buffer)?; - Ok(::std::os::unix::ffi::OsStringExt::from_vec(buffer)) + Ok(OsStringExt::from_vec(buffer)) } #[cfg(windows)] { + use std::mem; + use std::os::windows::ffi::OsStringExt; + // SAFETY: These bytes are random, so their values are arbitrary. getrandom(unsafe { #[allow(clippy::transmute_ptr_to_ptr)] - ::std::mem::transmute::<&mut [u16], &mut [u8]>(&mut buffer) + mem::transmute::<&mut [u16], &mut [u8]>(&mut buffer) })?; - Ok(::std::os::windows::ffi::OsStringExt::from_wide(&buffer)) + Ok(OsStringExt::from_wide(&buffer)) } }