block

Attribute Macro block 

Source
#[block]
Expand description

Marks a struct as a Block type used within the brec system.

This macro enables code generation for the given struct, including:

  • A companion BlockReferred struct (with signature and CRC metadata)
  • Implementations of required brec traits for serialization, deserialization, and streaming

§Supported Field Types

The struct may contain any combination of the following field types:

Type
u8
u16
u32
u64
u128
i8
i16
i32
i64
i128
f32
f64
bool
[u8; N]

§Enums

You may use custom enum fields in your block if you provide conversions to and from a supported base type. Here’s an example using Level:

pub enum Level {
    Error,
    Warn,
    Info,
    Debug,
}

impl From<&Level> for u8 {
    fn from(value: &Level) -> Self { ... }
}

impl TryFrom<u8> for Level {
    type Error = String;
    fn try_from(value: u8) -> Result<Self, Self::Error> { ... }
}

#[block]
pub struct LogBlock {
    pub level: Level,
    pub message: [u8; 200],
}

§Visibility

The macro inherits the visibility of the original struct.
If the struct is pub, the generated BlockReferred and trait implementations will also be pub.

§Integration with Code Generator

The #[block] macro marks this struct for inclusion in the brec::generate!() macro.
For this to work correctly, the block must be visible at the macro invocation site. Example:

pub use blocks::*;
brec::generate!();

If you cannot import the block directly, you may specify the full module path via the path directive:

#[block(path = mycrate::some_module)]
pub struct ExternalBlock { ... }

Shortcut syntax is also supported:

#[block(mycrate::some_module)]
pub struct ExternalBlock { ... }

Using path works but is not recommended, as it makes the code harder to maintain.

§Optional Attributes

The macro accepts the following optional directives:

  • path = mod::mod
    Specifies the module path for this block type (used if it’s not directly imported).

  • no_crc
    Disables CRC calculation and verification. The CRC field is still included in the block’s binary layout,
    but is filled with zeroes and not checked during read/write operations.