Trait repr_offset::ext::ROExtOps

source ·
pub unsafe trait ROExtOps<A>: ROExtAcc {
    // Required methods
    fn f_replace<F>(&mut self, offset: FieldOffset<Self, F, A>, value: F) -> F;
    fn f_swap<F>(&mut self, offset: FieldOffset<Self, F, A>, right: &mut Self);
    fn f_get_copy<F>(&self, offset: FieldOffset<Self, F, A>) -> F
       where F: Copy;
}
Expand description

Extension trait for (mutable) references 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

fn f_replace<F>(&mut self, offset: FieldOffset<Self, F, A>, value: F) -> F

Replaces a field (determined by offset) with value, returning the previous value of the field.

§Example
use repr_offset::{
    for_examples::ReprPacked,
    utils::moved,
    ROExtOps, off,
};

let mut value = ReprPacked {
    a: 3u128,
    b: Some(5u64),
    c: vec![0, 1],
    d: (),
};

assert_eq!(value.f_replace(off!(a), 200), 3);
assert_eq!(moved(value.a), 200);

assert_eq!(value.f_replace(off!(b), None), Some(5));
assert_eq!(moved(value.b), None);

assert_eq!(value.f_replace(off!(c), vec![2, 3]), vec![0, 1]);
assert_eq!(moved(value.c), vec![2, 3]);
source

fn f_swap<F>(&mut self, offset: FieldOffset<Self, F, A>, right: &mut Self)

Swaps a field (determined by offset) with the same field in right.

§Example
use repr_offset::{
    for_examples::ReprC,
    ROExtOps, off,
};

let mut left = ReprC {
    a: 3u128,
    b: Some(5u64),
    c: vec![0, 1],
    d: (),
};
let mut right = ReprC {
    a: 55,
    b: None,
    c: vec![89, 144],
    d: (),
};

left.f_swap(off!(a), &mut right);
assert_eq!(left.a, 55);
assert_eq!(right.a, 3);

left.f_swap(off!(b), &mut right);
assert_eq!(left.b, None);
assert_eq!(right.b, Some(5));

left.f_swap(off!(c), &mut right);
assert_eq!(left.c, vec![89, 144]);
assert_eq!(right.c, vec![0, 1]);
source

fn f_get_copy<F>(&self, offset: FieldOffset<Self, F, A>) -> F
where F: Copy,

Gets a copy of a field (determined by offset). The field is determined by offset.

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

let value = ReprPacked {
    a: 3,
    b: "foo",
    c: 'g',
    d: false,
};

assert_eq!(value.f_get_copy(off!(a)), 3);
assert_eq!(value.f_get_copy(off!(b)), "foo");
assert_eq!(value.f_get_copy(off!(c)), 'g');
assert_eq!(value.f_get_copy(off!(d)), false);

Object Safety§

This trait is not object safe.

Implementors§

source§

impl<S> ROExtOps<Aligned> for S

source§

impl<S> ROExtOps<Unaligned> for S