Skip to main content

Crate basalt_derive

Crate basalt_derive 

Source
Expand description

Proc-macro crate for Minecraft protocol serialization.

Provides two ways to generate Encode, Decode, and EncodedSize impls:

§#[packet(id = N)] — for protocol packets

Attribute macro that generates all three trait impls plus a PACKET_ID constant. Use this on every packet struct:

#[derive(Debug, Clone, PartialEq)]
#[packet(id = 0x00)]
pub struct StatusRequest;

§#[derive(Encode, Decode, EncodedSize)] — for non-packet types

Standard derive macros for inner data structures, enums, and other types that need serialization but aren’t protocol packets:

#[derive(Debug, Encode, Decode, EncodedSize)]
pub struct SomeInnerData {
    pub value: i32,
}

§Field attributes

  • #[field(varint)] — encode i32/i64 as VarInt/VarLong
  • #[field(length = "varint")] — VarInt length prefix for Vec
  • #[field(optional)] — boolean-prefixed Option
  • #[field(rest)] — consume remaining bytes (last field only, must be Vec)

§Enum attributes

  • #[variant(id = N)] — explicit discriminant (default: sequential from 0)

Attribute Macros§

packet
Attribute macro for protocol packet structs.

Derive Macros§

Decode
Derives the Decode trait for a struct or enum.
Encode
Derives the Encode trait for a struct or enum.
EncodedSize
Derives the EncodedSize trait for a struct or enum.