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

vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
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
lets say i have a sample like a = 20; b = 10; if (a &gt; b) do {:ok, "a"} end if (a &lt; b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
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
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 131117 1222
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
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40165 209
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
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

We're in Beta

About us Mission Statement