Macro rustler::atoms [−][src]
macro_rules! atoms { { $( $( #[$attr:meta] )* $name:ident $( = $str:expr )? ),*$(,)? } => { ... }; { @internal_make_atom($env:ident, $name:ident) } => { ... }; { @internal_make_atom($env:ident, $name:ident = $str:expr) } => { ... }; }
Expand description
Macro for defining Rust functions that return Erlang atoms.
To use this macro, you must also import the lazy_static
crate.
For example, this code:
#[macro_use] extern crate rustler; mod my_atoms { rustler::atoms! { jpeg, } }
defines a public function my_atoms::jpeg()
that returns the Atom
for the jpeg
atom.
Multiple atoms can be defined. Each one can have its own doc comment and other attributes.
rustler::atoms! { /// The `jpeg` atom. jpeg, /// The `png` atom. png, #[allow(non_snake_case)] WebP, }
When you need an atom that’s not a legal Rust function name, write NAME = "ATOM"
, like
this:
rustler::atoms! { /// The `mod` atom. The function isn't called `mod` because that's /// a Rust keyword. mod_atom = "mod", /// The atom `'hello world'`. Obviously this function can't be /// called `hello world` because there's a space in it. hello_world = "hello world", }
Performance
These functions are faster than get_atom
and get_atom_init
. The first time you call one, it
creates atoms for all its sibling functions and caches them, so that all later calls are fast.
The only overhead is checking that the atoms have been created (an atomic integer read).