Skip to content

Commit 523418f

Browse files
committed
feat: add support for wasi (#470)
This allows path conversions there to be just as efficient as on unix. This was adopted from [a PR in the hexlix-editor](https://github.com/helix-editor/helix/pull/3890/files#diff-504515b66023120e75a921cd56a932aed76c0ff62593fbb69d92e0ef65089501R47).
1 parent 5878ad1 commit 523418f

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

git-path/src/convert.rs

+20
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ pub fn try_into_bstr<'a>(path: impl Into<Cow<'a, Path>>) -> Result<Cow<'a, BStr>
4141
use std::os::unix::ffi::OsStringExt;
4242
path.into_os_string().into_vec().into()
4343
};
44+
#[cfg(target_os = "wasi")]
45+
let p: BString = {
46+
use std::os::wasi::ffi::OsStringExt;
47+
path.into_os_string().into_vec().into()
48+
};
4449
#[cfg(not(unix))]
4550
let p: BString = path.into_os_string().into_string().map_err(|_| Utf8Error)?.into();
4651
p
@@ -51,6 +56,11 @@ pub fn try_into_bstr<'a>(path: impl Into<Cow<'a, Path>>) -> Result<Cow<'a, BStr>
5156
use std::os::unix::ffi::OsStrExt;
5257
path.as_os_str().as_bytes().into()
5358
};
59+
#[cfg(target_os = "wasi")]
60+
let p: &BStr = {
61+
use std::os::wasi::ffi::OsStrExt;
62+
path.as_os_str().as_bytes().into()
63+
};
5464
#[cfg(not(unix))]
5565
let p: &BStr = path.to_str().ok_or(Utf8Error)?.as_bytes().into();
5666
p
@@ -75,6 +85,11 @@ pub fn try_from_byte_slice(input: &[u8]) -> Result<&Path, Utf8Error> {
7585
use std::os::unix::ffi::OsStrExt;
7686
OsStr::from_bytes(input).as_ref()
7787
};
88+
#[cfg(target_os = "wasi")]
89+
let p = {
90+
use std::os::wasi::ffi::OsStrExt;
91+
OsStr::from_bytes(input).as_ref()
92+
};
7893
#[cfg(not(unix))]
7994
let p = Path::new(std::str::from_utf8(input).map_err(|_| Utf8Error)?);
8095
Ok(p)
@@ -102,6 +117,11 @@ pub fn try_from_bstring(input: impl Into<BString>) -> Result<PathBuf, Utf8Error>
102117
use std::os::unix::ffi::OsStringExt;
103118
std::ffi::OsString::from_vec(input.into()).into()
104119
};
120+
#[cfg(target_os = "wasi")]
121+
let p = {
122+
use std::os::wasi::ffi::OsStringExt;
123+
std::ffi::OsString::from_vec(input.into()).into()
124+
};
105125
#[cfg(not(unix))]
106126
let p = {
107127
use bstr::ByteVec;

0 commit comments

Comments
 (0)