repr_offset::ext

Trait ROExtRawOps

Source
pub unsafe trait ROExtRawOps<A>: ROExtRawAcc {
    // Required methods
    unsafe fn f_read_copy<F>(self, offset: FieldOffset<Self::Target, F, A>) -> F
       where F: Copy;
    unsafe fn f_read<F>(self, offset: FieldOffset<Self::Target, F, A>) -> F;
}
Expand description

Extension trait for raw pointers to do generic field operations, where the field is determined by a FieldOffset parameter.

§Safety

This trait must not to be implemented outside the repr_offset crate.

§Alignment

The A type parameter is the Alignment of the field, used to implement methods differently depending on whether the field is Aligned or Unaligned.

Required Methods§

Source

unsafe fn f_read_copy<F>(self, offset: FieldOffset<Self::Target, F, A>) -> F
where F: Copy,

Copies a field (determined by offset) from self.

§Safety

You must ensure these properties about the pointed-to value:

  • The value must be in an allocated object (this includes the stack) allocated at least up to the field (inclusive).

  • The field must be initialized

  • If the passed in offset is a FieldOffset<_, _, Aligned> (because it is for an aligned field), self must be an aligned pointer.

§Example
use repr_offset::{
    for_examples::ReprPacked,
    ROExtRawOps, off,
};

use std::cmp::Ordering;

let mut value = ReprPacked {
    a: 3,
    b: Some(5),
    c: Ordering::Less,
    d: (),
};

let ptr: *const _ = &value;
unsafe {
    assert_eq!(ptr.f_read_copy(off!(a)), 3);
    assert_eq!(ptr.f_read_copy(off!(b)), Some(5));
    assert_eq!(ptr.f_read_copy(off!(c)), Ordering::Less);
}
Source

unsafe fn f_read<F>(self, offset: FieldOffset<Self::Target, F, A>) -> F

Reads a copy of a field (determined by offset) from self, without mutating or moving the field.

§Safety

You must ensure these properties about the pointed-to value:

  • The value must be in an allocated object (this includes the stack) allocated at least up to the field (inclusive).

  • The field must be initialized

  • If the passed in offset is a FieldOffset<_, _, Aligned> (because it is for an aligned field), self must be an aligned pointer.

§Example
use repr_offset::{
    for_examples::ReprPacked,
    ROExtRawOps, off,
};

use std::{cmp::Ordering, mem::ManuallyDrop};

let mut value = ManuallyDrop::new(ReprPacked {
    a: 3,
    b: Some(5),
    c: "hello".to_string(),
    d: vec![0, 1, 2],
});

let ptr: *const ReprPacked<_, _, _, _> = &*value;
unsafe {
    assert_eq!(ptr.f_read(off!(a)), 3);
    assert_eq!(ptr.f_read(off!(b)), Some(5));
    assert_eq!(ptr.f_read(off!(c)), "hello".to_string());
    assert_eq!(ptr.f_read(off!(d)), vec![0, 1, 2]);
}

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.

Implementations on Foreign Types§

Source§

impl<S> ROExtRawOps<Aligned> for *const S

Source§

unsafe fn f_read_copy<F>( self, offset: FieldOffset<Self::Target, F, Aligned>, ) -> F
where F: Copy,

Source§

unsafe fn f_read<F>(self, offset: FieldOffset<Self::Target, F, Aligned>) -> F

Source§

impl<S> ROExtRawOps<Aligned> for *mut S

Source§

unsafe fn f_read_copy<F>( self, offset: FieldOffset<Self::Target, F, Aligned>, ) -> F
where F: Copy,

Source§

unsafe fn f_read<F>(self, offset: FieldOffset<Self::Target, F, Aligned>) -> F

Source§

impl<S> ROExtRawOps<Unaligned> for *const S

Source§

unsafe fn f_read_copy<F>( self, offset: FieldOffset<Self::Target, F, Unaligned>, ) -> F
where F: Copy,

Source§

unsafe fn f_read<F>(self, offset: FieldOffset<Self::Target, F, Unaligned>) -> F

Source§

impl<S> ROExtRawOps<Unaligned> for *mut S

Source§

unsafe fn f_read_copy<F>( self, offset: FieldOffset<Self::Target, F, Unaligned>, ) -> F
where F: Copy,

Source§

unsafe fn f_read<F>(self, offset: FieldOffset<Self::Target, F, Unaligned>) -> F

Implementors§