vmm_sys_util::fam

Trait FamStruct

Source
pub unsafe trait FamStruct {
    type Entry: PartialEq + Copy;

    // Required methods
    fn len(&self) -> usize;
    fn set_len(&mut self, len: usize);
    fn max_len() -> usize;
    fn as_slice(&self) -> &[Self::Entry];
    fn as_mut_slice(&mut self) -> &mut [Self::Entry];
}
Expand description

Trait for accessing properties of C defined FAM structures.

§Safety

This is unsafe due to the number of constraints that aren’t checked:

  • the implementer should be a POD
  • the implementor should contain a flexible array member of elements of type Entry
  • Entry should be a POD

Violating these may cause problems.

§Example

use vmm_sys_util::fam::*;

#[repr(C)]
#[derive(Default)]
pub struct __IncompleteArrayField<T>(::std::marker::PhantomData<T>, [T; 0]);
impl<T> __IncompleteArrayField<T> {
    #[inline]
    pub fn new() -> Self {
        __IncompleteArrayField(::std::marker::PhantomData, [])
    }
    #[inline]
    pub unsafe fn as_ptr(&self) -> *const T {
        ::std::mem::transmute(self)
    }
    #[inline]
    pub unsafe fn as_mut_ptr(&mut self) -> *mut T {
        ::std::mem::transmute(self)
    }
    #[inline]
    pub unsafe fn as_slice(&self, len: usize) -> &[T] {
        ::std::slice::from_raw_parts(self.as_ptr(), len)
    }
    #[inline]
    pub unsafe fn as_mut_slice(&mut self, len: usize) -> &mut [T] {
        ::std::slice::from_raw_parts_mut(self.as_mut_ptr(), len)
    }
}

#[repr(C)]
#[derive(Default)]
struct MockFamStruct {
    pub len: u32,
    pub padding: u32,
    pub entries: __IncompleteArrayField<u32>,
}

unsafe impl FamStruct for MockFamStruct {
    type Entry = u32;

    fn len(&self) -> usize {
        self.len as usize
    }

    fn set_len(&mut self, len: usize) {
        self.len = len as u32
    }

    fn max_len() -> usize {
        100
    }

    fn as_slice(&self) -> &[u32] {
        let len = self.len();
        unsafe { self.entries.as_slice(len) }
    }

    fn as_mut_slice(&mut self) -> &mut [u32] {
        let len = self.len();
        unsafe { self.entries.as_mut_slice(len) }
    }
}

type MockFamStructWrapper = FamStructWrapper<MockFamStruct>;

Required Associated Types§

Source

type Entry: PartialEq + Copy

The type of the FAM entries

Required Methods§

Source

fn len(&self) -> usize

Get the FAM length

These type of structures contain a member that holds the FAM length. This method will return the value of that member.

Source

fn set_len(&mut self, len: usize)

Set the FAM length

These type of structures contain a member that holds the FAM length. This method will set the value of that member.

Source

fn max_len() -> usize

Get max allowed FAM length

This depends on each structure. For example a structure representing the cpuid can contain at most 80 entries.

Source

fn as_slice(&self) -> &[Self::Entry]

Get the FAM entries as slice

Source

fn as_mut_slice(&mut self) -> &mut [Self::Entry]

Get the FAM entries as mut slice

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§