Macro labview_interop::labview_layout

source ·
macro_rules! labview_layout {
    ($struct:item) => { ... };
}
Expand description

Wrap a struct declaration to have the packing attributes set for exchanging the data with the LabVIEW cluster type.

§64 Bit

For 64 bit this is just the standard C packing and is fully functional as a Rust struct.

§32 Bit

On 32 bit this uses a packed representation.

Because Rust references must be aligned this means you cannot get a reference to an individual item and instead must use std::ptr::read_unaligned or std::ptr::write_unaligned after getting the address with std::ptr::addr_of. The 32 bit access example below shows this.

§Basic Example

use labview_interop::labview_layout;

labview_layout!(
pub struct TestStruct {
    one: u8,
    two: u16,
    three: u32,
}
);

§32 Bit Reference Access

use labview_interop::labview_layout;
use std::ptr::{addr_of, read_unaligned};

labview_layout!(
pub struct TestStruct {
    one: u8,
    two: u16,
    three: u32,
}
);

let value = TestStruct {
    one: 1,
    two: 2,
    three: 3
};

// Not allowed on 32 bit.
//let three_ref = &value.three;
unsafe {
    let three_ptr: *const u32 = addr_of!(value.three);
    let three: u32 = read_unaligned(three_ptr);
}