flesh out the guest type traits a bit further

This commit is contained in:
Pat Hickey
2020-01-22 16:38:25 -08:00
parent b8feffe6e1
commit aa5c5f7018
5 changed files with 126 additions and 77 deletions

View File

@@ -33,7 +33,7 @@ impl<'a> GuestMemory<'a> {
pub fn ptr<T: GuestType>(&'a self, at: u32) -> Result<GuestPtr<'a, T>, MemoryError> {
let r = Region {
start: at,
len: T::len(),
len: T::size(),
};
let mut borrows = self.borrows.borrow_mut();
if !self.contains(r) {
@@ -53,7 +53,7 @@ impl<'a> GuestMemory<'a> {
pub fn ptr_mut<T: GuestType>(&'a self, at: u32) -> Result<GuestPtrMut<'a, T>, MemoryError> {
let r = Region {
start: at,
len: T::len(),
len: T::size(),
};
let mut borrows = self.borrows.borrow_mut();
if !self.contains(r) {
@@ -77,10 +77,19 @@ pub struct GuestPtr<'a, T> {
type_: PhantomData<T>,
}
impl<'a, T> GuestPtr<'a, T> {
impl<'a, T: GuestType> GuestPtr<'a, T> {
pub fn ptr(&self) -> *const u8 {
(self.mem.ptr as usize + self.region.start as usize) as *const u8
}
pub unsafe fn downcast<Q: GuestType>(self) -> GuestPtr<'a, Q> {
debug_assert!(T::size() == Q::size(), "downcast to type of same size");
GuestPtr {
mem: self.mem,
region: self.region,
type_: PhantomData,
}
}
}
impl<'a, T> Drop for GuestPtr<'a, T> {