8 releases

0.3.1 Feb 29, 2024
0.3.0 Oct 1, 2022
0.2.2 Sep 29, 2022
0.2.1 Aug 18, 2022
0.1.2 Jul 8, 2022

#1652 in Procedural macros

22 downloads per month
Used in radiation

MIT license

13KB
288 lines

Radiation

Radio receiver absorbs electromagnetic radiation and converts it in some signal. Radio transmitter converts a signal into electromagnetic radiation and emit it.

This crate calls raw bytes as a radiation. It allows 'absorb' the radiation and convert it into Rust type, and vice versa, 'emit' Rust type as a radiation.

Trait Absorb parse bytes and return the typed value. It may fail and return an error.

Trait Emit convert the typed value into raw bytes.

Derive

Attribute tag

At enum. Specifies the type of tag, it may be any type implementing Absorb and Display (for error handling). Default is u16.

At variant. Specifies the value of tag for the variant. If the type of the tag implements Default and Add numeric (e.g. it is meaningful to do tag + 1). Then the attribute can be omitted. Default is 0, 1, 2,...

Attributes custom_absorb and custom_emit

At field. Allows to specify custom function for absorb and emit the field. Useful if the field has foreign type, for example SocketAddr.

Attribute as_str

At field. The implementation will absorb and emit the string and use FromStr and Display on the field to convert.

Example

#[derive(Debug, PartialEq, Eq, Absorb, Emit)]
#[tag(u8)]
enum SomeEnum {
    #[tag(1)]
    A {
        one: u8,
        two: u8,
        // specify a function to parse the field with
        #[custom_absorb(absorb)]
        #[custom_emit(emit)]
        three: u16,
    },
    // use `FromStr` implementation to parse the value from `str`
    B(#[as_str] u16),
    C(u32),
}

See tests.rs for more information and examples.

Limit

Attribute limit overwrite the L argument when parse the field. This attribute provides a type which implements Limit trait.

The trait provides lower and upper limit in bytes. Also, it carry information which limit will be on next field, or in inner structure.

See tests.rs for more information and examples.

Dependencies

~0.4–0.9MB
~20K SLoC