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:
Pat Hickey
2020-02-21 08:43:45 -08:00
committed by GitHub
parent 3ef24a04fe
commit 2f223acc55
6 changed files with 38 additions and 67 deletions

View File

@@ -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<Self, GuestError>;
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<Self, GuestError>;
fn write_to_guest(&self, location: &GuestPtrMut<'a, Self>);
}

View File

@@ -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;

View File

@@ -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")

View File

@@ -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