Skip to content

Commit

Permalink
Compile for WASI (#50)
Browse files Browse the repository at this point in the history
* Compile on WASI

* Fix omitting members when not compiling on WASI

* Use cfg(any(...)) where applicable
  • Loading branch information
zebp authored Dec 20, 2020
1 parent 8370838 commit 9358459
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
6 changes: 6 additions & 0 deletions src/dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,12 @@ impl PathOps for PathDir {
}
}

#[cfg(target_os = "wasi")]
fn symlink_dir<P: AsRef<Path>, Q: AsRef<Path>>(src: P, dst: Q) -> io::Result<()> {
let file = std::fs::File::open(&src)?;
std::os::wasi::fs::symlink(src, &file, dst)
}

#[cfg(unix)]
fn symlink_dir<P: AsRef<Path>, Q: AsRef<Path>>(src: P, dst: Q) -> io::Result<()> {
::std::os::unix::fs::symlink(src, dst)
Expand Down
6 changes: 6 additions & 0 deletions src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,12 @@ impl PathOps for PathFile {
}
}

#[cfg(target_os = "wasi")]
fn symlink_file<P: AsRef<Path>, Q: AsRef<Path>>(src: P, dst: Q) -> io::Result<()> {
let file = std::fs::File::open(&src)?;
std::os::wasi::fs::symlink(src, &file, dst)
}

#[cfg(unix)]
fn symlink_file<P: AsRef<Path>, Q: AsRef<Path>>(src: P, dst: Q) -> io::Result<()> {
::std::os::unix::fs::symlink(src, dst)
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,9 @@
//! [`PathOps`]: trait.PathOps.html
//! [`PathMut`]: trait.PathMut.html
#![cfg_attr(target_os = "wasi",
feature(wasi_ext))]

#[cfg(feature = "serialize")]
extern crate serde;
#[macro_use]
Expand Down
8 changes: 5 additions & 3 deletions src/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ use stfu8;
use super::{PathMut, PathOps};

use std::ffi::{OsStr, OsString};
#[cfg(target_os = "wasi")]
use std::os::wasi::ffi::{OsStrExt, OsStringExt};
#[cfg(unix)]
use std::os::unix::ffi::{OsStrExt, OsStringExt};
#[cfg(windows)]
Expand Down Expand Up @@ -151,7 +153,7 @@ impl<T> ToStfu8 for T
where
T: Borrow<PathBuf>,
{
#[cfg(unix)]
#[cfg(any(target_os = "wasi", unix))]
fn to_stfu8(&self) -> String {
let bytes = self.borrow().as_os_str().as_bytes();
stfu8::encode_u8(bytes)
Expand All @@ -168,7 +170,7 @@ impl<T> FromStfu8 for T
where
T: From<PathBuf>,
{
#[cfg(unix)]
#[cfg(any(target_os = "wasi", unix))]
fn from_stfu8(s: &str) -> Result<T, stfu8::DecodeError> {
let raw_path = stfu8::decode_u8(s)?;
let os_str = OsString::from_vec(raw_path);
Expand Down Expand Up @@ -252,7 +254,7 @@ mod tests {
use super::super::{PathDir, PathFile, PathInfo, PathMut, PathOps, PathType};
use super::*;

#[cfg(unix)]
#[cfg(any(target_os = "wasi", unix))]
static SERIALIZED: &str = "[\
{\"type\":\"file\",\"path\":\"{0}/foo.txt\"},\
{\"type\":\"dir\",\"path\":\"{0}/bar\"},\
Expand Down

0 comments on commit 9358459

Please sign in to comment.