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

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

Other popular topics Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New

We're in Beta

About us Mission Statement