Skip to main content

WriteXDR

Derive Macro WriteXDR 

Source
#[derive(WriteXDR)]
{
    // Attributes available to this derive:
    #[xdr]
    #[xdrs]
}
Expand description

Derives the WriteXDR trait for a struct or enum.

This macro generates an implementation that serializes the type to XDR format. All field types must implement WriteXDR. It also implements EncodesAs<Self>, so the type can be passed where an EncodesAs bound expects it.

§Attributes

  • #[xdr(into_type = "Type")] - Convert (via Into) a different type before serializing.
  • #[xdr(via = "Type")] - Same as above (also implies from_type).
  • #[xdr(default_variant)] - On an enum variant: mark it as the union’s default arm. The variant must be the trailing variant and must not specify a discriminant, and its first field must be i32, which stores the wire discriminant and is emitted verbatim when encoding.
  • #[xdr(crate = "path::to::xdrs")] - Override the path to the xdrs crate (useful when re-exporting from another crate).

§Limitations

  • No recursive types. XDR encoding is not self-describing; a recursive type will loop at runtime. The macro does not detect or reject recursion. In particular, #[xdr(into_type = "Self")] is an infinite loop — the generated impl converts self into Self (a no-op) and re-enters the same encoder until the stack overflows. The surrogate type must always be a distinct, non-recursive type.
  • All field types must be serializable. Every field (or the surrogate type, when using into_type/via) must independently satisfy WriteXDR. The macro copies the input type’s generic bounds verbatim — it does not infer or propagate trait bounds required by a surrogate type’s own generic parameters. For example, #[xdr(into_type = "VecM<T, 8>")] on a generic Wrapper<T> will not automatically add T: WriteXDR; you must supply the bound yourself.
  • Default variant discriminant is not validated. When a Default(i32, ..) variant is encoded, the writer emits the stored discriminant without checking whether it collides with a concrete variant. Encoding a Default with a known discriminant value will produce bytes that the reader decodes as the concrete variant, not as Default.