merge GuestTypePtr and GuestTypeClone into a single GuestTypeClone<'a> trait (#14)
* merge GuestTypePtr and GuestTypeClone into a single GuestTypeClone<'a> trait * GuestArray can derive Clone (but not impl GuestTypeClone) * fix array tests * Fix GuestTypeClone for flags Co-authored-by: Jakub Konka <kubkon@jakubkonka.com>
This commit is contained in:
@@ -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")
|
||||
|
||||
@@ -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, GuestError> {
|
||||
T::read_from_guest(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T> GuestPtr<'a, T>
|
||||
where
|
||||
T: GuestTypePtr<'a>,
|
||||
{
|
||||
pub fn read_ptr_from_guest(&self) -> Result<T, GuestError> {
|
||||
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<Self, GuestError> {
|
||||
// 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, GuestError> {
|
||||
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, GuestError> {
|
||||
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<Self, GuestError> {
|
||||
// location is guaranteed to be in GuestMemory and aligned to 4
|
||||
|
||||
Reference in New Issue
Block a user