It is possible to use Rustler to write Erlang NIFs in a safe Rust environment. As a result, it is safe to assume that the BEAM cannot be compromised in any manner (Erlang VM). The library handles the encoding and decoding of Erlang words and catches rust panics before they unwind into C. It also offers utilities for producing boilerplate for communicating with the BEAM.
Erlang and Elixir may both use the library, however Elixir is the preferred language for the time being.
A Rust NIF’s code should never be able to cause the BEAM to fail. As simple as calling a function, interop allows rust values to be decoded and encoded into Erlang terms. A single property may be used to make a Rust struct encodable and decodable to Erlang or Elixir. Rust struct references may be securely sent into Erlang code via resource objects. When the struct is no longer referenced, it will be automatically disposed of.
To write NIFs in Rust, the v0.22 edition of Rustler provides a significantly clearer syntax, making it easier to do so. Encoding and decoding Rust values, catching Rust panics before they unwind to C and preventing the BEAM from crashing from a Rust NIF are all handled by the library.
During the development of a micro-library offering Base64 encoding and decoding, aptly titled base64, one of my first ventures into Rust-implemented NIFs occurred. It serves no use because Elixir already has that feature built in, but I was eager to get started with something basic. A bonus was that the NIF version and Elixir implementation in the Base module could readily be compared for performance.
NIFs are encoded and decoded using the rust-base64 library, which has two functions: encode/2 and decode/2. Let’s take a closer look at how everything works.
We’ll need a fresh mix project with rustler set up as a dependencies before we can get going.
No one is putting up any objection to using Rust in Elixir/Erlang because the library handles all of the boilerplate and translation complications.
Rust NIFs are encoded and decoded using the rust-base64 library, which has two functions: encode/2 and decode/2. Encoding and decoding Rust values, catching Rust panics before they unwind to C and preventing the BEAM from crashing from a Rust NIF are all handled by the library.