Derive Macro rustler::NifRecord

source ·
#[derive(NifRecord)]
{
    // Attributes available to this derive:
    #[tag]
    #[rustler]
}
Expand description

Derives implementations for the Encoder and Decoder traits which convert between a Rust struct and an Elixir record.

For example, annotate the following struct:

#[derive(Debug, NifRecord)]
#[tag = "record"]
struct AddRecord {
   lhs: i32,
   rhs: i32,
}

Create a value of that type:

let value = AddRecord { lhs: 33, rhs: 21 };

Then the traits Encoder and Decoder are derived automatically for your Rust struct such that value would be encoded into the following elixir value:

{:record, 33, 21}

If you supply the following matching Elixir record definition:

defmodule AddRecord do
  import Record
  defrecord :record, [lhs: 1, rhs: 2]
end

Then you can use record functions such as AddRecord.record/0, AddRecord.record/1, AddRecord.record/2, to work with the encoded data, and to create data that can be decoded back into your Rust struct.