#memory #reference #external #proc-macro #struct #getter #methods

no-std raw_struct

raw_struct is a Rust procedural macro for easily declaring C-style structs that reference local or external memory, based on your memory implementation. It generates appropiate getter methods for easy access.

3 releases

0.1.3 Oct 18, 2024
0.1.2 Oct 16, 2024
0.1.0 Oct 16, 2024

#1493 in Rust patterns

Download history 333/week @ 2024-10-12 94/week @ 2024-10-19 7/week @ 2024-10-26 20/week @ 2024-11-02 30/week @ 2024-11-09 14/week @ 2024-11-16 2/week @ 2024-11-23

66 downloads per month

Custom license

22KB
589 lines

raw_struct   Latest Version License: GPL v3 GitHub build status

raw_struct is a Rust procedural macro for easily declaring C-style structs that reference local or external memory, based on your memory implementation. It generates appropiate getter methods for easy access. This crate has support for no_std environments.

Usage

To use raw_struct, simply define a struct with the raw_struct attribute as following:

#[raw_struct(size = 0x10)]
struct MyStruct {
    #[field(offset = 0x00)]
    pub field_a: u32,

    #[field(offset = 0x04)]
    pub field_b: u32,

    #[field(offset = 0x08, getter = "get_field_c")]
    pub field_c: [u8; 0x8],
}

To reference the declared struct in memory you can ether do so by Reference or Copy:

let memory = [0u8; 0x10];
{
    let object = Reference::<dyn MyStruct>::new(0x00, Arc::new(memory.clone()));
    println!("field_a = {}", object.field_a()?);
    println!("field_b = {}", object.field_b()?);
}

{
    let object = Copy::<dyn MyStruct>::new(memory);
    println!("field_a = {}", object.field_a()?);
    println!("field_b = {}", object.field_b()?);
}

Examples

Examples can be found within the examples directory of this repository. These examples demonstrate how to use raw_struct in various contexts.

To run the examples, clone the repository and use the following command:

cargo run --bin <example_name>

Dependencies

~1.5MB
~37K SLoC