diff --git a/crates/generate/src/types.rs b/crates/generate/src/types.rs index 4d2376ab99..d2fc80fbce 100644 --- a/crates/generate/src/types.rs +++ b/crates/generate/src/types.rs @@ -179,7 +179,7 @@ fn define_flags(names: &Names, name: &witx::Id, f: &witx::FlagsDatatype) -> Toke } impl wiggle_runtime::GuestTypeCopy for #ident {} - impl wiggle_runtime::GuestTypeClone for #ident { + impl<'a> wiggle_runtime::GuestTypeClone<'a> for #ident { fn read_from_guest(location: &wiggle_runtime::GuestPtr<#ident>) -> Result<#ident, wiggle_runtime::GuestError> { Ok(*location.as_ref()?) } @@ -267,7 +267,7 @@ fn define_enum(names: &Names, name: &witx::Id, e: &witx::EnumDatatype) -> TokenS } impl wiggle_runtime::GuestTypeCopy for #ident {} - impl wiggle_runtime::GuestTypeClone for #ident { + impl<'a> wiggle_runtime::GuestTypeClone<'a> for #ident { fn read_from_guest(location: &wiggle_runtime::GuestPtr<#ident>) -> Result<#ident, wiggle_runtime::GuestError> { use ::std::convert::TryFrom; let raw: #repr = unsafe { (location.as_raw() as *const #repr).read() }; @@ -502,7 +502,7 @@ fn define_ptr_struct(names: &Names, name: &witx::Id, s: &witx::StructDatatype) - Ok(()) } } - impl<'a> wiggle_runtime::GuestTypePtr<'a> for #ident<'a> { + impl<'a> wiggle_runtime::GuestTypeClone<'a> for #ident<'a> { fn read_from_guest(location: &wiggle_runtime::GuestPtr<'a, #ident<'a>>) -> Result<#ident<'a>, wiggle_runtime::GuestError> { #(#member_reads)* Ok(#ident { #(#member_names),* }) diff --git a/crates/runtime/src/guest_type.rs b/crates/runtime/src/guest_type.rs index 6c2f98795e..83eacaa0ce 100644 --- a/crates/runtime/src/guest_type.rs +++ b/crates/runtime/src/guest_type.rs @@ -11,11 +11,7 @@ pub trait GuestType: Sized { } pub trait GuestTypeCopy: GuestType + Copy {} -pub trait GuestTypeClone: GuestType + Clone { - fn read_from_guest<'a>(location: &GuestPtr<'a, Self>) -> Result; - fn write_to_guest<'a>(&self, location: &GuestPtrMut<'a, Self>); -} -pub trait GuestTypePtr<'a>: GuestType { +pub trait GuestTypeClone<'a>: GuestType + Clone { fn read_from_guest(location: &GuestPtr<'a, Self>) -> Result; fn write_to_guest(&self, location: &GuestPtrMut<'a, Self>); } diff --git a/crates/runtime/src/lib.rs b/crates/runtime/src/lib.rs index 5c3cea5e70..b07da6a21d 100644 --- a/crates/runtime/src/lib.rs +++ b/crates/runtime/src/lib.rs @@ -5,6 +5,6 @@ mod memory; mod region; pub use error::GuestError; -pub use guest_type::{GuestErrorType, GuestType, GuestTypeClone, GuestTypeCopy, GuestTypePtr}; +pub use guest_type::{GuestErrorType, GuestType, GuestTypeClone, GuestTypeCopy}; pub use memory::{GuestArray, GuestMemory, GuestPtr, GuestPtrMut, GuestRef, GuestRefMut}; pub use region::Region; diff --git a/crates/runtime/src/memory/array.rs b/crates/runtime/src/memory/array.rs index 40a5e37d82..c9615b1e2f 100644 --- a/crates/runtime/src/memory/array.rs +++ b/crates/runtime/src/memory/array.rs @@ -2,6 +2,7 @@ use super::ptr::{GuestPtr, GuestRef}; use crate::{GuestError, GuestType, GuestTypeCopy}; use std::{fmt, ops::Deref}; +#[derive(Clone)] pub struct GuestArray<'a, T> where T: GuestType, @@ -145,9 +146,9 @@ where #[cfg(test)] mod test { - use super::super::{ - ptr::{GuestPtr, GuestPtrMut}, - GuestError, GuestMemory, Region, + use crate::{ + memory::ptr::{GuestPtr, GuestPtrMut}, + GuestError, GuestMemory, GuestTypeClone, Region, }; #[repr(align(4096))] @@ -248,9 +249,7 @@ mod test { let contents = arr .iter() .map(|ptr_ptr| { - *ptr_ptr - .expect("valid ptr to ptr") - .read_ptr_from_guest() + *GuestTypeClone::read_from_guest(&ptr_ptr.expect("valid ptr to ptr")) .expect("valid ptr to some value") .as_ref() .expect("deref ptr to some value") diff --git a/crates/runtime/src/memory/ptr.rs b/crates/runtime/src/memory/ptr.rs index da1fd3e4bc..afebb6c332 100644 --- a/crates/runtime/src/memory/ptr.rs +++ b/crates/runtime/src/memory/ptr.rs @@ -1,8 +1,5 @@ use super::{array::GuestArray, GuestMemory}; -use crate::{ - borrow::BorrowHandle, GuestError, GuestType, GuestTypeClone, GuestTypeCopy, GuestTypePtr, - Region, -}; +use crate::{borrow::BorrowHandle, GuestError, GuestType, GuestTypeClone, GuestTypeCopy, Region}; use std::{ fmt, marker::PhantomData, @@ -81,22 +78,13 @@ where impl<'a, T> GuestPtr<'a, T> where - T: GuestTypeClone, + T: GuestTypeClone<'a>, { pub fn clone_from_guest(&self) -> Result { T::read_from_guest(self) } } -impl<'a, T> GuestPtr<'a, T> -where - T: GuestTypePtr<'a>, -{ - pub fn read_ptr_from_guest(&self) -> Result { - T::read_from_guest(self) - } -} - impl<'a, T> GuestType for GuestPtr<'a, T> where T: GuestType, @@ -123,9 +111,9 @@ where } // Operations for reading and writing Ptrs to memory: -impl<'a, T> GuestTypePtr<'a> for GuestPtr<'a, T> +impl<'a, T> GuestTypeClone<'a> for GuestPtr<'a, T> where - T: GuestType, + T: GuestType + Clone, { fn read_from_guest(location: &GuestPtr<'a, Self>) -> Result { // location is guaranteed to be in GuestMemory and aligned to 4 @@ -220,7 +208,7 @@ where impl<'a, T> GuestPtrMut<'a, T> where - T: GuestTypePtr<'a>, + T: GuestTypeClone<'a>, { pub fn read_ptr_from_guest(&self) -> Result { T::read_from_guest(&self.as_immut()) @@ -231,19 +219,6 @@ where } } -impl<'a, T> GuestPtrMut<'a, T> -where - T: GuestTypeClone, -{ - pub fn clone_from_guest(&self) -> Result { - T::read_from_guest(&self.as_immut()) - } - - pub fn clone_to_guest(&self, val: &T) { - T::write_to_guest(val, &self) - } -} - impl<'a, T> GuestType for GuestPtrMut<'a, T> where T: GuestType, @@ -270,9 +245,9 @@ where } // Reading and writing GuestPtrMuts to memory: -impl<'a, T> GuestTypePtr<'a> for GuestPtrMut<'a, T> +impl<'a, T> GuestTypeClone<'a> for GuestPtrMut<'a, T> where - T: GuestType, + T: GuestType + Clone, { fn read_from_guest(location: &GuestPtr<'a, Self>) -> Result { // location is guaranteed to be in GuestMemory and aligned to 4 diff --git a/tests/main.rs b/tests/main.rs index 2b0800ebab..41aae0f5ab 100644 --- a/tests/main.rs +++ b/tests/main.rs @@ -56,11 +56,13 @@ impl foo::Foo for WasiCtx { println!("wrote to input2_ref {:?}", input3); // Read ptr value from mutable ptr: - let input4_ptr: GuestPtr = - input4_ptr_ptr.read_ptr_from_guest().map_err(|e| { - eprintln!("input4_ptr_ptr error: {}", e); - types::Errno::InvalidArg - })?; + let input4_ptr: GuestPtr = wiggle_runtime::GuestTypeClone::read_from_guest( + &input4_ptr_ptr.as_immut(), + ) + .map_err(|e| { + eprintln!("input4_ptr_ptr error: {}", e); + types::Errno::InvalidArg + })?; // Read enum value from that ptr: let input4: types::Excuse = *input4_ptr.as_ref().map_err(|e| { @@ -99,22 +101,22 @@ impl foo::Foo for WasiCtx { &mut self, excuses: &types::ConstExcuseArray, ) -> Result { - let last = excuses - .iter() - .last() - .expect("input array is non-empty") - .expect("valid ptr to ptr") - .read_ptr_from_guest() - .expect("valid ptr to some Excuse value"); + let last = wiggle_runtime::GuestTypeClone::read_from_guest( + &excuses + .iter() + .last() + .expect("input array is non-empty") + .expect("valid ptr to ptr"), + ) + .expect("valid ptr to some Excuse value"); Ok(*last.as_ref().expect("dereferencing ptr should succeed")) } fn populate_excuses(&mut self, excuses: &types::ExcuseArray) -> Result<(), types::Errno> { for excuse in excuses.iter() { - let ptr_to_ptr = excuse - .expect("valid ptr to ptr") - .read_ptr_from_guest() - .expect("valid ptr to some Excuse value"); + let ptr_to_ptr = + wiggle_runtime::GuestTypeClone::read_from_guest(&excuse.expect("valid ptr to ptr")) + .expect("valid ptr to some Excuse value"); let mut ptr = ptr_to_ptr .as_ref_mut() .expect("dereferencing mut ptr should succeed"); @@ -767,10 +769,9 @@ impl PopulateExcusesExcercise { .array(self.elements.len() as u32) .expect("as array"); for el in arr.iter() { - let ptr_to_ptr = el - .expect("valid ptr to ptr") - .read_ptr_from_guest() - .expect("valid ptr to some Excuse value"); + let ptr_to_ptr = + wiggle_runtime::GuestTypeClone::read_from_guest(&el.expect("valid ptr to ptr")) + .expect("valid ptr to some Excuse value"); assert_eq!( *ptr_to_ptr .as_ref()