RustQ.Rustler (rustq v0.4.0)

Copy Markdown View Source

Builders for common Rustler NIF code.

RustQ.Rustler returns RustQ.Rust fragments that can be spliced into real Rust templates or declared in rustq.exs with rust_items/2.

Use the safe helpers by default. Helpers that work with raw rustler::wrapper::NIF_TERM are named with the nif_term_ prefix so unsafe code stays explicit.

Summary

Functions

Builds a decoder from Rustler atoms into a Rust enum/value type.

Builds a function that dispatches on an atom expression.

Builds a rustler::atoms! block.

Builds cached atom helper functions backed by OnceLock<Atom>.

Builds the rustler::init! declaration for an Elixir module.

Builds a Rustler NIF function declaration.

Builds a single exported Rustler NIF function.

Builds exported Rustler NIF functions that delegate to implementation functions.

Builds a Rust struct deriving NifStruct for an Elixir struct module.

Builds raw NIF_TERM map/struct helpers.

Builds an options struct plus decoder function for keyword/optionss.

Builds keyword/options helper functions over &[(Atom, Term<'a>)].

Builds a Rustler resource struct and resource registration helpers.

Returns the ResourceArc<...> Rust type for a resource.

Builds a decoder function for a Rustler resource.

Builds a Rustler resource plus an Elixir handle decoder.

Builds resource initialization code for a Rustler module.

Builds a type alias for a resource's ResourceArc type.

Returns Rust items for a RustQ.Rustler.Schema schema.

Builds a Rust enum that decodes tagged Elixir structs.

Builds safe Term<'a> map/struct helpers.

Builds a decoder function from a Rustler map term into a Rust struct.

Builds common map/term helper functions used by generated decoders.

Functions

atom_decoder(name, opts)

@spec atom_decoder(
  atom() | String.t(),
  keyword()
) :: RustQ.Rust.Fragment.t()

Builds a decoder from Rustler atoms into a Rust enum/value type.

RustQ.Rustler.atom_decoder(:decode_blend_mode,
  returns: :BlendMode,
  cases: [src_over: "BlendMode::SrcOver", multiply: "BlendMode::Multiply"]
)

The generated function returns NifResult<returns> by default. Pass :result to override the full return type, :input to override the input type, :atoms to override the atoms module, and :unknown to override the fallback branch.

atom_dispatch(name, opts)

@spec atom_dispatch(
  atom() | String.t(),
  keyword()
) :: RustQ.Rust.Function.t()

Builds a function that dispatches on an atom expression.

RustQ.Rustler.atom_dispatch(:draw_command,
  args: [surface: "&mut Surface", command: "Term<'a>"],
  on: "command.map_get(atoms::op())?.decode::<Atom>()?",
  cases: [rect: "draw_rect(surface, command)"],
  unknown: "Ok(())"
)

This is intentionally generic: use it for command dispatch, tagged map dispatch, AST node dispatch, or any other Rustler atom switch.

atoms(atoms, opts \\ [])

@spec atoms(
  [atom() | String.t() | {atom() | String.t(), String.t()}],
  keyword()
) :: RustQ.Rust.Fragment.t()

Builds a rustler::atoms! block.

cached_atoms(atoms, opts \\ [])

@spec cached_atoms(
  [atom() | String.t() | {atom() | String.t(), String.t()}],
  keyword()
) :: [RustQ.Rust.Fragment.t()]

Builds cached atom helper functions backed by OnceLock<Atom>.

init(module)

@spec init(module() | String.t()) :: RustQ.Rust.Fragment.t()

Builds the rustler::init! declaration for an Elixir module.

nif(name, opts \\ [])

@spec nif(
  atom() | String.t(),
  keyword()
) :: RustQ.Rust.Function.t()

Builds a Rustler NIF function declaration.

Options include :args, :returns, :body, :vis, and :schedule.

nif_export(name, opts)

@spec nif_export(
  atom() | String.t(),
  keyword()
) :: RustQ.Rust.Function.t()

Builds a single exported Rustler NIF function.

nif_exports(specs)

@spec nif_exports([{atom() | String.t(), keyword()}]) :: [RustQ.Rust.Function.t()]

Builds exported Rustler NIF functions that delegate to implementation functions.

This is useful when the exported Rustler surface is repetitive but the implementation bodies should stay handwritten.

RustQ.Rustler.nif_exports([
  render_png: [
    args: [env: "Env<'a>", batch: "Term<'a>"],
    returns: "NifResult<Term<'a>>",
    lifetime: :a,
    schedule: :dirty_cpu
  ]
])

Generates a #[rustler::nif] fn render_png(...) { render_png_impl(...) } export. Pass :impl to override the implementation function name.

nif_struct(name, module, opts \\ [])

@spec nif_struct(atom() | String.t(), module() | String.t(), keyword()) ::
  RustQ.Rust.Fragment.t()

Builds a Rust struct deriving NifStruct for an Elixir struct module.

nif_term_builders(opts \\ [])

@spec nif_term_builders(keyword()) :: [RustQ.Rust.Fragment.t()]

Builds raw NIF_TERM map/struct helpers.

These helpers generate unsafe Rust and are intended for low-level Rustler code where raw terms are required. Prefer term_builders/1 when Term<'a> is available.

opts_decoder(name, opts)

@spec opts_decoder(
  atom() | String.t(),
  keyword()
) :: [RustQ.Rust.Fragment.t()]

Builds an options struct plus decoder function for keyword/optionss.

opts_helpers(opts \\ [])

@spec opts_helpers(keyword()) :: [RustQ.Rust.Fragment.t()]

Builds keyword/options helper functions over &[(Atom, Term<'a>)].

By default this includes decode_opts, opt_term, opt_f32, opt_f32_option, opt_f32_default, opt_bool_option, and opt_atom_option. Pass :include or :exclude to select helpers.

RustQ.Rustler.opts_helpers(include: [:decode_opts, :opt_term])

decode_opts extracts from atoms::opts() by default. Pass :key to use a different map key expression.

resource(name, opts \\ [])

@spec resource(
  atom() | String.t(),
  keyword()
) :: [RustQ.Rust.Fragment.t()]

Builds a Rustler resource struct and resource registration helpers.

resource_arc(name)

@spec resource_arc(atom() | String.t()) :: String.t()

Returns the ResourceArc<...> Rust type for a resource.

resource_decoder(name, opts \\ [])

@spec resource_decoder(
  atom() | String.t(),
  keyword()
) :: RustQ.Rust.Fragment.t()

Builds a decoder function for a Rustler resource.

resource_handle(name, opts \\ [])

@spec resource_handle(
  atom() | String.t(),
  keyword()
) :: [RustQ.Rust.Fragment.t()]

Builds a Rustler resource plus an Elixir handle decoder.

The generated decoder extracts a ResourceArc<...> from a field on an Elixir-facing handle struct or map. The field defaults to "ref".

RustQ.Rustler.resource_handle(:EncodedImage,
  fields: [bytes: "Vec<u8>"],
  handle_field: "ref"
)

Pass :decoder to override the decoder function name.

resource_init(name)

@spec resource_init(atom() | String.t()) :: RustQ.Rust.Fragment.t()

Builds resource initialization code for a Rustler module.

resource_type(name, opts \\ [])

@spec resource_type(
  atom() | String.t(),
  keyword()
) :: RustQ.Rust.TypeAlias.t()

Builds a type alias for a resource's ResourceArc type.

schema_items(schema)

@spec schema_items(RustQ.Rustler.Schema.t()) :: [RustQ.Rust.Fragment.t()]

Returns Rust items for a RustQ.Rustler.Schema schema.

tagged_enum(name, opts)

@spec tagged_enum(
  atom() | String.t(),
  keyword()
) :: [RustQ.Rust.Fragment.t()]

Builds a Rust enum that decodes tagged Elixir structs.

term_builders(opts \\ [])

@spec term_builders(keyword()) :: [RustQ.Rust.Fragment.t()]

Builds safe Term<'a> map/struct helpers.

term_decoder(name, opts)

@spec term_decoder(
  atom() | String.t(),
  keyword()
) :: [RustQ.Rust.Fragment.t()]

Builds a decoder function from a Rustler map term into a Rust struct.

term_helpers(opts \\ [])

@spec term_helpers(keyword()) :: [RustQ.Rust.Fragment.t()]

Builds common map/term helper functions used by generated decoders.