Skip to content

Commit f88b181

Browse files
committed
feat(path): enable Path and friends on wasip2 without the feature
Unfortunately, `OsStrExt` and `OsStringExt` require the wasip2 feature on wasip2 so we can't use `as_bytes()` on `OsStr` and friends. However, the recommendation on the upstream issue [1] is to just use `to_str` as WASI paths are unicode. [1]: rust-lang/rust#130323 (comment)
1 parent 7c16f4e commit f88b181

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

src/path/arg.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,6 @@ impl Arg for String {
229229
}
230230

231231
#[cfg(feature = "std")]
232-
#[cfg(any(not(target_os = "wasi"), not(target_env = "p2"), wasip2))]
233232
impl Arg for &OsStr {
234233
#[inline]
235234
fn as_str(&self) -> io::Result<&str> {
@@ -251,6 +250,9 @@ impl Arg for &OsStr {
251250
where
252251
Self: 'b,
253252
{
253+
#[cfg(all(target_os = "wasi", target_env = "p2", not(wasip2)))]
254+
return self.to_str().ok_or(io::Errno::INVAL)?.into_c_str();
255+
#[cfg(any(wasip2, not(all(target_os = "wasi", target_env = "p2"))))]
254256
return self.as_bytes().into_c_str();
255257
}
256258

@@ -260,12 +262,15 @@ impl Arg for &OsStr {
260262
Self: Sized,
261263
F: FnOnce(&CStr) -> io::Result<T>,
262264
{
265+
#[cfg(all(target_os = "wasi", target_env = "p2", not(wasip2)))]
266+
return self.as_str()?.into_with_c_str(f);
267+
268+
#[cfg(any(wasip2, not(all(target_os = "wasi", target_env = "p2"))))]
263269
return self.as_bytes().into_with_c_str(f);
264270
}
265271
}
266272

267273
#[cfg(feature = "std")]
268-
#[cfg(any(not(target_os = "wasi"), not(target_env = "p2"), wasip2))]
269274
impl Arg for &OsString {
270275
#[inline]
271276
fn as_str(&self) -> io::Result<&str> {
@@ -322,6 +327,12 @@ impl Arg for OsString {
322327
where
323328
Self: 'b,
324329
{
330+
#[cfg(all(target_os = "wasi", target_env = "p2", not(wasip2)))]
331+
return self
332+
.into_string()
333+
.map_err(|_strng_err| io::Errno::INVAL)?
334+
.into_c_str();
335+
#[cfg(any(wasip2, not(all(target_os = "wasi", target_env = "p2"))))]
325336
self.into_vec().into_c_str()
326337
}
327338

@@ -591,7 +602,6 @@ impl<'a> Arg for Cow<'a, str> {
591602
}
592603

593604
#[cfg(feature = "std")]
594-
#[cfg(any(not(target_os = "wasi"), not(target_env = "p2"), wasip2))]
595605
impl<'a> Arg for Cow<'a, OsStr> {
596606
#[inline]
597607
fn as_str(&self) -> io::Result<&str> {
@@ -666,7 +676,6 @@ impl<'a> Arg for Cow<'a, CStr> {
666676
}
667677

668678
#[cfg(feature = "std")]
669-
#[cfg(any(not(target_os = "wasi"), not(target_env = "p2"), wasip2))]
670679
impl<'a> Arg for Component<'a> {
671680
#[inline]
672681
fn as_str(&self) -> io::Result<&str> {
@@ -855,7 +864,6 @@ impl Arg for &Vec<u8> {
855864
}
856865

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

0 commit comments

Comments
 (0)