diff --git a/crates/wasi-common/src/virtfs/pipe.rs b/crates/wasi-common/src/virtfs/pipe.rs index b081a68171..ef253b72ae 100644 --- a/crates/wasi-common/src/virtfs/pipe.rs +++ b/crates/wasi-common/src/virtfs/pipe.rs @@ -26,12 +26,21 @@ use std::sync::{Arc, RwLock}; /// let stdin = ReadPipe::from("hello from stdin!"); /// ctx.stdin(stdin); /// ``` -#[derive(Clone, Debug)] +#[derive(Debug)] pub struct ReadPipe { - rights: Arc>, + rights: RwLock, reader: Arc>, } +impl Clone for ReadPipe { + fn clone(&self) -> Self { + Self { + rights: RwLock::new(*self.rights.read().unwrap()), + reader: self.reader.clone(), + } + } +} + impl ReadPipe { /// Create a new pipe from a `Read` type. /// @@ -46,14 +55,14 @@ impl ReadPipe { pub fn from_shared(reader: Arc>) -> Self { use types::Rights; Self { - rights: Arc::new(RwLock::new(HandleRights::from_base( + rights: RwLock::new(HandleRights::from_base( Rights::FD_DATASYNC | Rights::FD_FDSTAT_SET_FLAGS | Rights::FD_READ | Rights::FD_SYNC | Rights::FD_FILESTAT_GET | Rights::POLL_FD_READWRITE, - ))), + )), reader, } } @@ -102,10 +111,7 @@ impl Handle for ReadPipe { } fn try_clone(&self) -> io::Result> { - Ok(Box::new(Self { - rights: self.rights.clone(), - reader: self.reader.clone(), - })) + Ok(Box::new(self.clone())) } fn get_file_type(&self) -> types::Filetype { @@ -113,7 +119,7 @@ impl Handle for ReadPipe { } fn get_rights(&self) -> HandleRights { - self.rights.read().unwrap().clone() + *self.rights.read().unwrap() } fn set_rights(&self, rights: HandleRights) { @@ -222,12 +228,21 @@ impl Handle for ReadPipe { } /// A virtual pipe write end. -#[derive(Clone, Debug)] +#[derive(Debug)] pub struct WritePipe { - rights: Arc>, + rights: RwLock, writer: Arc>, } +impl Clone for WritePipe { + fn clone(&self) -> Self { + Self { + rights: RwLock::new(*self.rights.read().unwrap()), + writer: self.writer.clone(), + } + } +} + impl WritePipe { /// Create a new pipe from a `Write` type. /// @@ -242,14 +257,14 @@ impl WritePipe { pub fn from_shared(writer: Arc>) -> Self { use types::Rights; Self { - rights: Arc::new(RwLock::new(HandleRights::from_base( + rights: RwLock::new(HandleRights::from_base( Rights::FD_DATASYNC | Rights::FD_FDSTAT_SET_FLAGS | Rights::FD_SYNC | Rights::FD_WRITE | Rights::FD_FILESTAT_GET | Rights::POLL_FD_READWRITE, - ))), + )), writer, } } @@ -281,10 +296,7 @@ impl Handle for WritePipe { } fn try_clone(&self) -> io::Result> { - Ok(Box::new(Self { - rights: self.rights.clone(), - writer: self.writer.clone(), - })) + Ok(Box::new(self.clone())) } fn get_file_type(&self) -> types::Filetype { @@ -292,7 +304,7 @@ impl Handle for WritePipe { } fn get_rights(&self) -> HandleRights { - self.rights.read().unwrap().clone() + *self.rights.read().unwrap() } fn set_rights(&self, rights: HandleRights) {