Skip to main content

Crate onc_xdr_derive

Crate onc_xdr_derive 

Source
Expand description

Derive macro for XDR (External Data Representation) encoding.

XDR is the serialisation format every ONC RPC protocol is built on – NFSv2, NFSv3, NFSv4, MOUNT, and the portmapper all encode their arguments and results with it. The format is defined by RFC 4506; the essential rules this macro implements are:

  • Every field is packed in declaration order with no padding between them.
  • Enums (XDR “enum” and discriminated “union”) are tagged by a 4-byte big-endian discriminant, followed by the arm’s payload if it has one.
  • Structs carry no tag of their own – they are just their fields, in order.

Writing Pack and Unpack by hand for the ~200 types in RFC 1813 alone would be error-prone busywork, so #[derive(XdrCodec)] generates them. Types whose wire form does not follow the mechanical rules above (NFSv2’s fixed-width 32-byte file handles, the RPC reply body’s nested unions) still implement the traits by hand in the protocol crate that defines them.

§Discriminant values

Simple enums (all unit variants) use the variant’s own as u32 value, so the discriminant is declared the normal Rust way:

ⓘ
#[derive(XdrCodec, Clone, Copy)]
#[repr(u32)]
enum ftype3 {
    NF3REG = 1,
    NF3DIR = 2,
}

Enums carrying data cannot use as u32, so each variant is tagged with an explicit #[xdr(N)] attribute giving its discriminant:

ⓘ
#[derive(XdrCodec)]
enum post_op_attr {
    #[xdr(1)]
    Some(fattr3),
    #[xdr(0)]
    None,
}

Derive Macros§

XdrCodec
Derives XDR Pack and Unpack for a struct or enum.