in ,

rusterlium / rustler, Hacker News

rusterlium / rustler, Hacker News


                    

        

Documentation|Getting Started|Example

Build Status

Rustler is a library for writing Erlang NIFs in safe Rust code. That means there should be no ways to crash the BEAM (Erlang VM). The library provides facilities for generating the boilerplate for interacting with the BEAM, handles encoding and decoding of Erlang terms, and catches rust panics before they unwind into C.

The library provides functionality for both Erlang and Elixir, however Elixir is favored as of now.

Features:

  • Safety – The code you write in a Rust NIF should never be able to crash the BEAM.
  • Interop – Decoding and encoding rust values ​​into Erlang terms is as easy as a function call.
  • Type composition – Making a Rust struct encodable and decodable to Erlang or Elixir can be done with a single attribute.
  • Resource objects – Enables you to safely pass a reference to a Rust struct into Erlang code. The struct will be automatically dropped when it’s no longer referenced.

Getting started

The easiest way of getting started is therustler elixir library.

  • Add therustler elixir libraryas a dependency of your project.
  • Runmix rustler.newto generate a new NIF in your project. Follow the instructions.
  • If you’re already usingserde, consider usingserde_rustlerto easily encode and decode your data types into and from Elixir terms.

NOTE: If you have previously used Rustler, you need to runmix archive.uninstall rustler_installer.ezto remove it before generating the NIF.

What it looks like

This is the code for a minimal NIF that adds two numbers and returns the result.

userustler::{Encoder, Env, Error, Term};modatoms {     rustler::rustler_atoms!{         atom ok;     } }  rustler::rustler_export_nifs!(     "Elixir.Math",     [        ("add",2, add)    ],     None);fnadd''a>(env: Env' (a)>, args:&[Term'a>]) ->(Result)  ''  (a)>, Error>{     letA:  (i)=args [0].  (decode)  () ?;     letB:  (i)=args [1].  (decode)  () ?;      Ok((atoms::(ok)  (), ab).encode(env)) }

Supported nif_version

Rustler useserlang: system_info (nif_version)to detect the supported NIF version of the Erlang / OTP system for which the NIF is to be compiled. It is possible to restrict the NIF version to an older version if the NIF is to be compiled for an older version of Erlang. For example, if the target NIF version should be2.7(Erlang / OTP 17 .3), this can be defined using an environment variable:

RUSTLER_NIF_VERSION=2.7 mix compile

Community

You can find us in# rustleronfreenodeorthe elixir-lang slack.

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

  

Brave Browser
Read More
Payeer

What do you think?

Leave a Reply

Your email address will not be published. Required fields are marked *

GIPHY App Key not set. Please check settings

Twitter Roasts Trump Over 'Obama Netflix' Tweet – CCN.com, Crypto Coins News

Twitter Roasts Trump Over 'Obama Netflix' Tweet – CCN.com, Crypto Coins News

Why Netflix won the Seinfeld streaming deal, Recode

Why Netflix won the Seinfeld streaming deal, Recode