mirror of
https://github.com/openharmony/third_party_rust_os_str_bytes.git
synced 2026-07-20 01:23:35 -04:00
Minor improvements
This commit is contained in:
@@ -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<u8>`]: https://doc.rust-lang.org/std/vec/struct.Vec.html
|
||||
|
||||
|
||||
+6
-4
@@ -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<u8>) -> 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<u8> {
|
||||
::std::os::unix::ffi::OsStringExt::into_vec(self)
|
||||
OsStringExt::into_vec(self)
|
||||
}
|
||||
}
|
||||
|
||||
+8
-8
@@ -59,17 +59,17 @@ fn encode_utf16(string: &[u8]) -> Vec<u16> {
|
||||
impl OsStrBytes for OsStr {
|
||||
#[inline]
|
||||
fn from_bytes(string: &[u8]) -> Result<Cow<'_, Self>, 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<u8>) -> Result<Self, EncodingError> {
|
||||
Self::from_bytes(string)
|
||||
OsStringBytes::from_bytes(string)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
unsafe fn from_vec_unchecked(string: Vec<u8>) -> Self {
|
||||
Self::from_bytes_unchecked(string)
|
||||
OsStringBytes::from_bytes_unchecked(string)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn into_vec(self) -> Vec<u8> {
|
||||
self.to_bytes().into_owned()
|
||||
OsStrBytes::to_bytes(self.as_os_str()).into_owned()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+8
-3
@@ -17,17 +17,22 @@ fn random_os_string(buffer_length: usize) -> Result<OsString, GetRandomError> {
|
||||
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))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user