reth_codecs

Macro validate_bitflag_backwards_compat

source
macro_rules! validate_bitflag_backwards_compat {
    ($type:ty, $expected_unused_bits:expr) => { ... };
}
Available on crate feature test-utils only.
Expand description

Macro to ensure that derived Compact types can be extended with new fields while maintaining backwards compatibility.

Verifies that the unused bits in the bitflag struct remain as expected: Zero or NotZero. For more on bitflag struct: reth_codecs_derive::Compact.

Possible failures:

§1. NotZero -> Zero

This wouldn’t allow new fields to be added in the future. Instead, the new field of T should be Option<TExtension> to allow for new fields. The new user field should be included in TExtension type. Only then, update the test to expect Zero for T and add a new test for TExtension.

Goal:

{
struct T {
    // ... other fields
    ext: Option<TExtension>
}
 
// Use an extension type for new fields:
struct TExtension {
    new_field_b: Option<u8>,
}
 
// Change tests
validate_bitflag_backwards_compat!(T, UnusedBits::Zero);
validate_bitflag_backwards_compat!(TExtension, UnusedBits::NotZero);
}   

§2. Zero -> NotZero

If it becomes NotZero, it would break backwards compatibility, so there is not an action item, and should be handled with care in a case by case scenario.