Skip to content

Conversions to OsStr. #107

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/ascii_char.rs
Original file line number Diff line number Diff line change
Expand Up @@ -794,8 +794,8 @@ macro_rules! impl_into_partial_eq_ord {
($wider:ty, $to_wider:expr) => {
impl From<AsciiChar> for $wider {
#[inline]
fn from(a: AsciiChar) -> $wider {
$to_wider(a)
fn from(ascii: AsciiChar) -> $wider {
$to_wider(ascii)
}
}
impl PartialEq<$wider> for AsciiChar {
Expand Down
52 changes: 52 additions & 0 deletions src/ascii_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down Expand Up @@ -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]}
Expand All @@ -433,6 +437,13 @@ impl AsRef<str> for AsciiStr {
self.as_str()
}
}
#[cfg(feature = "std")]
impl AsRef<OsStr> for AsciiStr {
#[inline]
fn as_ref(&self) -> &OsStr {
self.as_str().as_ref()
}
}
impl AsRef<[AsciiChar]> for AsciiStr {
#[inline]
fn as_ref(&self) -> &[AsciiChar] {
Expand Down Expand Up @@ -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")]
Expand All @@ -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<AsciiStr> for AsciiChar {
Expand Down Expand Up @@ -1291,6 +1311,25 @@ impl AsAsciiStr for CStr {
}
}

#[cfg(feature = "std")]
impl AsAsciiStr for OsStr {
type Inner = u8;
fn slice_ascii<R>(&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};
Expand Down Expand Up @@ -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: AsAsciiStr + ?Sized>(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))}}
Expand Down
30 changes: 30 additions & 0 deletions src/ascii_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -451,6 +453,22 @@ impl PartialEq<AsciiString> for str {
}
}

#[cfg(feature = "std")]
impl PartialEq<OsStr> for AsciiString {
#[inline]
fn eq(&self, other: &OsStr) -> bool {
**self == *other
}
}

#[cfg(feature = "std")]
impl PartialEq<AsciiString> for OsStr {
#[inline]
fn eq(&self, other: &AsciiString) -> bool {
**other == *self
}
}

macro_rules! impl_eq {
($lhs:ty, $rhs:ty) => {
impl PartialEq<$rhs> for $lhs {
Expand All @@ -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<AsciiStr> for AsciiString {
#[inline]
Expand Down Expand Up @@ -619,6 +641,14 @@ impl AsRef<str> for AsciiString {
}
}

#[cfg(feature = "std")]
impl AsRef<OsStr> for AsciiString {
#[inline]
fn as_ref(&self) -> &OsStr {
self.as_str().as_ref()
}
}

impl AsMut<AsciiStr> for AsciiString {
#[inline]
fn as_mut(&mut self) -> &mut AsciiStr {
Expand Down