Derive Macro Linearize

Source
#[derive(Linearize)]
{
    // Attributes available to this derive:
    #[linearize]
}
Expand description

A proc macro to derive the Linearize trait.

This macro can be used to derive the Linearize trait for structs and enums.

The structure of these types can be arbitrary except that all contained fields must also implement the Linearize trait.

§Using different crate names

If you use the linearize crate under a name other than linearize, you can use the crate attribute to have the proc macro reference the correct crate. For example, if you import the linearize crate like this:

linearize-0_1 = { package = "linearize", version = "0.1" }

Then you can use this attribute as follows:

#[derive(Linearize)]
#[linearize(crate = linearize_0_1)]
struct S;

If you import the linearize crate under a name other than linearize or use the crate attribute, you must ensure that these two names are in sync. Otherwise the macro might not uphold the invariants of the Linearize trait.

§Implementing const functions

If you want to use the forms of the static_map and static_copy_map macros that work in constants and statics, you must enable the const attribute:

#[derive(Linearize)]
#[linearize(const)]
struct S;

In this case, your type must only contain fields that also enabled this attribute. In particular, you cannot use any of the standard types u8, bool, etc.

§Performance

If the type is a C-style enum with default discriminants, the derived functions will be compiled to a jump table in debug mode and will be completely optimized away in release mode.

If the type contains fields, the generated code will still be reasonably efficient.

§Limitations

While this macro fully supports types with generics, the generated output will not compile. This is due to limitations of the rust type system. If a future version of the rust compiler lifts these limitations, this macro will automatically start working for generic types.