Benjamin-Philip

Benjamin-Philip

I’m having some trouble just returning ok from rust. The following fails with an Erlang error: :invalid_struct:

mod atoms {
    rustler::atoms! {
        ok
    }
}

#[rustler::nif]
pub fn backend_deallocate(array: ExAf) -> Atom {
    let exaf_array = array.resource.value();

    match exaf_array {
        ExAfArray::U8(a) => drop(a),
        ExAfArray::U16(a) => drop(a),
        ExAfArray::U32(a) => drop(a),
        ExAfArray::U64(a) => drop(a),
        ExAfArray::S16(a) => drop(a),
        ExAfArray::S32(a) => drop(a),
        ExAfArray::S64(a) => drop(a),
        ExAfArray::F16(a) => drop(a),
        ExAfArray::F32(a) => drop(a),
        ExAfArray::F64(a) => drop(a),
        ExAfArray::C64(a) => drop(a),
        ExAfArray::C128(a) => drop(a),
    }

    atoms::ok()
}

How can I return :ok (or any other atom) from Rust?

Showing Posts 1 to 3

mindok

mindok

I don’t really understand what I’m doing with Rustler, but this recipe seems to work (for me):

#[rustler::nif]
pub fn backend_deallocate(array: ExAf) -> Result<Atom> {
   ...

    return Ok(atoms::ok());
}
Benjamin-Philip

Benjamin-Philip OP

Thanks for your reply @mindok!

It turns out the reason I was getting an Erlang Error: :invalid_struct error was because I was passing in an “invalid struct” (i.e. an incorrect input for my NIF) and not because of any issues with the rustler output.

Here’s what (in essence) worked for me:

#[rustler::nif]
pub fn ok() -> Atom {
    atoms::ok()
}
SeokminHong

SeokminHong

You can use rustler::types::atom module to use predefined atoms: nil, ok, error, true, and so on.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
ryanwinchester
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted” Version...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
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
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; 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

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews