tomazzlender

tomazzlender

I tried out Rustler and it is amazing when I need to define NIFs at the compile time.

I’m experimenting with a use case where it would be useful to define a NIF at a run time.

Imagine being in an IEx shell where you are able to define a function at the runtime and use it right after.

I wonder how I could do something similar for nifs. Let’s imagine that I have the following Rust code that I want to compile and load at the runtime of BEAM

  fn add(a: i64, b: i64) -> i64 {
    a + b
  }

and use it like

defmodule AdditionWithRust do
  def add(a, b), do
     add_function = """
     fn add(a: i64, b: i64) -> i64 {
       a + b
     }
    """

    RustOnRuntime.apply(add_function, a, b)
end
"""

Where RustOnRuntime is a fictional module that does some magic.

I would copy paste this to IEx and I would be able to use the function normally.

It’s OK to assume I have a fixed number of Rust libraries that are defined at the compile time, and only those library functions can be used, no new can be loaded.

I suppose this is quite a complex question. I suppose that at the bottom of it there is probably a question if it is even possible to compile and load erlang nifs (of any kind not just Rust) at the run time..

Any pointers what things to consider or where to start would be much appreciated.

Showing Posts 1 to 3

arcadian

arcadian

I’m not a Rust fan - prefer C++ - but I’ll tell you what I know. If you update the shared library, and the NIF supports reloading (see ERL_NIF_INIT), then there is no reason that you shouldn’t be able to update your NIF whenever you want with new functionality. Updating the shared library for my setup is as easy as just running gmake in the right directory.

Imagine being in an IEx shell where you are able to define a function at the runtime and use it right after.

Yeah, this is what I should be doing, but I haven’t set the appropriate reload functions. Still, though my development cycle is pretty close - edit the c++ code in one window, then restart iex in another (which compiles my nif via gmake) and then run my test at the iex command line.

BTW I proposed a dedicated Nif subforum here but couldn’t get any momentum so my plan is to maintain some pages of Nif-related resources somewhere else, either github or my own site.

tomazzlender

tomazzlender OP

That’s a great pointer @arcadian, thanks! I’ll look into ERL_NIF_INIT. Your answer also helped me find the Rust equivalent.

lkuty

lkuty

That would be great :slight_smile:

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
rahultumpala
Hello, I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews