Revert changes and require Clone if Copy

This commit aligns `wiggle` a little bit closer with Rust proper in
the sense that now `GuestTypeCopy` implies `GuestTypeClone` which
in turn implies that any type implementing `GuestTypeCopy` will have
to provide `read_from_guest` and `write_to_guest` methods. As a result,
we can safely revert changes introduced in the previous commit.
This commit is contained in:
Jakub Konka
2020-02-24 21:38:12 +01:00
committed by Jakub Konka
parent 694cf117bb
commit a02bce6eaf
4 changed files with 36 additions and 41 deletions

View File

@@ -10,11 +10,12 @@ pub trait GuestType: Sized {
fn validate<'a>(location: &GuestPtr<'a, Self>) -> Result<(), GuestError>;
}
pub trait GuestTypeCopy: GuestType + Copy {}
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>);
}
// Following trait system in Rust, Copy implies Clone
pub trait GuestTypeCopy<'a>: GuestTypeClone<'a> + Copy {}
macro_rules! builtin_type {
( $( $t:ident ), * ) => {
@@ -33,7 +34,15 @@ macro_rules! builtin_type {
Ok(())
}
}
impl GuestTypeCopy for $t {}
impl<'a> GuestTypeClone<'a> for $t {
fn read_from_guest(location: &GuestPtr<'a, Self>) -> Result<Self, GuestError> {
Ok(*location.as_ref()?)
}
fn write_to_guest(&self, location: &GuestPtrMut<'a, Self>) {
unsafe { (location.as_raw() as *mut $t).write(*self) };
}
}
impl<'a> GuestTypeCopy<'a> for $t {}
)*
};
}

View File

@@ -79,7 +79,7 @@ where
impl<'a, T> GuestArray<'a, T>
where
T: GuestTypeCopy,
T: GuestTypeCopy<'a>,
{
pub fn as_ref(&self) -> Result<GuestArrayRef<'a, T>, GuestError> {
let mut next = self.ptr.elem(0)?;
@@ -109,7 +109,7 @@ where
pub struct GuestArrayRef<'a, T>
where
T: GuestTypeCopy,
T: GuestTypeCopy<'a>,
{
ref_: GuestRef<'a, T>,
num_elems: u32,
@@ -117,7 +117,7 @@ where
impl<'a, T> fmt::Debug for GuestArrayRef<'a, T>
where
T: GuestTypeCopy + fmt::Debug,
T: GuestTypeCopy<'a> + fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
@@ -130,7 +130,7 @@ where
impl<'a, T> Deref for GuestArrayRef<'a, T>
where
T: GuestTypeCopy,
T: GuestTypeCopy<'a>,
{
type Target = [T];

View File

@@ -57,7 +57,7 @@ impl<'a, T: GuestType> GuestPtr<'a, T> {
impl<'a, T> GuestPtr<'a, T>
where
T: GuestTypeCopy,
T: GuestTypeCopy<'a>,
{
pub fn as_ref(&self) -> Result<GuestRef<'a, T>, GuestError> {
T::validate(&self)?;
@@ -183,7 +183,7 @@ where
impl<'a, T> GuestPtrMut<'a, T>
where
T: GuestTypeCopy,
T: GuestTypeCopy<'a>,
{
pub fn as_ref(&self) -> Result<GuestRef<'a, T>, GuestError> {
self.as_immut().as_ref()
@@ -298,7 +298,7 @@ impl<'a, T> GuestRef<'a, T> {
impl<'a, T> Deref for GuestRef<'a, T>
where
T: GuestTypeCopy,
T: GuestTypeCopy<'a>,
{
type Target = T;
@@ -357,7 +357,7 @@ impl<'a, T> GuestRefMut<'a, T> {
impl<'a, T> ::std::ops::Deref for GuestRefMut<'a, T>
where
T: GuestTypeCopy,
T: GuestTypeCopy<'a>,
{
type Target = T;
@@ -372,7 +372,7 @@ where
impl<'a, T> DerefMut for GuestRefMut<'a, T>
where
T: GuestTypeCopy,
T: GuestTypeCopy<'a>,
{
fn deref_mut(&mut self) -> &mut Self::Target {
unsafe {