From 8b48ce7fb79c7e36748e2e1a90be3bdf7d65cbce Mon Sep 17 00:00:00 2001 From: Nathaniel McCallum Date: Wed, 9 Mar 2022 18:22:42 -0500 Subject: [PATCH] feat(wasi)!: make most `WasiFile` methods take `&mut self` (#3901) 1. This makes it easier for implementors to deal with internal APIs. 2. This matches the signatures of the WASI Snapshot traits. Although it is likely true that these methods would have to become immutable in order to implement threading efficiently, threading will impact a large number of existing traits. So this change is practical for now with an already-unavoidable change required for threading. Signed-off-by: Nathaniel McCallum --- crates/wasi-common/cap-std-sync/src/file.rs | 32 ++++----- crates/wasi-common/cap-std-sync/src/net.rs | 14 ++-- crates/wasi-common/cap-std-sync/src/stdio.rs | 30 ++++----- crates/wasi-common/src/ctx.rs | 14 ++-- crates/wasi-common/src/file.rs | 37 ++++++----- crates/wasi-common/src/pipe.rs | 10 +-- crates/wasi-common/src/snapshots/preview_0.rs | 28 ++++---- crates/wasi-common/src/snapshots/preview_1.rs | 66 ++++++++++--------- crates/wasi-common/tokio/src/file.rs | 32 ++++----- crates/wasi-common/tokio/tests/poll_oneoff.rs | 2 +- 10 files changed, 138 insertions(+), 127 deletions(-) diff --git a/crates/wasi-common/cap-std-sync/src/file.rs b/crates/wasi-common/cap-std-sync/src/file.rs index 5c2a2b1820..8fec43c684 100644 --- a/crates/wasi-common/cap-std-sync/src/file.rs +++ b/crates/wasi-common/cap-std-sync/src/file.rs @@ -26,19 +26,19 @@ impl WasiFile for File { fn as_any(&self) -> &dyn Any { self } - async fn datasync(&self) -> Result<(), Error> { + async fn datasync(&mut self) -> Result<(), Error> { self.0.sync_data()?; Ok(()) } - async fn sync(&self) -> Result<(), Error> { + async fn sync(&mut self) -> Result<(), Error> { self.0.sync_all()?; Ok(()) } - async fn get_filetype(&self) -> Result { + async fn get_filetype(&mut self) -> Result { let meta = self.0.metadata()?; Ok(filetype_from(&meta.file_type())) } - async fn get_fdflags(&self) -> Result { + async fn get_fdflags(&mut self) -> Result { let fdflags = self.0.get_fd_flags()?; Ok(from_sysif_fdflags(fdflags)) } @@ -54,7 +54,7 @@ impl WasiFile for File { self.0.set_fd_flags(set_fd_flags)?; Ok(()) } - async fn get_filestat(&self) -> Result { + async fn get_filestat(&mut self) -> Result { let meta = self.0.metadata()?; Ok(Filestat { device_id: meta.dev(), @@ -67,20 +67,20 @@ impl WasiFile for File { ctim: meta.created().map(|t| Some(t.into_std())).unwrap_or(None), }) } - async fn set_filestat_size(&self, size: u64) -> Result<(), Error> { + async fn set_filestat_size(&mut self, size: u64) -> Result<(), Error> { self.0.set_len(size)?; Ok(()) } - async fn advise(&self, offset: u64, len: u64, advice: Advice) -> Result<(), Error> { + async fn advise(&mut self, offset: u64, len: u64, advice: Advice) -> Result<(), Error> { self.0.advise(offset, len, convert_advice(advice))?; Ok(()) } - async fn allocate(&self, offset: u64, len: u64) -> Result<(), Error> { + async fn allocate(&mut self, offset: u64, len: u64) -> Result<(), Error> { self.0.allocate(offset, len)?; Ok(()) } async fn set_times( - &self, + &mut self, atime: Option, mtime: Option, ) -> Result<(), Error> { @@ -88,41 +88,41 @@ impl WasiFile for File { .set_times(convert_systimespec(atime), convert_systimespec(mtime))?; Ok(()) } - async fn read_vectored<'a>(&self, bufs: &mut [io::IoSliceMut<'a>]) -> Result { + async fn read_vectored<'a>(&mut self, bufs: &mut [io::IoSliceMut<'a>]) -> Result { let n = self.0.read_vectored(bufs)?; Ok(n.try_into()?) } async fn read_vectored_at<'a>( - &self, + &mut self, bufs: &mut [io::IoSliceMut<'a>], offset: u64, ) -> Result { let n = self.0.read_vectored_at(bufs, offset)?; Ok(n.try_into()?) } - async fn write_vectored<'a>(&self, bufs: &[io::IoSlice<'a>]) -> Result { + async fn write_vectored<'a>(&mut self, bufs: &[io::IoSlice<'a>]) -> Result { let n = self.0.write_vectored(bufs)?; Ok(n.try_into()?) } async fn write_vectored_at<'a>( - &self, + &mut self, bufs: &[io::IoSlice<'a>], offset: u64, ) -> Result { let n = self.0.write_vectored_at(bufs, offset)?; Ok(n.try_into()?) } - async fn seek(&self, pos: std::io::SeekFrom) -> Result { + async fn seek(&mut self, pos: std::io::SeekFrom) -> Result { Ok(self.0.seek(pos)?) } - async fn peek(&self, buf: &mut [u8]) -> Result { + async fn peek(&mut self, buf: &mut [u8]) -> Result { let n = self.0.peek(buf)?; Ok(n.try_into()?) } async fn num_ready_bytes(&self) -> Result { Ok(self.0.num_ready_bytes()?) } - fn isatty(&self) -> bool { + fn isatty(&mut self) -> bool { self.0.is_terminal() } } diff --git a/crates/wasi-common/cap-std-sync/src/net.rs b/crates/wasi-common/cap-std-sync/src/net.rs index 850fedcc79..0fb82b5f02 100644 --- a/crates/wasi-common/cap-std-sync/src/net.rs +++ b/crates/wasi-common/cap-std-sync/src/net.rs @@ -91,11 +91,11 @@ macro_rules! wasi_listen_write_impl { stream.set_fdflags(fdflags).await?; Ok(Box::new(stream)) } - async fn get_filetype(&self) -> Result { + async fn get_filetype(&mut self) -> Result { Ok(FileType::SocketStream) } #[cfg(unix)] - async fn get_fdflags(&self) -> Result { + async fn get_fdflags(&mut self) -> Result { let fdflags = self.0.as_filelike().get_fd_flags()?; Ok(from_sysif_fdflags(fdflags)) } @@ -170,11 +170,11 @@ macro_rules! wasi_stream_write_impl { fn as_any(&self) -> &dyn Any { self } - async fn get_filetype(&self) -> Result { + async fn get_filetype(&mut self) -> Result { Ok(FileType::SocketStream) } #[cfg(unix)] - async fn get_fdflags(&self) -> Result { + async fn get_fdflags(&mut self) -> Result { let fdflags = self.0.as_filelike().get_fd_flags()?; Ok(from_sysif_fdflags(fdflags)) } @@ -191,19 +191,19 @@ macro_rules! wasi_stream_write_impl { Ok(()) } async fn read_vectored<'a>( - &self, + &mut self, bufs: &mut [io::IoSliceMut<'a>], ) -> Result { use std::io::Read; let n = Read::read_vectored(&mut *self.as_socketlike_view::<$std_ty>(), bufs)?; Ok(n.try_into()?) } - async fn write_vectored<'a>(&self, bufs: &[io::IoSlice<'a>]) -> Result { + async fn write_vectored<'a>(&mut self, bufs: &[io::IoSlice<'a>]) -> Result { use std::io::Write; let n = Write::write_vectored(&mut *self.as_socketlike_view::<$std_ty>(), bufs)?; Ok(n.try_into()?) } - async fn peek(&self, buf: &mut [u8]) -> Result { + async fn peek(&mut self, buf: &mut [u8]) -> Result { let n = self.0.peek(buf)?; Ok(n.try_into()?) } diff --git a/crates/wasi-common/cap-std-sync/src/stdio.rs b/crates/wasi-common/cap-std-sync/src/stdio.rs index 876b2cb45c..24f8631529 100644 --- a/crates/wasi-common/cap-std-sync/src/stdio.rs +++ b/crates/wasi-common/cap-std-sync/src/stdio.rs @@ -31,32 +31,32 @@ impl WasiFile for Stdin { fn as_any(&self) -> &dyn Any { self } - async fn get_filetype(&self) -> Result { + async fn get_filetype(&mut self) -> Result { if self.isatty() { Ok(FileType::CharacterDevice) } else { Ok(FileType::Unknown) } } - async fn read_vectored<'a>(&self, bufs: &mut [io::IoSliceMut<'a>]) -> Result { + async fn read_vectored<'a>(&mut self, bufs: &mut [io::IoSliceMut<'a>]) -> Result { let n = self.0.as_filelike_view::().read_vectored(bufs)?; Ok(n.try_into().map_err(|_| Error::range())?) } async fn read_vectored_at<'a>( - &self, + &mut self, _bufs: &mut [io::IoSliceMut<'a>], _offset: u64, ) -> Result { Err(Error::seek_pipe()) } - async fn seek(&self, _pos: std::io::SeekFrom) -> Result { + async fn seek(&mut self, _pos: std::io::SeekFrom) -> Result { Err(Error::seek_pipe()) } - async fn peek(&self, _buf: &mut [u8]) -> Result { + async fn peek(&mut self, _buf: &mut [u8]) -> Result { Err(Error::seek_pipe()) } async fn set_times( - &self, + &mut self, atime: Option, mtime: Option, ) -> Result<(), Error> { @@ -67,7 +67,7 @@ impl WasiFile for Stdin { async fn num_ready_bytes(&self) -> Result { Ok(self.0.num_ready_bytes()?) } - fn isatty(&self) -> bool { + fn isatty(&mut self) -> bool { self.0.is_terminal() } } @@ -98,17 +98,17 @@ macro_rules! wasi_file_write_impl { fn as_any(&self) -> &dyn Any { self } - async fn get_filetype(&self) -> Result { + async fn get_filetype(&mut self) -> Result { if self.isatty() { Ok(FileType::CharacterDevice) } else { Ok(FileType::Unknown) } } - async fn get_fdflags(&self) -> Result { + async fn get_fdflags(&mut self) -> Result { Ok(FdFlags::APPEND) } - async fn get_filestat(&self) -> Result { + async fn get_filestat(&mut self) -> Result { let meta = self.0.as_filelike_view::().metadata()?; Ok(Filestat { device_id: 0, @@ -121,22 +121,22 @@ macro_rules! wasi_file_write_impl { ctim: meta.created().ok(), }) } - async fn write_vectored<'a>(&self, bufs: &[io::IoSlice<'a>]) -> Result { + async fn write_vectored<'a>(&mut self, bufs: &[io::IoSlice<'a>]) -> Result { let n = self.0.as_filelike_view::().write_vectored(bufs)?; Ok(n.try_into().map_err(|c| Error::range().context(c))?) } async fn write_vectored_at<'a>( - &self, + &mut self, _bufs: &[io::IoSlice<'a>], _offset: u64, ) -> Result { Err(Error::seek_pipe()) } - async fn seek(&self, _pos: std::io::SeekFrom) -> Result { + async fn seek(&mut self, _pos: std::io::SeekFrom) -> Result { Err(Error::seek_pipe()) } async fn set_times( - &self, + &mut self, atime: Option, mtime: Option, ) -> Result<(), Error> { @@ -144,7 +144,7 @@ macro_rules! wasi_file_write_impl { .set_times(convert_systimespec(atime), convert_systimespec(mtime))?; Ok(()) } - fn isatty(&self) -> bool { + fn isatty(&mut self) -> bool { self.0.is_terminal() } } diff --git a/crates/wasi-common/src/ctx.rs b/crates/wasi-common/src/ctx.rs index 334f60474a..f99a9f7ed6 100644 --- a/crates/wasi-common/src/ctx.rs +++ b/crates/wasi-common/src/ctx.rs @@ -70,22 +70,22 @@ impl WasiCtx { Ok(()) } - pub fn set_stdin(&mut self, f: Box) { - let rights = Self::stdio_rights(&*f); + pub fn set_stdin(&mut self, mut f: Box) { + let rights = Self::stdio_rights(&mut *f); self.insert_file(0, f, rights); } - pub fn set_stdout(&mut self, f: Box) { - let rights = Self::stdio_rights(&*f); + pub fn set_stdout(&mut self, mut f: Box) { + let rights = Self::stdio_rights(&mut *f); self.insert_file(1, f, rights); } - pub fn set_stderr(&mut self, f: Box) { - let rights = Self::stdio_rights(&*f); + pub fn set_stderr(&mut self, mut f: Box) { + let rights = Self::stdio_rights(&mut *f); self.insert_file(2, f, rights); } - fn stdio_rights(f: &dyn WasiFile) -> FileCaps { + fn stdio_rights(f: &mut dyn WasiFile) -> FileCaps { let mut rights = FileCaps::all(); // If `f` is a tty, restrict the `tell` and `seek` capabilities, so diff --git a/crates/wasi-common/src/file.rs b/crates/wasi-common/src/file.rs index 0c342884d5..a846ae6779 100644 --- a/crates/wasi-common/src/file.rs +++ b/crates/wasi-common/src/file.rs @@ -5,9 +5,9 @@ use std::any::Any; #[wiggle::async_trait] pub trait WasiFile: Send + Sync { fn as_any(&self) -> &dyn Any; - async fn get_filetype(&self) -> Result; + async fn get_filetype(&mut self) -> Result; - fn isatty(&self) -> bool { + fn isatty(&mut self) -> bool { false } @@ -15,15 +15,15 @@ pub trait WasiFile: Send + Sync { Err(Error::badf()) } - async fn datasync(&self) -> Result<(), Error> { + async fn datasync(&mut self) -> Result<(), Error> { Ok(()) } - async fn sync(&self) -> Result<(), Error> { + async fn sync(&mut self) -> Result<(), Error> { Ok(()) } - async fn get_fdflags(&self) -> Result { + async fn get_fdflags(&mut self) -> Result { Ok(FdFlags::empty()) } @@ -31,7 +31,7 @@ pub trait WasiFile: Send + Sync { Err(Error::badf()) } - async fn get_filestat(&self) -> Result { + async fn get_filestat(&mut self) -> Result { Ok(Filestat { device_id: 0, inode: 0, @@ -44,55 +44,58 @@ pub trait WasiFile: Send + Sync { }) } - async fn set_filestat_size(&self, _size: u64) -> Result<(), Error> { + async fn set_filestat_size(&mut self, _size: u64) -> Result<(), Error> { Err(Error::badf()) } - async fn advise(&self, _offset: u64, _len: u64, _advice: Advice) -> Result<(), Error> { + async fn advise(&mut self, _offset: u64, _len: u64, _advice: Advice) -> Result<(), Error> { Err(Error::badf()) } - async fn allocate(&self, _offset: u64, _len: u64) -> Result<(), Error> { + async fn allocate(&mut self, _offset: u64, _len: u64) -> Result<(), Error> { Err(Error::badf()) } async fn set_times( - &self, + &mut self, _atime: Option, _mtime: Option, ) -> Result<(), Error> { Err(Error::badf()) } - async fn read_vectored<'a>(&self, _bufs: &mut [std::io::IoSliceMut<'a>]) -> Result { + async fn read_vectored<'a>( + &mut self, + _bufs: &mut [std::io::IoSliceMut<'a>], + ) -> Result { Err(Error::badf()) } async fn read_vectored_at<'a>( - &self, + &mut self, _bufs: &mut [std::io::IoSliceMut<'a>], _offset: u64, ) -> Result { Err(Error::badf()) } - async fn write_vectored<'a>(&self, _bufs: &[std::io::IoSlice<'a>]) -> Result { + async fn write_vectored<'a>(&mut self, _bufs: &[std::io::IoSlice<'a>]) -> Result { Err(Error::badf()) } async fn write_vectored_at<'a>( - &self, + &mut self, _bufs: &[std::io::IoSlice<'a>], _offset: u64, ) -> Result { Err(Error::badf()) } - async fn seek(&self, _pos: std::io::SeekFrom) -> Result { + async fn seek(&mut self, _pos: std::io::SeekFrom) -> Result { Err(Error::badf()) } - async fn peek(&self, _buf: &mut [u8]) -> Result { + async fn peek(&mut self, _buf: &mut [u8]) -> Result { Err(Error::badf()) } @@ -190,7 +193,7 @@ impl FileEntry { Ok(()) } - pub async fn get_fdstat(&self) -> Result { + pub async fn get_fdstat(&mut self) -> Result { Ok(FdStat { filetype: self.file.get_filetype().await?, caps: self.caps, diff --git a/crates/wasi-common/src/pipe.rs b/crates/wasi-common/src/pipe.rs index 1700131bd6..a5fceb80a1 100644 --- a/crates/wasi-common/src/pipe.rs +++ b/crates/wasi-common/src/pipe.rs @@ -105,10 +105,10 @@ impl WasiFile for ReadPipe { fn as_any(&self) -> &dyn Any { self } - async fn get_filetype(&self) -> Result { + async fn get_filetype(&mut self) -> Result { Ok(FileType::Pipe) } - async fn read_vectored<'a>(&self, bufs: &mut [io::IoSliceMut<'a>]) -> Result { + async fn read_vectored<'a>(&mut self, bufs: &mut [io::IoSliceMut<'a>]) -> Result { let n = self.borrow().read_vectored(bufs)?; Ok(n.try_into()?) } @@ -189,13 +189,13 @@ impl WasiFile for WritePipe { fn as_any(&self) -> &dyn Any { self } - async fn get_filetype(&self) -> Result { + async fn get_filetype(&mut self) -> Result { Ok(FileType::Pipe) } - async fn get_fdflags(&self) -> Result { + async fn get_fdflags(&mut self) -> Result { Ok(FdFlags::APPEND) } - async fn write_vectored<'a>(&self, bufs: &[io::IoSlice<'a>]) -> Result { + async fn write_vectored<'a>(&mut self, bufs: &[io::IoSlice<'a>]) -> Result { let n = self.borrow().write_vectored(bufs)?; Ok(n.try_into()?) } diff --git a/crates/wasi-common/src/snapshots/preview_0.rs b/crates/wasi-common/src/snapshots/preview_0.rs index 616a362aab..96f86820bf 100644 --- a/crates/wasi-common/src/snapshots/preview_0.rs +++ b/crates/wasi-common/src/snapshots/preview_0.rs @@ -462,8 +462,10 @@ impl wasi_unstable::WasiUnstable for WasiCtx { fd: types::Fd, iovs: &types::IovecArray<'a>, ) -> Result { - let table = self.table(); - let f = table.get_file(u32::from(fd))?.get_cap(FileCaps::READ)?; + let f = self + .table() + .get_file_mut(u32::from(fd))? + .get_cap_mut(FileCaps::READ)?; let mut guest_slices: Vec> = iovs .iter() @@ -489,10 +491,10 @@ impl wasi_unstable::WasiUnstable for WasiCtx { iovs: &types::IovecArray<'a>, offset: types::Filesize, ) -> Result { - let table = self.table(); - let f = table - .get_file(u32::from(fd))? - .get_cap(FileCaps::READ | FileCaps::SEEK)?; + let f = self + .table() + .get_file_mut(u32::from(fd))? + .get_cap_mut(FileCaps::READ | FileCaps::SEEK)?; let mut guest_slices: Vec> = iovs .iter() @@ -517,8 +519,10 @@ impl wasi_unstable::WasiUnstable for WasiCtx { fd: types::Fd, ciovs: &types::CiovecArray<'a>, ) -> Result { - let table = self.table(); - let f = table.get_file(u32::from(fd))?.get_cap(FileCaps::WRITE)?; + let f = self + .table() + .get_file_mut(u32::from(fd))? + .get_cap_mut(FileCaps::WRITE)?; let guest_slices: Vec> = ciovs .iter() @@ -544,10 +548,10 @@ impl wasi_unstable::WasiUnstable for WasiCtx { ciovs: &types::CiovecArray<'a>, offset: types::Filesize, ) -> Result { - let table = self.table(); - let f = table - .get_file(u32::from(fd))? - .get_cap(FileCaps::WRITE | FileCaps::SEEK)?; + let f = self + .table() + .get_file_mut(u32::from(fd))? + .get_cap_mut(FileCaps::WRITE | FileCaps::SEEK)?; let guest_slices: Vec> = ciovs .iter() diff --git a/crates/wasi-common/src/snapshots/preview_1.rs b/crates/wasi-common/src/snapshots/preview_1.rs index 0279bcbb41..9c6f372d3d 100644 --- a/crates/wasi-common/src/snapshots/preview_1.rs +++ b/crates/wasi-common/src/snapshots/preview_1.rs @@ -260,8 +260,8 @@ impl wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx { advice: types::Advice, ) -> Result<(), Error> { self.table() - .get_file(u32::from(fd))? - .get_cap(FileCaps::ADVISE)? + .get_file_mut(u32::from(fd))? + .get_cap_mut(FileCaps::ADVISE)? .advise(offset, len, advice.into()) .await?; Ok(()) @@ -274,8 +274,8 @@ impl wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx { len: types::Filesize, ) -> Result<(), Error> { self.table() - .get_file(u32::from(fd))? - .get_cap(FileCaps::ALLOCATE)? + .get_file_mut(u32::from(fd))? + .get_cap_mut(FileCaps::ALLOCATE)? .allocate(offset, len) .await?; Ok(()) @@ -309,8 +309,8 @@ impl wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx { async fn fd_datasync(&mut self, fd: types::Fd) -> Result<(), Error> { self.table() - .get_file(u32::from(fd))? - .get_cap(FileCaps::DATASYNC)? + .get_file_mut(u32::from(fd))? + .get_cap_mut(FileCaps::DATASYNC)? .datasync() .await?; Ok(()) @@ -320,7 +320,7 @@ impl wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx { let table = self.table(); let fd = u32::from(fd); if table.is::(fd) { - let file_entry: &FileEntry = table.get(fd)?; + let file_entry: &mut FileEntry = table.get_mut(fd)?; let fdstat = file_entry.get_fdstat().await?; Ok(types::Fdstat::from(&fdstat)) } else if table.is::(fd) { @@ -371,8 +371,8 @@ impl wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx { let fd = u32::from(fd); if table.is::(fd) { let filestat = table - .get_file(fd)? - .get_cap(FileCaps::FILESTAT_GET)? + .get_file_mut(fd)? + .get_cap_mut(FileCaps::FILESTAT_GET)? .get_filestat() .await?; Ok(filestat.into()) @@ -394,8 +394,8 @@ impl wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx { size: types::Filesize, ) -> Result<(), Error> { self.table() - .get_file(u32::from(fd))? - .get_cap(FileCaps::FILESTAT_SET_SIZE)? + .get_file_mut(u32::from(fd))? + .get_cap_mut(FileCaps::FILESTAT_SET_SIZE)? .set_filestat_size(size) .await?; Ok(()) @@ -421,9 +421,9 @@ impl wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx { if table.is::(fd) { table - .get_file(fd) + .get_file_mut(fd) .expect("checked that entry is file") - .get_cap(FileCaps::FILESTAT_SET_TIMES)? + .get_cap_mut(FileCaps::FILESTAT_SET_TIMES)? .set_times(atim, mtim) .await } else if table.is::(fd) { @@ -443,8 +443,10 @@ impl wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx { fd: types::Fd, iovs: &types::IovecArray<'a>, ) -> Result { - let table = self.table(); - let f = table.get_file(u32::from(fd))?.get_cap(FileCaps::READ)?; + let f = self + .table() + .get_file_mut(u32::from(fd))? + .get_cap_mut(FileCaps::READ)?; let mut guest_slices: Vec> = iovs .iter() @@ -470,10 +472,10 @@ impl wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx { iovs: &types::IovecArray<'a>, offset: types::Filesize, ) -> Result { - let table = self.table(); - let f = table - .get_file(u32::from(fd))? - .get_cap(FileCaps::READ | FileCaps::SEEK)?; + let f = self + .table() + .get_file_mut(u32::from(fd))? + .get_cap_mut(FileCaps::READ | FileCaps::SEEK)?; let mut guest_slices: Vec> = iovs .iter() @@ -498,8 +500,10 @@ impl wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx { fd: types::Fd, ciovs: &types::CiovecArray<'a>, ) -> Result { - let table = self.table(); - let f = table.get_file(u32::from(fd))?.get_cap(FileCaps::WRITE)?; + let f = self + .table() + .get_file_mut(u32::from(fd))? + .get_cap_mut(FileCaps::WRITE)?; let guest_slices: Vec> = ciovs .iter() @@ -525,10 +529,10 @@ impl wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx { ciovs: &types::CiovecArray<'a>, offset: types::Filesize, ) -> Result { - let table = self.table(); - let f = table - .get_file(u32::from(fd))? - .get_cap(FileCaps::WRITE | FileCaps::SEEK)?; + let f = self + .table() + .get_file_mut(u32::from(fd))? + .get_cap_mut(FileCaps::WRITE | FileCaps::SEEK)?; let guest_slices: Vec> = ciovs .iter() @@ -622,8 +626,8 @@ impl wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx { }; let newoffset = self .table() - .get_file(u32::from(fd))? - .get_cap(required_caps)? + .get_file_mut(u32::from(fd))? + .get_cap_mut(required_caps)? .seek(whence) .await?; Ok(newoffset) @@ -631,8 +635,8 @@ impl wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx { async fn fd_sync(&mut self, fd: types::Fd) -> Result<(), Error> { self.table() - .get_file(u32::from(fd))? - .get_cap(FileCaps::SYNC)? + .get_file_mut(u32::from(fd))? + .get_cap_mut(FileCaps::SYNC)? .sync() .await?; Ok(()) @@ -642,8 +646,8 @@ impl wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx { // XXX should this be stream_position? let offset = self .table() - .get_file(u32::from(fd))? - .get_cap(FileCaps::TELL)? + .get_file_mut(u32::from(fd))? + .get_cap_mut(FileCaps::TELL)? .seek(std::io::SeekFrom::Current(0)) .await?; Ok(offset) diff --git a/crates/wasi-common/tokio/src/file.rs b/crates/wasi-common/tokio/src/file.rs index 168a8de28d..b80bd66e19 100644 --- a/crates/wasi-common/tokio/src/file.rs +++ b/crates/wasi-common/tokio/src/file.rs @@ -94,64 +94,64 @@ macro_rules! wasi_file_impl { fn as_any(&self) -> &dyn Any { self } - async fn datasync(&self) -> Result<(), Error> { + async fn datasync(&mut self) -> Result<(), Error> { block_on_dummy_executor(|| self.0.datasync()) } - async fn sync(&self) -> Result<(), Error> { + async fn sync(&mut self) -> Result<(), Error> { block_on_dummy_executor(|| self.0.sync()) } - async fn get_filetype(&self) -> Result { + async fn get_filetype(&mut self) -> Result { block_on_dummy_executor(|| self.0.get_filetype()) } - async fn get_fdflags(&self) -> Result { + async fn get_fdflags(&mut self) -> Result { block_on_dummy_executor(|| self.0.get_fdflags()) } async fn set_fdflags(&mut self, fdflags: FdFlags) -> Result<(), Error> { block_on_dummy_executor(|| self.0.set_fdflags(fdflags)) } - async fn get_filestat(&self) -> Result { + async fn get_filestat(&mut self) -> Result { block_on_dummy_executor(|| self.0.get_filestat()) } - async fn set_filestat_size(&self, size: u64) -> Result<(), Error> { + async fn set_filestat_size(&mut self, size: u64) -> Result<(), Error> { block_on_dummy_executor(move || self.0.set_filestat_size(size)) } - async fn advise(&self, offset: u64, len: u64, advice: Advice) -> Result<(), Error> { + async fn advise(&mut self, offset: u64, len: u64, advice: Advice) -> Result<(), Error> { block_on_dummy_executor(move || self.0.advise(offset, len, advice)) } - async fn allocate(&self, offset: u64, len: u64) -> Result<(), Error> { + async fn allocate(&mut self, offset: u64, len: u64) -> Result<(), Error> { block_on_dummy_executor(move || self.0.allocate(offset, len)) } async fn read_vectored<'a>( - &self, + &mut self, bufs: &mut [io::IoSliceMut<'a>], ) -> Result { block_on_dummy_executor(move || self.0.read_vectored(bufs)) } async fn read_vectored_at<'a>( - &self, + &mut self, bufs: &mut [io::IoSliceMut<'a>], offset: u64, ) -> Result { block_on_dummy_executor(move || self.0.read_vectored_at(bufs, offset)) } - async fn write_vectored<'a>(&self, bufs: &[io::IoSlice<'a>]) -> Result { + async fn write_vectored<'a>(&mut self, bufs: &[io::IoSlice<'a>]) -> Result { block_on_dummy_executor(move || self.0.write_vectored(bufs)) } async fn write_vectored_at<'a>( - &self, + &mut self, bufs: &[io::IoSlice<'a>], offset: u64, ) -> Result { block_on_dummy_executor(move || self.0.write_vectored_at(bufs, offset)) } - async fn seek(&self, pos: std::io::SeekFrom) -> Result { + async fn seek(&mut self, pos: std::io::SeekFrom) -> Result { block_on_dummy_executor(move || self.0.seek(pos)) } - async fn peek(&self, buf: &mut [u8]) -> Result { + async fn peek(&mut self, buf: &mut [u8]) -> Result { block_on_dummy_executor(move || self.0.peek(buf)) } async fn set_times( - &self, + &mut self, atime: Option, mtime: Option, ) -> Result<(), Error> { @@ -160,7 +160,7 @@ macro_rules! wasi_file_impl { async fn num_ready_bytes(&self) -> Result { block_on_dummy_executor(|| self.0.num_ready_bytes()) } - fn isatty(&self) -> bool { + fn isatty(&mut self) -> bool { self.0.isatty() } diff --git a/crates/wasi-common/tokio/tests/poll_oneoff.rs b/crates/wasi-common/tokio/tests/poll_oneoff.rs index 9ba85f6dee..abaacef891 100644 --- a/crates/wasi-common/tokio/tests/poll_oneoff.rs +++ b/crates/wasi-common/tokio/tests/poll_oneoff.rs @@ -20,7 +20,7 @@ async fn empty_file_readable() -> Result<(), Error> { let d = workspace.open_dir("d").context("open dir")?; let d = Dir::from_cap_std(d); - let f = d + let mut f = d .open_file(false, "f", OFlags::CREATE, false, true, FdFlags::empty()) .await .context("create writable file f")?;