Browse Source

wiggle: implement array get/get_range in terms of ptr/add/as_array

hard to get it wrong when you use the safe primitives i already made!
pull/2160/head
Pat Hickey 4 years ago
parent
commit
580c236dee
  1. 26
      crates/wiggle/src/lib.rs

26
crates/wiggle/src/lib.rs

@ -543,24 +543,36 @@ impl<'a, T> GuestPtr<'a, [T]> {
GuestPtr::new(self.mem, self.offset_base()) GuestPtr::new(self.mem, self.offset_base())
} }
pub fn get(&self, index: u32) -> Option<GuestPtr<'a, T>> { pub fn get(&self, index: u32) -> Option<GuestPtr<'a, T>>
where
T: GuestType<'a>,
{
if index < self.len() { if index < self.len() {
Some(GuestPtr::new(self.mem, self.offset_base() + index)) Some(
self.as_ptr()
.add(index)
.expect("just performed bounds check"),
)
} else { } else {
None None
} }
} }
pub fn get_range(&self, r: std::ops::Range<u32>) -> Option<GuestPtr<'a, [T]>> { pub fn get_range(&self, r: std::ops::Range<u32>) -> Option<GuestPtr<'a, [T]>>
where
T: GuestType<'a>,
{
if r.end < r.start { if r.end < r.start {
return None; return None;
} }
let range_length = r.end - r.start; let range_length = r.end - r.start;
if r.start <= self.len() && r.end <= self.len() { if r.start <= self.len() && r.end <= self.len() {
Some(GuestPtr::new( Some(
self.mem, self.as_ptr()
(self.offset_base() + r.start, range_length), .add(r.start)
)) .expect("just performed bounds check")
.as_array(range_length),
)
} else { } else {
None None
} }

Loading…
Cancel
Save