Trait vmm_sys_util::fam::FamStruct [−][src]
pub unsafe trait FamStruct {
type Entry: PartialEq + Copy;
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.
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>;
Associated Types
Required methods
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.
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.
Get max allowed FAM length
This depends on each structure. For example a structure representing the cpuid can contain at most 80 entries.
fn as_mut_slice(&mut self) -> &mut [Self::Entry]
fn as_mut_slice(&mut self) -> &mut [Self::Entry]
Get the FAM entries as mut slice