c2rs 0.1.2

C struct to Rust struct
Documentation
  • Coverage
  • 50%
    1 out of 2 items documented1 out of 1 items with examples
  • Size
  • Source code size: 14.82 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 306.72 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 5s Average build duration of successful builds.
  • all releases: 5s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • editso/c2rs
    1 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • editso

c2rs

This is a macro that converts the struct of the c language into a rust struct

Crates.io MIT licensed

use

// Cargo.toml

[dependencies]
c2rs = "0.1.2"

Example

fn test(){
    use c2rs::c2rs_def;

    type DWORD = u32;
    const SIZE: usize = 10;

    c2rs_def!(
        struct A{
            DWORD var1;
            DWORD var2;
            union {
                DWORD var4;
                DWORD var5;   
            }var3;
            
            struct {
                u8 var7;
            }var6;

            DWORD array[SIZE];
        };
        
        struct B{
            u8 var1;
        };
        
        // ....
    );
    
    let mut buffer = [1u8; 1024];
    
    unsafe{
        let mut buf = A::from_mut_bytes(buffer.as_mut_ptr());
        let buf = buf.as_mut().unwrap();
        buf.var1 = 10;
        
        assert_eq!(10, buf.var1);
        assert_eq!(10, buffer[0]);
        
        let mut b = B::from_mut_bytes(buffer.as_mut_ptr()).as_mut().unwrap();
        
        assert_eq!(10, b.var1);
    
    }
}