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§

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§

Converts a slice of raw data into a reference of Self. Read more
Converts a mutable slice of raw data into a mutable reference of Self. Read more
Creates an instance of Self by copying raw data from an io::Read stream.
Converts a reference to self into a slice of bytes. Read more
Converts a mutable reference to self into a mutable slice of bytes. Read more
Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Returns the “default value” for a type. Read more
Converts to this type from the input type.
Converts to this type from the input type.
This method tests for self and other values to be equal, and is used by ==.
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
This method tests for self and other values to be equal, and is used by ==.
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
This method tests for self and other values to be equal, and is used by ==.
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.