Skip to content

Commit

Permalink
Minor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
dylni committed Nov 8, 2023
1 parent 53cd3b5 commit aa81bae
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 74 deletions.
12 changes: 6 additions & 6 deletions src/common/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@ pub(crate) type EncodingError = Infallible;
pub(crate) type Result<T> = result::Result<T, EncodingError>;

pub(crate) fn os_str_from_bytes(string: &[u8]) -> Result<Cow<'_, OsStr>> {
Ok(Cow::Borrowed(OsStrExt::from_bytes(string)))
Ok(Cow::Borrowed(OsStr::from_bytes(string)))
}

pub(crate) fn os_str_to_bytes(os_string: &OsStr) -> Cow<'_, [u8]> {
Cow::Borrowed(os_string.as_bytes())
pub(crate) fn os_str_to_bytes(string: &OsStr) -> Cow<'_, [u8]> {
Cow::Borrowed(string.as_bytes())
}

pub(crate) fn os_string_from_vec(string: Vec<u8>) -> Result<OsString> {
Ok(OsStringExt::from_vec(string))
Ok(OsString::from_vec(string))
}

pub(crate) fn os_string_into_vec(os_string: OsString) -> Vec<u8> {
os_string.into_vec()
pub(crate) fn os_string_into_vec(string: OsString) -> Vec<u8> {
string.into_vec()
}
6 changes: 3 additions & 3 deletions src/common/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
if_conversions! {
pub(super) mod convert;
}

if_raw_str! {
pub(super) mod raw;
if_raw_str! {
pub(super) mod raw;
}
}
12 changes: 4 additions & 8 deletions src/common/raw.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
if_conversions! {
pub(crate) fn ends_with(string: &[u8], suffix: &[u8]) -> bool {
string.ends_with(suffix)
}
pub(crate) fn ends_with(string: &[u8], suffix: &[u8]) -> bool {
string.ends_with(suffix)
}

if_conversions! {
pub(crate) fn starts_with(string: &[u8], prefix: &[u8]) -> bool {
string.starts_with(prefix)
}
pub(crate) fn starts_with(string: &[u8], prefix: &[u8]) -> bool {
string.starts_with(prefix)
}
20 changes: 1 addition & 19 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@
//! Provides:
//! - [`iter`]
//! - [`NonUnicodeOsStr`]
//! - [`Pattern`]
//! - [`OsStrBytesExt`]
//! - [`Pattern`]
//! - [`RawOsStr`]
//! - [`RawOsStrCow`]
//! - [`RawOsString`]
Expand Down Expand Up @@ -238,15 +238,6 @@ if_checked_conversions! {
);
}

macro_rules! if_nightly {
( $($item:item)+ ) => {
$(
#[cfg(feature = "nightly")]
$item
)+
};
}

macro_rules! r#impl {
( $($feature:literal),+ ) => {
$(
Expand All @@ -265,15 +256,6 @@ macro_rules! r#impl {
}
r#impl!("memchr", "print_bytes", "uniquote");

#[cfg(not(os_str_bytes_docs_rs))]
if_nightly! {
const _: &str = env!(
"OS_STR_BYTES_NIGHTLY",
"The 'OS_STR_BYTES_NIGHTLY' environment variable must be defined to \
use the 'nightly' feature.",
);
}

macro_rules! if_raw_str {
( $($item:item)+ ) => {
$(
Expand Down
8 changes: 3 additions & 5 deletions src/raw_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1330,18 +1330,16 @@ where
}

macro_rules! r#impl {
( $(#[$attr:meta])* $type:ty , $other_type:ty ) => {
$(#[$attr])*
( $type:ty , $other_type:ty ) => {
impl PartialEq<$other_type> for $type {
#[inline]
fn eq(&self, other: &$other_type) -> bool {
let raw: &RawOsStr = self;
let other: &RawOsStr = other.as_ref();
let raw: &OsStr = self.as_ref();
let other: &OsStr = other.as_ref();
raw == other
}
}

$(#[$attr])*
impl PartialEq<$type> for $other_type {
#[inline]
fn eq(&self, other: &$type) -> bool {
Expand Down
16 changes: 7 additions & 9 deletions src/wasm/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,14 @@ macro_rules! expect_utf8 {
};
}

fn from_bytes(string: &[u8]) -> Result<&str> {
str::from_utf8(string).map_err(EncodingError)
}

pub(crate) fn os_str_from_bytes(string: &[u8]) -> Result<Cow<'_, OsStr>> {
from_bytes(string).map(|x| Cow::Borrowed(OsStr::new(x)))
str::from_utf8(string)
.map(|x| Cow::Borrowed(OsStr::new(x)))
.map_err(EncodingError)
}

pub(crate) fn os_str_to_bytes(os_string: &OsStr) -> Cow<'_, [u8]> {
Cow::Borrowed(expect_utf8!(os_string.to_str()).as_bytes())
pub(crate) fn os_str_to_bytes(string: &OsStr) -> Cow<'_, [u8]> {
Cow::Borrowed(expect_utf8!(string.to_str()).as_bytes())
}

pub(crate) fn os_string_from_vec(string: Vec<u8>) -> Result<OsString> {
Expand All @@ -49,6 +47,6 @@ pub(crate) fn os_string_from_vec(string: Vec<u8>) -> Result<OsString> {
.map_err(|x| EncodingError(x.utf8_error()))
}

pub(crate) fn os_string_into_vec(os_string: OsString) -> Vec<u8> {
expect_utf8!(os_string.into_string()).into_bytes()
pub(crate) fn os_string_into_vec(string: OsString) -> Vec<u8> {
expect_utf8!(string.into_string()).into_bytes()
}
8 changes: 4 additions & 4 deletions src/wasm/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
if_conversions! {
pub(super) mod convert;
}

if_raw_str! {
#[path = "../common/raw.rs"]
pub(super) mod raw;
if_raw_str! {
#[path = "../common/raw.rs"]
pub(super) mod raw;
}
}
22 changes: 11 additions & 11 deletions src/windows/convert/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,20 +76,20 @@ fn from_bytes(string: &[u8]) -> Result<Option<OsString>> {
Ok(encoder
.is_still_utf8()
.not()
.then(|| OsStringExt::from_wide(&encoded_string)))
.then(|| OsString::from_wide(&encoded_string)))
}

fn to_bytes(os_string: &OsStr) -> Vec<u8> {
let encoder = os_string.encode_wide();
fn to_bytes(string: &OsStr) -> Vec<u8> {
let encoder = string.encode_wide();

let mut string = Vec::with_capacity(encoder.size_hint().0);
string.extend(DecodeWide::new(encoder));
string
}

pub(crate) fn os_str_from_bytes(string: &[u8]) -> Result<Cow<'_, OsStr>> {
from_bytes(string).map(|os_string| {
os_string.map(Cow::Owned).unwrap_or_else(|| {
from_bytes(string).map(|result| {
result.map(Cow::Owned).unwrap_or_else(|| {
// SAFETY: This slice was validated to be UTF-8.
Cow::Borrowed(OsStr::new(unsafe {
str::from_utf8_unchecked(string)
Expand All @@ -98,19 +98,19 @@ pub(crate) fn os_str_from_bytes(string: &[u8]) -> Result<Cow<'_, OsStr>> {
})
}

pub(crate) fn os_str_to_bytes(os_string: &OsStr) -> Cow<'_, [u8]> {
Cow::Owned(to_bytes(os_string))
pub(crate) fn os_str_to_bytes(string: &OsStr) -> Cow<'_, [u8]> {
Cow::Owned(to_bytes(string))
}

pub(crate) fn os_string_from_vec(string: Vec<u8>) -> Result<OsString> {
from_bytes(&string).map(|os_string| {
os_string.unwrap_or_else(|| {
from_bytes(&string).map(|result| {
result.unwrap_or_else(|| {
// SAFETY: This slice was validated to be UTF-8.
unsafe { String::from_utf8_unchecked(string) }.into()
})
})
}

pub(crate) fn os_string_into_vec(os_string: OsString) -> Vec<u8> {
to_bytes(&os_string)
pub(crate) fn os_string_into_vec(string: OsString) -> Vec<u8> {
to_bytes(&string)
}
6 changes: 3 additions & 3 deletions src/windows/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
if_conversions! {
pub(super) mod convert;
}

if_raw_str! {
pub(super) mod raw;
if_raw_str! {
pub(super) mod raw;
}
}
6 changes: 2 additions & 4 deletions src/windows/raw.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
if_conversions! {
pub(crate) use super::convert::ends_with;
pub(crate) use super::convert::starts_with;
}
pub(crate) use super::convert::ends_with;
pub(crate) use super::convert::starts_with;
4 changes: 2 additions & 2 deletions tests/random_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub(crate) fn fastrand_os_string(buffer_length: usize) -> OsString {
#[cfg(unix)]
{
rng.fill(&mut buffer);
OsStringExt::from_vec(buffer)
OsString::from_vec(buffer)
}
#[cfg(windows)]
{
Expand All @@ -35,6 +35,6 @@ pub(crate) fn fastrand_os_string(buffer_length: usize) -> OsString {
}

rng.fill(as_mut_bytes(&mut buffer));
OsStringExt::from_wide(&buffer)
OsString::from_wide(&buffer)
}
}

0 comments on commit aa81bae

Please sign in to comment.