Skip to content

Commit

Permalink
feat: add support for wasi (#470)
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
Byron committed Sep 20, 2022
1 parent 5878ad1 commit 523418f
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions git-path/src/convert.rs
Expand Up @@ -41,6 +41,11 @@ pub fn try_into_bstr<'a>(path: impl Into<Cow<'a, Path>>) -> Result<Cow<'a, BStr>
use std::os::unix::ffi::OsStringExt;
path.into_os_string().into_vec().into()
};
#[cfg(target_os = "wasi")]
let p: BString = {
use std::os::wasi::ffi::OsStringExt;
path.into_os_string().into_vec().into()
};
#[cfg(not(unix))]
let p: BString = path.into_os_string().into_string().map_err(|_| Utf8Error)?.into();
p
Expand All @@ -51,6 +56,11 @@ pub fn try_into_bstr<'a>(path: impl Into<Cow<'a, Path>>) -> Result<Cow<'a, BStr>
use std::os::unix::ffi::OsStrExt;
path.as_os_str().as_bytes().into()
};
#[cfg(target_os = "wasi")]
let p: &BStr = {
use std::os::wasi::ffi::OsStrExt;
path.as_os_str().as_bytes().into()
};
#[cfg(not(unix))]
let p: &BStr = path.to_str().ok_or(Utf8Error)?.as_bytes().into();
p
Expand All @@ -75,6 +85,11 @@ pub fn try_from_byte_slice(input: &[u8]) -> Result<&Path, Utf8Error> {
use std::os::unix::ffi::OsStrExt;
OsStr::from_bytes(input).as_ref()
};
#[cfg(target_os = "wasi")]
let p = {
use std::os::wasi::ffi::OsStrExt;
OsStr::from_bytes(input).as_ref()
};
#[cfg(not(unix))]
let p = Path::new(std::str::from_utf8(input).map_err(|_| Utf8Error)?);
Ok(p)
Expand Down Expand Up @@ -102,6 +117,11 @@ pub fn try_from_bstring(input: impl Into<BString>) -> Result<PathBuf, Utf8Error>
use std::os::unix::ffi::OsStringExt;
std::ffi::OsString::from_vec(input.into()).into()
};
#[cfg(target_os = "wasi")]
let p = {
use std::os::wasi::ffi::OsStringExt;
std::ffi::OsString::from_vec(input.into()).into()
};
#[cfg(not(unix))]
let p = {
use bstr::ByteVec;
Expand Down

0 comments on commit 523418f

Please sign in to comment.