use std::io;
use std::path::Path;
use std::time::Duration;
use super::std_env::StdEnv;
use super::{
Capabilities, DirEntry, Env, FileLock, FileMeta, JoinHandle, ReadFile, WriteFile, WriteMode,
};
#[derive(Debug, Default, Clone, Copy)]
pub struct WasiEnv {
fs: StdEnv,
}
impl WasiEnv {
pub const fn new() -> Self {
Self { fs: StdEnv::new() }
}
}
impl Env for WasiEnv {
fn create_dir_all(&self, path: &Path) -> io::Result<()> {
self.fs.create_dir_all(path)
}
fn read_dir(&self, path: &Path) -> io::Result<Vec<DirEntry>> {
self.fs.read_dir(path)
}
fn open_read(&self, path: &Path) -> io::Result<Box<dyn ReadFile>> {
self.fs.open_read(path)
}
fn open_write(&self, path: &Path, mode: WriteMode) -> io::Result<Box<dyn WriteFile>> {
self.fs.open_write(path, mode)
}
fn metadata(&self, path: &Path) -> io::Result<FileMeta> {
self.fs.metadata(path)
}
fn remove_file(&self, path: &Path) -> io::Result<()> {
self.fs.remove_file(path)
}
fn rename(&self, from: &Path, to: &Path) -> io::Result<()> {
self.fs.rename(from, to)
}
fn hard_link(&self, src: &Path, dst: &Path) -> io::Result<()> {
self.fs.hard_link(src, dst)
}
fn sync_dir(&self, path: &Path) -> io::Result<()> {
self.fs.sync_dir(path)
}
fn lock_file(&self, path: &Path, exclusive: bool) -> io::Result<Box<dyn FileLock>> {
self.fs.lock_file(path, exclusive)
}
fn capabilities(&self) -> Capabilities {
self.fs.capabilities()
}
fn now_micros(&self) -> Option<u64> {
self.fs.now_micros()
}
fn unix_secs(&self) -> Option<u64> {
self.fs.unix_secs()
}
fn spawn(
&self,
name: &str,
body: Box<dyn FnOnce() + Send + 'static>,
) -> io::Result<Box<dyn JoinHandle>> {
drop(body);
Err(io::Error::new(
io::ErrorKind::Unsupported,
format!("cannot start thread {name}: WASI has no threads"),
))
}
fn sleep(&self, dur: Duration) {
self.fs.sleep(dur);
}
fn drop_page_cache(&self, path: &Path) {
let _ = path;
}
}