Struct virtio_driver::Le16
source · pub struct Le16(_);Expand description
An integer type of with an explicit endianness.
See module level documentation for examples.
Implementations§
source§impl Le16
impl Le16
sourcepub fn to_native(self) -> u16
pub fn to_native(self) -> u16
Converts self to the native endianness.
Examples found in repository?
src/virtqueue.rs (line 347)
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
pub fn add_request<F>(&mut self, prepare: F) -> Result<u16, Error>
where
F: FnOnce(&mut R, &mut dyn FnMut(iovec, bool) -> Result<(), Error>) -> Result<(), Error>,
{
let first_idx = match self.first_free_desc {
NO_FREE_DESC => {
return Err(Error::new(ErrorKind::Other, "Not enough free descriptors"));
}
idx => idx,
};
let req_ptr = unsafe { &mut *self.req.offset(first_idx as isize) };
let mut last_idx: Option<u16> = None;
let res = prepare(req_ptr, &mut |iovec: iovec, from_dev: bool| {
// Set NEXT for all descriptors, it is unset again below for the last one
let mut flags = VirtqueueDescriptorFlags::NEXT;
if from_dev {
flags.insert(VirtqueueDescriptorFlags::WRITE);
}
last_idx = Some(self.add_desc(iovec, flags)?);
Ok(())
});
if let Err(e) = res {
self.first_free_desc = first_idx;
return Err(e);
}
let mut last_flags = self.desc[last_idx.unwrap() as usize].flags.to_native();
last_flags &= !VirtqueueDescriptorFlags::NEXT.bits();
self.desc[last_idx.unwrap() as usize].flags = last_flags.into();
self.add_avail(&[first_idx]);
Ok(first_idx)
}
fn free_desc(&mut self, first_idx: u16) {
let mut idx = first_idx as usize;
while self.desc[idx].flags.to_native() & VirtqueueDescriptorFlags::NEXT.bits() != 0 {
idx = self.desc[idx].next.to_native().into();
}
self.desc[idx].next = self.first_free_desc.into();
self.first_free_desc = first_idx;
}Trait Implementations§
source§impl ByteValued for Le16
impl ByteValued for Le16
source§fn from_slice(data: &[u8]) -> Option<&Self>
fn from_slice(data: &[u8]) -> Option<&Self>
Converts a slice of raw data into a reference of
Self. Read moresource§fn from_mut_slice(data: &mut [u8]) -> Option<&mut Self>
fn from_mut_slice(data: &mut [u8]) -> Option<&mut Self>
Converts a mutable slice of raw data into a mutable reference of
Self. Read moresource§fn from_reader<R: Read>(read: R) -> Result<Self>
fn from_reader<R: Read>(read: R) -> Result<Self>
Creates an instance of
Self by copying raw data from an io::Read stream.