Trait repr_offset::ext::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§
sourceunsafe fn f_read_copy<F>(self, offset: FieldOffset<Self::Target, F, A>) -> Fwhere
F: Copy,
unsafe fn f_read_copy<F>(self, offset: FieldOffset<Self::Target, F, A>) -> Fwhere
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 aFieldOffset<_, _, 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);
}
sourceunsafe fn f_read<F>(self, offset: FieldOffset<Self::Target, F, A>) -> F
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 aFieldOffset<_, _, 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]);
}