diff --git a/lib/vfs/src/host_fs.rs b/lib/vfs/src/host_fs.rs index 961f6685be0..c2eb4908693 100644 --- a/lib/vfs/src/host_fs.rs +++ b/lib/vfs/src/host_fs.rs @@ -9,6 +9,8 @@ use std::fs; use std::io::{self, Read, Seek, Write}; #[cfg(unix)] use std::os::unix::io::{AsRawFd, RawFd}; +#[cfg(target_arch = "wasm32")] +use std::os::wasi::io::{AsRawFd, RawFd}; #[cfg(windows)] use std::os::windows::io::{AsRawHandle, RawHandle}; use std::path::{Path, PathBuf}; @@ -67,6 +69,31 @@ impl TryInto for FileDescriptor { } } +#[cfg(target_arch = "wasm32")] +impl TryIntoFileDescriptor for T +where + T: AsRawFd, +{ + type Error = FsError; + + fn try_into_filedescriptor(&self) -> std::result::Result { + Ok(FileDescriptor( + self.as_raw_fd() + .try_into() + .map_err(|_| FsError::InvalidFd)?, + )) + } +} + +#[cfg(target_arch = "wasm32")] +impl TryInto for FileDescriptor { + type Error = FsError; + + fn try_into(self) -> std::result::Result { + self.0.try_into().map_err(|_| FsError::InvalidFd) + } +} + #[derive(Debug, Default, Clone)] #[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))] pub struct FileSystem;