Skip to content
Draft
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
137 changes: 45 additions & 92 deletions src/path/arg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,6 @@ impl Arg for String {
}

#[cfg(feature = "std")]
#[cfg(any(not(target_os = "wasi"), not(target_env = "p2"), wasip2))]
impl Arg for &OsStr {
#[inline]
fn as_str(&self) -> io::Result<&str> {
Expand All @@ -243,19 +242,18 @@ impl Arg for &OsStr {

#[inline]
fn as_cow_c_str(&self) -> io::Result<Cow<'_, CStr>> {
Ok(Cow::Owned(
CString::new(self.as_bytes()).map_err(|_cstr_err| io::Errno::INVAL)?,
))
self.into_c_str()
}

#[inline]
fn into_c_str<'b>(self) -> io::Result<Cow<'b, CStr>>
where
Self: 'b,
{
Ok(Cow::Owned(
CString::new(self.as_bytes()).map_err(|_cstr_err| io::Errno::INVAL)?,
))
#[cfg(all(target_os = "wasi", target_env = "p2", not(wasip2)))]
return self.to_str().ok_or(io::Errno::INVAL)?.into_c_str();
#[cfg(any(wasip2, not(all(target_os = "wasi", target_env = "p2"))))]
return self.as_bytes().into_c_str();
}

#[inline]
Expand All @@ -264,12 +262,15 @@ impl Arg for &OsStr {
Self: Sized,
F: FnOnce(&CStr) -> io::Result<T>,
{
with_c_str(self.as_bytes(), f)
#[cfg(all(target_os = "wasi", target_env = "p2", not(wasip2)))]
return self.as_str()?.into_with_c_str(f);

#[cfg(any(wasip2, not(all(target_os = "wasi", target_env = "p2"))))]
return self.as_bytes().into_with_c_str(f);
}
}

#[cfg(feature = "std")]
#[cfg(any(not(target_os = "wasi"), not(target_env = "p2"), wasip2))]
impl Arg for &OsString {
#[inline]
fn as_str(&self) -> io::Result<&str> {
Expand All @@ -283,10 +284,7 @@ impl Arg for &OsString {

#[inline]
fn as_cow_c_str(&self) -> io::Result<Cow<'_, CStr>> {
Ok(Cow::Owned(
CString::new(OsString::as_os_str(self).as_bytes())
.map_err(|_cstr_err| io::Errno::INVAL)?,
))
self.as_os_str().into_c_str()
}

#[inline]
Expand All @@ -303,12 +301,11 @@ impl Arg for &OsString {
Self: Sized,
F: FnOnce(&CStr) -> io::Result<T>,
{
with_c_str(self.as_bytes(), f)
self.as_os_str().into_with_c_str(f)
}
}

#[cfg(feature = "std")]
#[cfg(any(not(target_os = "wasi"), not(target_env = "p2"), wasip2))]
impl Arg for OsString {
#[inline]
fn as_str(&self) -> io::Result<&str> {
Expand All @@ -322,19 +319,21 @@ impl Arg for OsString {

#[inline]
fn as_cow_c_str(&self) -> io::Result<Cow<'_, CStr>> {
Ok(Cow::Owned(
CString::new(self.as_bytes()).map_err(|_cstr_err| io::Errno::INVAL)?,
))
self.as_os_str().into_c_str()
}

#[inline]
fn into_c_str<'b>(self) -> io::Result<Cow<'b, CStr>>
where
Self: 'b,
{
Ok(Cow::Owned(
CString::new(self.into_vec()).map_err(|_cstr_err| io::Errno::INVAL)?,
))
#[cfg(all(target_os = "wasi", target_env = "p2", not(wasip2)))]
return self
.into_string()
.map_err(|_strng_err| io::Errno::INVAL)?
.into_c_str();
#[cfg(any(wasip2, not(all(target_os = "wasi", target_env = "p2"))))]
self.into_vec().into_c_str()
}

#[inline]
Expand All @@ -343,12 +342,11 @@ impl Arg for OsString {
Self: Sized,
F: FnOnce(&CStr) -> io::Result<T>,
{
f(&CString::new(self.into_vec()).map_err(|_cstr_err| io::Errno::INVAL)?)
f(&self.into_c_str()?)
}
}

#[cfg(feature = "std")]
#[cfg(any(not(target_os = "wasi"), not(target_env = "p2"), wasip2))]
impl Arg for &Path {
#[inline]
fn as_str(&self) -> io::Result<&str> {
Expand All @@ -362,19 +360,15 @@ impl Arg for &Path {

#[inline]
fn as_cow_c_str(&self) -> io::Result<Cow<'_, CStr>> {
Ok(Cow::Owned(
CString::new(self.as_os_str().as_bytes()).map_err(|_cstr_err| io::Errno::INVAL)?,
))
self.as_os_str().into_c_str()
}

#[inline]
fn into_c_str<'b>(self) -> io::Result<Cow<'b, CStr>>
where
Self: 'b,
{
Ok(Cow::Owned(
CString::new(self.as_os_str().as_bytes()).map_err(|_cstr_err| io::Errno::INVAL)?,
))
self.as_os_str().into_c_str()
}

#[inline]
Expand All @@ -383,19 +377,15 @@ impl Arg for &Path {
Self: Sized,
F: FnOnce(&CStr) -> io::Result<T>,
{
with_c_str(self.as_os_str().as_bytes(), f)
self.as_os_str().into_with_c_str(f)
}
}

#[cfg(feature = "std")]
#[cfg(any(not(target_os = "wasi"), not(target_env = "p2"), wasip2))]
impl Arg for &PathBuf {
#[inline]
fn as_str(&self) -> io::Result<&str> {
PathBuf::as_path(self)
.as_os_str()
.to_str()
.ok_or(io::Errno::INVAL)
self.as_os_str().to_str().ok_or(io::Errno::INVAL)
}

#[inline]
Expand All @@ -405,10 +395,7 @@ impl Arg for &PathBuf {

#[inline]
fn as_cow_c_str(&self) -> io::Result<Cow<'_, CStr>> {
Ok(Cow::Owned(
CString::new(PathBuf::as_path(self).as_os_str().as_bytes())
.map_err(|_cstr_err| io::Errno::INVAL)?,
))
self.as_os_str().into_c_str()
}

#[inline]
Expand All @@ -425,12 +412,11 @@ impl Arg for &PathBuf {
Self: Sized,
F: FnOnce(&CStr) -> io::Result<T>,
{
with_c_str(self.as_os_str().as_bytes(), f)
self.as_os_str().into_with_c_str(f)
}
}

#[cfg(feature = "std")]
#[cfg(any(not(target_os = "wasi"), not(target_env = "p2"), wasip2))]
impl Arg for PathBuf {
#[inline]
fn as_str(&self) -> io::Result<&str> {
Expand All @@ -444,19 +430,15 @@ impl Arg for PathBuf {

#[inline]
fn as_cow_c_str(&self) -> io::Result<Cow<'_, CStr>> {
Ok(Cow::Owned(
CString::new(self.as_os_str().as_bytes()).map_err(|_cstr_err| io::Errno::INVAL)?,
))
self.as_os_str().into_c_str()
}

#[inline]
fn into_c_str<'b>(self) -> io::Result<Cow<'b, CStr>>
where
Self: 'b,
{
Ok(Cow::Owned(
CString::new(self.into_os_string().into_vec()).map_err(|_cstr_err| io::Errno::INVAL)?,
))
self.into_os_string().into_c_str()
}

#[inline]
Expand All @@ -465,10 +447,7 @@ impl Arg for PathBuf {
Self: Sized,
F: FnOnce(&CStr) -> io::Result<T>,
{
f(
&CString::new(self.into_os_string().into_vec())
.map_err(|_cstr_err| io::Errno::INVAL)?,
)
self.into_os_string().into_with_c_str(f)
}
}

Expand Down Expand Up @@ -623,7 +602,6 @@ impl<'a> Arg for Cow<'a, str> {
}

#[cfg(feature = "std")]
#[cfg(any(not(target_os = "wasi"), not(target_env = "p2"), wasip2))]
impl<'a> Arg for Cow<'a, OsStr> {
#[inline]
fn as_str(&self) -> io::Result<&str> {
Expand All @@ -637,23 +615,18 @@ impl<'a> Arg for Cow<'a, OsStr> {

#[inline]
fn as_cow_c_str(&self) -> io::Result<Cow<'_, CStr>> {
Ok(Cow::Owned(
CString::new(self.as_bytes()).map_err(|_cstr_err| io::Errno::INVAL)?,
))
(&**self).into_c_str()
}

#[inline]
fn into_c_str<'b>(self) -> io::Result<Cow<'b, CStr>>
where
Self: 'b,
{
Ok(Cow::Owned(
match self {
Cow::Owned(os) => CString::new(os.into_vec()),
Cow::Borrowed(os) => CString::new(os.as_bytes()),
}
.map_err(|_cstr_err| io::Errno::INVAL)?,
))
match self {
Cow::Owned(os) => os.into_c_str(),
Cow::Borrowed(os) => os.into_c_str(),
}
}

#[inline]
Expand All @@ -662,7 +635,7 @@ impl<'a> Arg for Cow<'a, OsStr> {
Self: Sized,
F: FnOnce(&CStr) -> io::Result<T>,
{
with_c_str(self.as_bytes(), f)
(&*self).into_with_c_str(f)
}
}

Expand Down Expand Up @@ -703,7 +676,6 @@ impl<'a> Arg for Cow<'a, CStr> {
}

#[cfg(feature = "std")]
#[cfg(any(not(target_os = "wasi"), not(target_env = "p2"), wasip2))]
impl<'a> Arg for Component<'a> {
#[inline]
fn as_str(&self) -> io::Result<&str> {
Expand All @@ -717,19 +689,15 @@ impl<'a> Arg for Component<'a> {

#[inline]
fn as_cow_c_str(&self) -> io::Result<Cow<'_, CStr>> {
Ok(Cow::Owned(
CString::new(self.as_os_str().as_bytes()).map_err(|_cstr_err| io::Errno::INVAL)?,
))
self.as_os_str().into_c_str()
}

#[inline]
fn into_c_str<'b>(self) -> io::Result<Cow<'b, CStr>>
where
Self: 'b,
{
Ok(Cow::Owned(
CString::new(self.as_os_str().as_bytes()).map_err(|_cstr_err| io::Errno::INVAL)?,
))
self.as_os_str().into_c_str()
}

#[inline]
Expand All @@ -738,12 +706,11 @@ impl<'a> Arg for Component<'a> {
Self: Sized,
F: FnOnce(&CStr) -> io::Result<T>,
{
with_c_str(self.as_os_str().as_bytes(), f)
self.as_os_str().into_with_c_str(f)
}
}

#[cfg(feature = "std")]
#[cfg(any(not(target_os = "wasi"), not(target_env = "p2"), wasip2))]
impl<'a> Arg for Components<'a> {
#[inline]
fn as_str(&self) -> io::Result<&str> {
Expand All @@ -757,21 +724,15 @@ impl<'a> Arg for Components<'a> {

#[inline]
fn as_cow_c_str(&self) -> io::Result<Cow<'_, CStr>> {
Ok(Cow::Owned(
CString::new(self.as_path().as_os_str().as_bytes())
.map_err(|_cstr_err| io::Errno::INVAL)?,
))
self.as_path().into_c_str()
}

#[inline]
fn into_c_str<'b>(self) -> io::Result<Cow<'b, CStr>>
where
Self: 'b,
{
Ok(Cow::Owned(
CString::new(self.as_path().as_os_str().as_bytes())
.map_err(|_cstr_err| io::Errno::INVAL)?,
))
self.as_path().into_c_str()
}

#[inline]
Expand All @@ -780,12 +741,11 @@ impl<'a> Arg for Components<'a> {
Self: Sized,
F: FnOnce(&CStr) -> io::Result<T>,
{
with_c_str(self.as_path().as_os_str().as_bytes(), f)
self.as_path().into_with_c_str(f)
}
}

#[cfg(feature = "std")]
#[cfg(any(not(target_os = "wasi"), not(target_env = "p2"), wasip2))]
impl<'a> Arg for Iter<'a> {
#[inline]
fn as_str(&self) -> io::Result<&str> {
Expand All @@ -799,21 +759,15 @@ impl<'a> Arg for Iter<'a> {

#[inline]
fn as_cow_c_str(&self) -> io::Result<Cow<'_, CStr>> {
Ok(Cow::Owned(
CString::new(self.as_path().as_os_str().as_bytes())
.map_err(|_cstr_err| io::Errno::INVAL)?,
))
self.as_path().into_c_str()
}

#[inline]
fn into_c_str<'b>(self) -> io::Result<Cow<'b, CStr>>
where
Self: 'b,
{
Ok(Cow::Owned(
CString::new(self.as_path().as_os_str().as_bytes())
.map_err(|_cstr_err| io::Errno::INVAL)?,
))
self.as_path().into_c_str()
}

#[inline]
Expand All @@ -822,7 +776,7 @@ impl<'a> Arg for Iter<'a> {
Self: Sized,
F: FnOnce(&CStr) -> io::Result<T>,
{
with_c_str(self.as_path().as_os_str().as_bytes(), f)
self.as_path().into_with_c_str(f)
}
}

Expand Down Expand Up @@ -910,7 +864,6 @@ impl Arg for &Vec<u8> {
}

#[cfg(feature = "alloc")]
#[cfg(any(not(target_os = "wasi"), not(target_env = "p2"), wasip2))]
impl Arg for Vec<u8> {
#[inline]
fn as_str(&self) -> io::Result<&str> {
Expand Down
Loading