Function repr_offset::utils::moved

source ·
pub const fn moved<T>(val: T) -> T
Expand description

A helper function to force a variable to move (copy if it’s a Copy type).

Example

use repr_offset::utils::moved;

#[repr(C, packed)]
struct Packed{
    foo: usize,
    bar: u64,
}

let this = Packed{ foo: 21, bar: 34 };

assert_eq!( moved(this.foo), 21 );
assert_eq!( moved(this.bar), 34 );

// The code below causes undefined behavior because:
// -`assert_eq` borrows the operands implicitly.
// - Fields of `#[repr(C, packed)]` structs create unaligned references when borrowed.
// - Unaligned references are undefined behavior.
//
// unsafe{
//      assert_eq!( this.foo, 21 );
//      assert_eq!( this.bar, 34 );
// }