cuprate_epee_encoding

Macro epee_object

Source
macro_rules! epee_object {
    (
        @internal_try_right_then_left
        $a:expr, $b:expr
    ) => { ... };
    (
        @internal_try_right_then_left
        $a:expr,
    ) => { ... };
    (
        @internal_field_name
        $field: tt, $alt_name: tt
    ) => { ... };
    (
        @internal_field_name
        $field: ident,
    ) => { ... };
    (
        @internal_field_type
        $ty:ty, $ty_as:ty
    ) => { ... };
    (
        @internal_field_type
        $ty:ty,
    ) => { ... };
    (
        $obj:ident,
        $($field: ident $(($alt_name: literal))?: $ty:ty $(as $ty_as:ty )? $(= $default:expr)?  $(=> $read_fn:expr, $write_fn:expr, $should_write_fn:expr)?, )*
        $(!flatten: $flat_field: ident: $flat_ty:ty ,)*

    ) => { ... };
}
Expand description

Macro to derive EpeeObject for structs.

§Basic Usage:

// mod visibility is here because of Rust visibility weirdness, you shouldn't need this unless defined in a function.
// see: <https://github.com/rust-lang/rust/issues/64079>
mod visibility {

    use cuprate_epee_encoding::epee_object;

    struct Example {
        a: u8
    }    

    epee_object!(
        Example,
        a: u8,
    );
}

§Advanced Usage:

// mod visibility is here because of Rust visibility weirdness, you shouldn't need this unless defined in a function.
// see: <https://github.com/rust-lang/rust/issues/64079>
mod visibility {

    use cuprate_epee_encoding::epee_object;

    struct Example {
        a: u8,
        b: u8,
        c: u8,
        d: u8,
        e_f: Example2
    }

    struct Example2 {
        e: u8
    }

    epee_object!(
        Example2,
        e: u8,
    );

    epee_object!(
        Example,
        // `("ALT-NAME")` changes the name of the field in the encoded data.
        a("A"): u8,
        // `= VALUE` sets a default value that this field will be set to if not in the data
        // when encoding this field will be skipped if equal to the default.
        b: u8 = 0,
        // `as ALT-TYPE` encodes the data using the alt type, the alt type must impl Into<Type> and From<&Type>
        c: u8 as u8,
        // `=> read_fn, write_fn, should_write_fn,` allows you to specify alt field encoding functions.
        //  for the required args see the default functions, which are used here:
        d: u8 => cuprate_epee_encoding::read_epee_value, cuprate_epee_encoding::write_field, <u8 as cuprate_epee_encoding::EpeeValue>::should_write,
        // `!flatten` can be used on fields which are epee objects, and it flattens the fields of that object into this object.
        // So for this example `e_f` will not appear in the data but e will.
        // You can't use the other options with this.
        !flatten: e_f: Example2,
    );
}