Derive Macro rustler::NifUnitEnum

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

Implementation of the NifUnitEnum macro that lets the user annotate an enum with a unit type that will generate elixir atoms when encoded

#[derive(NifUnitEnum)]
enum UnitEnum {
   FooBar,
   Baz,
}

An example usage in elixir would look like the following.

test "unit enum transcoder" do
  assert :foo_bar == RustlerTest.unit_enum_echo(:foo_bar)
  assert :baz == RustlerTest.unit_enum_echo(:baz)
  assert :invalid_variant == RustlerTest.unit_enum_echo(:somethingelse)
end

Note that the :invalid_variant atom is returned if the user tries to encode something that isn’t in the Rust enum.