dimitarvp

dimitarvp

Rustler: how to return an `usize` variable?

Poking @hansihe and @scrogson if that’s okay. Everybody else’s input is appreciated as well.

I need to return a Rust usize variable and I am trying to do it like so:

let affected = ...; // a function that returns `usize`.
Ok((atoms::ok(), ResourceArc::new(affected)).encode(env))

I am getting this error (and a few more related to it):

error[E0277]: the trait bound `usize: rustler::resource::ResourceTypeProvider` is not satisfied
   --> src/lib.rs:101:63
    |
101 |             Ok(affected) => Ok((atoms::ok(), ResourceArc::new(affected)).encode(env)),
    |                                                               ^^^^^^^^ the trait `rustler::resource::ResourceTypeProvider` is not implemented for `usize`
    |
    = note: required by `rustler::ResourceArc::<T>::new`

I am at rustler 0.21.0.

Can you point me in the right direction?

Marked As Solved

scrogson

scrogson

Phoenix Core Team

As I responded on IRC…resources only work with structs and must be accompanied with the rustler::resource_struct_init! macro (in 0.21) or rustler::resource! (in 0.22).

In order to make this work, you’ll need to make a new type which wraps the usize like so:

struct Affected(usize)

Also Liked

scrogson

scrogson

Phoenix Core Team

Yeah a usize and other primitives don’t really fit the use-case for resources :grinning:. A resource is used to keep some rust data-structure alive in between NIF calls.

dimitarvp

dimitarvp

Curiously enough, I was able to just skip ResourceArc and I get no compilation error now.

Where Next?

Popular in Questions Top

srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
sacepums
Hey guys. I'm new to elixir and im really stocked about it. But I ran into a bit of problem - I need to convert a date sting, for examp...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
Codball
Mix format works fine if run from the cmd. I’ve followed this to facilitate the implementation into VSC which involves downloading an ext...
New

Other popular topics Top

yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

We're in Beta

About us Mission Statement