diff --git a/src/ascii_char.rs b/src/ascii_char.rs index 39f2ceb..3fc6e75 100644 --- a/src/ascii_char.rs +++ b/src/ascii_char.rs @@ -794,8 +794,8 @@ macro_rules! impl_into_partial_eq_ord { ($wider:ty, $to_wider:expr) => { impl From for $wider { #[inline] - fn from(a: AsciiChar) -> $wider { - $to_wider(a) + fn from(ascii: AsciiChar) -> $wider { + $to_wider(ascii) } } impl PartialEq<$wider> for AsciiChar { diff --git a/src/ascii_str.rs b/src/ascii_str.rs index bfdf54e..942e79f 100644 --- a/src/ascii_str.rs +++ b/src/ascii_str.rs @@ -10,6 +10,8 @@ use core::slice::{self, Iter, IterMut, SliceIndex}; use std::error::Error; #[cfg(feature = "std")] use std::ffi::CStr; +#[cfg(feature = "std")] +use std::ffi::OsStr; use ascii_char::AsciiChar; #[cfg(feature = "alloc")] @@ -407,6 +409,8 @@ macro_rules! impl_partial_eq { }; } +#[cfg(feature = "std")] +impl_partial_eq! {OsStr} impl_partial_eq! {str} impl_partial_eq! {[u8]} impl_partial_eq! {[AsciiChar]} @@ -433,6 +437,13 @@ impl AsRef for AsciiStr { self.as_str() } } +#[cfg(feature = "std")] +impl AsRef for AsciiStr { + #[inline] + fn as_ref(&self) -> &OsStr { + self.as_str().as_ref() + } +} impl AsRef<[AsciiChar]> for AsciiStr { #[inline] fn as_ref(&self) -> &[AsciiChar] { @@ -524,6 +535,13 @@ impl<'a> From<&'a AsciiStr> for &'a str { astr.as_str() } } +#[cfg(feature = "std")] +impl<'a> From<&'a AsciiStr> for &'a OsStr { + #[inline] + fn from(astr: &AsciiStr) -> &OsStr { + astr.as_ref() + } +} macro_rules! widen_box { ($wider: ty) => { #[cfg(feature = "alloc")] @@ -539,6 +557,8 @@ macro_rules! widen_box { widen_box! {[AsciiChar]} widen_box! {[u8]} widen_box! {str} +#[cfg(feature = "std")] +widen_box! {OsStr} // allows &AsciiChar to be used by generic AsciiString Extend and FromIterator impl AsRef for AsciiChar { @@ -1291,6 +1311,25 @@ impl AsAsciiStr for CStr { } } +#[cfg(feature = "std")] +impl AsAsciiStr for OsStr { + type Inner = u8; + fn slice_ascii(&self, range: R) -> Result<&AsciiStr, AsAsciiStrError> + where + R: SliceIndex<[u8], Output = [u8]>, + { + self.as_encoded_bytes().slice_ascii(range) + } + fn as_ascii_str(&self) -> Result<&AsciiStr, AsAsciiStrError> { + self.as_encoded_bytes().as_ascii_str() + } + #[inline] + unsafe fn as_ascii_str_unchecked(&self) -> &AsciiStr { + // SAFETY: Caller guarantees `self` does not contain non-ascii characters + unsafe { self.as_encoded_bytes().as_ascii_str_unchecked() } + } +} + #[cfg(test)] mod tests { use super::{AsAsciiStr, AsAsciiStrError, AsMutAsciiStr, AsciiStr}; @@ -1367,6 +1406,19 @@ mod tests { assert_eq!(generic_mut(ascii_str_mut), Ok(&mut *ascii_str_mut_2)); } + #[cfg(feature = "std")] + #[test] + fn osstr_as_ascii_str() { + use std::ffi::OsStr; + fn generic(c: &C) -> Result<&AsciiStr, AsAsciiStrError> { + c.as_ascii_str() + } + let arr = [AsciiChar::A]; + let ascii_str: &AsciiStr = arr.as_ref().into(); + let os_str = OsStr::new(ascii_str); + assert_eq!(generic(&*os_str), Ok(ascii_str)); + } + #[test] fn as_ascii_str() { macro_rules! err {{$i:expr} => {Err(AsAsciiStrError($i))}} diff --git a/src/ascii_string.rs b/src/ascii_string.rs index b6d2b0b..510d1b5 100644 --- a/src/ascii_string.rs +++ b/src/ascii_string.rs @@ -15,6 +15,8 @@ use core::str::FromStr; use std::error::Error; #[cfg(feature = "std")] use std::ffi::{CStr, CString}; +#[cfg(feature = "std")] +use std::ffi::OsStr; use ascii_char::AsciiChar; use ascii_str::{AsAsciiStr, AsAsciiStrError, AsciiStr}; @@ -451,6 +453,22 @@ impl PartialEq for str { } } +#[cfg(feature = "std")] +impl PartialEq for AsciiString { + #[inline] + fn eq(&self, other: &OsStr) -> bool { + **self == *other + } +} + +#[cfg(feature = "std")] +impl PartialEq for OsStr { + #[inline] + fn eq(&self, other: &AsciiString) -> bool { + **other == *self + } +} + macro_rules! impl_eq { ($lhs:ty, $rhs:ty) => { impl PartialEq<$rhs> for $lhs { @@ -470,6 +488,10 @@ impl_eq! { &AsciiStr, AsciiString } impl_eq! { AsciiString, &AsciiStr } impl_eq! { &str, AsciiString } impl_eq! { AsciiString, &str } +#[cfg(feature = "std")] +impl_eq! { &OsStr, AsciiString } +#[cfg(feature = "std")] +impl_eq! { AsciiString, &OsStr } impl Borrow for AsciiString { #[inline] @@ -619,6 +641,14 @@ impl AsRef for AsciiString { } } +#[cfg(feature = "std")] +impl AsRef for AsciiString { + #[inline] + fn as_ref(&self) -> &OsStr { + self.as_str().as_ref() + } +} + impl AsMut for AsciiString { #[inline] fn as_mut(&mut self) -> &mut AsciiStr {