Attribute Macro safer_ffi::derive_ReprC

source · []
#[derive_ReprC]
Expand description

Safely implement ReprC for a #[repr(C)] struct when all its fields are ReprC.

Examples

Simple struct

use ::safer_ffi::prelude::*;

#[derive_ReprC]
#[repr(C)]
struct Instant {
    seconds: u64,
    nanos: u32,
}
  • corresponding to the following C definition:

    typedef struct {
        uint64_t seconds;
        uint32_t nanos;
    } Instant_t;
    

Field-less enum

use ::safer_ffi::prelude::*;

#[derive_ReprC]
#[repr(u8)]
enum Status {
    Ok = 0,
    Busy,
    NotInTheMood,
    OnStrike,
    OhNo,
}
  • corresponding to the following C definition:

    typedef uint8_t Status_t; enum {
        STATUS_OK = 0,
        STATUS_BUSY,
        STATUS_NOT_IN_THE_MOOD,
        STATUS_ON_STRIKE,
        STATUS_OH_NO,
    }
    

Generic struct

In that case, it is required that the struct’s generic types carry a : ReprC bound each:

use ::safer_ffi::prelude::*;

#[derive_ReprC]
#[repr(C)]
struct Point<Coordinate : ReprC> {
    x: Coordinate,
    y: Coordinate,
}

Each monomorphization leads to its own C definition:

  • Point<i32>

    typedef struct {
        int32_t x;
        int32_t y;
    } Point_int32_t;
    
  • Point<f64>

    typedef struct {
        double x;
        double y;
    } Point_double_t;