Skip to main content
macro_rules! avro_schema 
    (for key:ty => builder:expr) => … ;
    ($builder:expr) => … ;

Helper macro to define a static Avro schema using the builder pattern.

This creates a OnceLock that initializes the schema exactly once and returns a &‘static str reference.

Limitation: one schema per macro call site, not per type

The plain form avro_schema!(builder) expands to a single static at the call site. Rust has no generic statics, so inside a generic function or a generic impl every instantiation shares that one static: the first instantiation to run builds the schema and every other type parameter gets the same string back, even when its builder expression would produce a different schema. Use the plain form only where the schema does not depend on a type parameter.

Where it does, use the keyed form avro_schema!(for T => builder), which caches one schema per T (keyed by TypeId, so T: ‘static). Each distinct schema is built once and its string lives for the rest of the program, the same as with the plain form.

Example

A schema that varies with a type parameter: