diff --git a/lib/c-api/src/wasm_c_api/wasi/mod.rs b/lib/c-api/src/wasm_c_api/wasi/mod.rs index 8c5244a9c9f..6bc928e7d39 100644 --- a/lib/c-api/src/wasm_c_api/wasi/mod.rs +++ b/lib/c-api/src/wasm_c_api/wasi/mod.rs @@ -16,8 +16,8 @@ use std::os::raw::c_char; use std::slice; use wasmer_api::{Exportable, Extern}; use wasmer_wasi::{ - generate_import_object_from_env, get_wasi_version, WasiEnv, WasiFile, WasiState, - WasiStateBuilder, WasiVersion, Pipe, + generate_import_object_from_env, get_wasi_version, Pipe, WasiEnv, WasiFile, WasiState, + WasiStateBuilder, WasiVersion, }; #[derive(Debug)] @@ -172,15 +172,11 @@ pub struct wasi_env_t { #[no_mangle] pub extern "C" fn wasi_env_new(mut config: Box) -> Option> { if !config.inherit_stdout { - config - .state_builder - .stdout(Box::new(Pipe::new())); + config.state_builder.stdout(Box::new(Pipe::new())); } if !config.inherit_stderr { - config - .state_builder - .stderr(Box::new(Pipe::new())); + config.state_builder.stderr(Box::new(Pipe::new())); } // TODO: impl capturer for stdin @@ -236,15 +232,18 @@ pub unsafe extern "C" fn wasi_env_read_stderr( } else { update_last_error("could not find a file handle for `stderr`"); return -1; - } + } } -fn read_inner(wasi_file: &mut Box, inner_buffer: &mut [u8]) -> isize { +fn read_inner( + wasi_file: &mut Box, + inner_buffer: &mut [u8], +) -> isize { match wasi_file.read(inner_buffer) { Ok(a) => a as isize, Err(err) => { update_last_error(format!("failed to read wasi_file: {}", err)); - -1 + -1 } } } diff --git a/lib/vfs/src/lib.rs b/lib/vfs/src/lib.rs index 65e725215df..b8670db35a2 100644 --- a/lib/vfs/src/lib.rs +++ b/lib/vfs/src/lib.rs @@ -162,7 +162,10 @@ impl OpenOptions { self } - pub fn open>(&mut self, path: P) -> Result> { + pub fn open>( + &mut self, + path: P, + ) -> Result> { self.opener.open(path.as_ref(), &self.conf) } } diff --git a/lib/wasi/src/lib.rs b/lib/wasi/src/lib.rs index 71375e7725a..cfad36e0317 100644 --- a/lib/wasi/src/lib.rs +++ b/lib/wasi/src/lib.rs @@ -64,7 +64,7 @@ use std::ops::Deref; use thiserror::Error; use wasmer::{ imports, Function, Imports, LazyInit, Memory, Memory32, MemoryAccessError, MemorySize, Module, - Store, WasmerEnv, TypedFunction, + Store, TypedFunction, WasmerEnv, }; pub use runtime::{ diff --git a/lib/wasi/src/state/guard.rs b/lib/wasi/src/state/guard.rs index 8e21647de35..e37c9c97845 100644 --- a/lib/wasi/src/state/guard.rs +++ b/lib/wasi/src/state/guard.rs @@ -1,7 +1,8 @@ -use std::{sync::{ - RwLockReadGuard, RwLockWriteGuard -}, io::{Seek, Read}}; use super::*; +use std::{ + io::{Read, Seek}, + sync::{RwLockReadGuard, RwLockWriteGuard}, +}; #[derive(Debug)] pub(crate) struct InodeValFileReadGuard<'a> { @@ -48,34 +49,30 @@ pub(crate) struct WasiStateFileGuard { inode: generational_arena::Index, } -impl WasiStateFileGuard -{ +impl WasiStateFileGuard { pub fn new(state: &WasiState, fd: __wasi_fd_t) -> Result, FsError> { let inodes = state.inodes.read().unwrap(); let fd_map = state.fs.fd_map.read().unwrap(); if let Some(fd) = fd_map.get(&fd) { let guard = inodes.arena[fd.inode].read(); if let Kind::File { .. } = guard.deref() { - Ok( - Some( - Self { - inodes: state.inodes.clone(), - inode: fd.inode - } - ) - ) + Ok(Some(Self { + inodes: state.inodes.clone(), + inode: fd.inode, + })) } else { // Our public API should ensure that this is not possible Err(FsError::NotAFile) } - } else { Ok(None) } } - pub fn lock_read<'a>(&self, inodes: &'a RwLockReadGuard) -> InodeValFileReadGuard<'a> - { + pub fn lock_read<'a>( + &self, + inodes: &'a RwLockReadGuard, + ) -> InodeValFileReadGuard<'a> { let guard = inodes.arena[self.inode].read(); if let Kind::File { .. } = guard.deref() { InodeValFileReadGuard { guard } @@ -85,8 +82,10 @@ impl WasiStateFileGuard } } - pub fn lock_write<'a>(&self, inodes: &'a RwLockReadGuard) -> InodeValFileWriteGuard<'a> - { + pub fn lock_write<'a>( + &self, + inodes: &'a RwLockReadGuard, + ) -> InodeValFileWriteGuard<'a> { let guard = inodes.arena[self.inode].write(); if let Kind::File { .. } = guard.deref() { InodeValFileWriteGuard { guard } @@ -97,9 +96,7 @@ impl WasiStateFileGuard } } -impl VirtualFile -for WasiStateFileGuard -{ +impl VirtualFile for WasiStateFileGuard { fn last_accessed(&self) -> u64 { let inodes = self.inodes.read().unwrap(); let guard = self.lock_read(&inodes); @@ -221,9 +218,7 @@ for WasiStateFileGuard } } -impl Write -for WasiStateFileGuard -{ +impl Write for WasiStateFileGuard { fn write(&mut self, buf: &[u8]) -> std::io::Result { let inodes = self.inodes.read().unwrap(); let mut guard = self.lock_write(&inodes); @@ -255,9 +250,7 @@ for WasiStateFileGuard } } -impl Read -for WasiStateFileGuard -{ +impl Read for WasiStateFileGuard { fn read(&mut self, buf: &mut [u8]) -> std::io::Result { let inodes = self.inodes.read().unwrap(); let mut guard = self.lock_write(&inodes); @@ -279,9 +272,7 @@ for WasiStateFileGuard } } -impl Seek -for WasiStateFileGuard -{ +impl Seek for WasiStateFileGuard { fn seek(&mut self, pos: std::io::SeekFrom) -> std::io::Result { let inodes = self.inodes.read().unwrap(); let mut guard = self.lock_write(&inodes); @@ -291,4 +282,4 @@ for WasiStateFileGuard Err(std::io::ErrorKind::Unsupported.into()) } } -} \ No newline at end of file +} diff --git a/lib/wasi/src/state/mod.rs b/lib/wasi/src/state/mod.rs index 484bc34c780..ed1fc4ea7c2 100644 --- a/lib/wasi/src/state/mod.rs +++ b/lib/wasi/src/state/mod.rs @@ -16,16 +16,16 @@ #![allow(clippy::cognitive_complexity, clippy::too_many_arguments)] mod builder; +mod guard; mod pipe; mod socket; mod types; -mod guard; pub use self::builder::*; +pub use self::guard::*; pub use self::pipe::*; pub use self::socket::*; pub use self::types::*; -pub use self::guard::*; use crate::syscalls::types::*; use crate::utils::map_io_err; use crate::WasiBusProcessId; @@ -1872,9 +1872,7 @@ impl WasiState { } /// Get the `VirtualFile` object at stdout - pub fn stdout( - &self, - ) -> Result>, FsError> { + pub fn stdout(&self) -> Result>, FsError> { self.std_dev_get(__WASI_STDOUT_FILENO) } @@ -1883,15 +1881,13 @@ impl WasiState { note = "stdout_mut() is no longer needed - just use stdout() instead" )] pub fn stdout_mut( - &self + &self, ) -> Result>, FsError> { self.stdout() } - + /// Get the `VirtualFile` object at stderr - pub fn stderr( - &self, - ) -> Result>, FsError> { + pub fn stderr(&self) -> Result>, FsError> { self.std_dev_get(__WASI_STDERR_FILENO) } @@ -1900,15 +1896,13 @@ impl WasiState { note = "stderr_mut() is no longer needed - just use stderr() instead" )] pub fn stderr_mut( - &self + &self, ) -> Result>, FsError> { self.stderr() } /// Get the `VirtualFile` object at stdin - pub fn stdin( - &self, - ) -> Result>, FsError> { + pub fn stdin(&self) -> Result>, FsError> { self.std_dev_get(__WASI_STDIN_FILENO) } @@ -1917,7 +1911,7 @@ impl WasiState { note = "stdin_mut() is no longer needed - just use stdin() instead" )] pub fn stdin_mut( - &self + &self, ) -> Result>, FsError> { self.stdin() } @@ -1928,15 +1922,12 @@ impl WasiState { &self, fd: __wasi_fd_t, ) -> Result>, FsError> { - let ret = WasiStateFileGuard::new(self, fd)? - .map(|a| { - let ret = Box::new(a); - let ret: Box = ret; - ret - }); - Ok( + let ret = WasiStateFileGuard::new(self, fd)?.map(|a| { + let ret = Box::new(a); + let ret: Box = ret; ret - ) + }); + Ok(ret) } } diff --git a/lib/wasi/tests/stdio.rs b/lib/wasi/tests/stdio.rs index bb7c0e62cc7..4cb56474552 100644 --- a/lib/wasi/tests/stdio.rs +++ b/lib/wasi/tests/stdio.rs @@ -23,7 +23,7 @@ mod sys { #[cfg(feature = "js")] mod js { use wasm_bindgen_test::*; - + #[wasm_bindgen_test] fn test_stdout() { super::test_stdout() @@ -162,7 +162,7 @@ fn test_stdin() { let start = instance.exports.get_function("_start").unwrap(); let result = start.call(&[]); assert!(result.is_err() == false); - + // We assure stdin is now empty let mut buf = Vec::new(); stdin.read_to_end(&mut buf).unwrap();