Skip to content

Commit 9358459

Browse files
authored
Compile for WASI (#50)
* Compile on WASI * Fix omitting members when not compiling on WASI * Use cfg(any(...)) where applicable
1 parent 8370838 commit 9358459

File tree

4 files changed

+20
-3
lines changed

4 files changed

+20
-3
lines changed

src/dir.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,12 @@ impl PathOps for PathDir {
523523
}
524524
}
525525

526+
#[cfg(target_os = "wasi")]
527+
fn symlink_dir<P: AsRef<Path>, Q: AsRef<Path>>(src: P, dst: Q) -> io::Result<()> {
528+
let file = std::fs::File::open(&src)?;
529+
std::os::wasi::fs::symlink(src, &file, dst)
530+
}
531+
526532
#[cfg(unix)]
527533
fn symlink_dir<P: AsRef<Path>, Q: AsRef<Path>>(src: P, dst: Q) -> io::Result<()> {
528534
::std::os::unix::fs::symlink(src, dst)

src/file.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,12 @@ impl PathOps for PathFile {
570570
}
571571
}
572572

573+
#[cfg(target_os = "wasi")]
574+
fn symlink_file<P: AsRef<Path>, Q: AsRef<Path>>(src: P, dst: Q) -> io::Result<()> {
575+
let file = std::fs::File::open(&src)?;
576+
std::os::wasi::fs::symlink(src, &file, dst)
577+
}
578+
573579
#[cfg(unix)]
574580
fn symlink_file<P: AsRef<Path>, Q: AsRef<Path>>(src: P, dst: Q) -> io::Result<()> {
575581
::std::os::unix::fs::symlink(src, dst)

src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,9 @@
203203
//! [`PathOps`]: trait.PathOps.html
204204
//! [`PathMut`]: trait.PathMut.html
205205
206+
#![cfg_attr(target_os = "wasi",
207+
feature(wasi_ext))]
208+
206209
#[cfg(feature = "serialize")]
207210
extern crate serde;
208211
#[macro_use]

src/ser.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ use stfu8;
1414
use super::{PathMut, PathOps};
1515

1616
use std::ffi::{OsStr, OsString};
17+
#[cfg(target_os = "wasi")]
18+
use std::os::wasi::ffi::{OsStrExt, OsStringExt};
1719
#[cfg(unix)]
1820
use std::os::unix::ffi::{OsStrExt, OsStringExt};
1921
#[cfg(windows)]
@@ -151,7 +153,7 @@ impl<T> ToStfu8 for T
151153
where
152154
T: Borrow<PathBuf>,
153155
{
154-
#[cfg(unix)]
156+
#[cfg(any(target_os = "wasi", unix))]
155157
fn to_stfu8(&self) -> String {
156158
let bytes = self.borrow().as_os_str().as_bytes();
157159
stfu8::encode_u8(bytes)
@@ -168,7 +170,7 @@ impl<T> FromStfu8 for T
168170
where
169171
T: From<PathBuf>,
170172
{
171-
#[cfg(unix)]
173+
#[cfg(any(target_os = "wasi", unix))]
172174
fn from_stfu8(s: &str) -> Result<T, stfu8::DecodeError> {
173175
let raw_path = stfu8::decode_u8(s)?;
174176
let os_str = OsString::from_vec(raw_path);
@@ -252,7 +254,7 @@ mod tests {
252254
use super::super::{PathDir, PathFile, PathInfo, PathMut, PathOps, PathType};
253255
use super::*;
254256

255-
#[cfg(unix)]
257+
#[cfg(any(target_os = "wasi", unix))]
256258
static SERIALIZED: &str = "[\
257259
{\"type\":\"file\",\"path\":\"{0}/foo.txt\"},\
258260
{\"type\":\"dir\",\"path\":\"{0}/bar\"},\

0 commit comments

Comments
 (0)