thlorenz / rid

Rust integrated Dart framework providing an easy way to build Flutter apps with Rust.
64 stars 4 forks source link

Question about `&Reply` for `rid::post` #45

Closed SecondFlight closed 2 years ago

SecondFlight commented 2 years ago

I have this helper function:

pub fn rid_reply_all(replies: &Vec<Reply>) {
    replies.iter().for_each(|reply| {
        rid::post(reply);
    });
}

When compiling, I get the following error:

error[E0277]: the trait bound `DartCObject: From<&model::store::Reply>` is not satisfied
   --> src\util\rid_reply_all.rs:24:9
    |
24  |         rid::post(reply);
    |         ^^^^^^^^^ the trait `From<&model::store::Reply>` is not implemented for `DartCObject`
    | 
   ::: (...)\rid\rid-ffi\src\post.rs:69:25
    |
69  | pub fn post(reply: impl ::allo_isolate::IntoDart) {
    |                         ------------------------ required by this bound in `post`
    |
    = note: required because of the requirements on the impl of `Into<DartCObject>` for `&model::store::Reply`
note: required because of the requirements on the impl of `IntoDart` for `&model::store::Reply`
   --> src\model\store.rs:117:10
    |
117 | pub enum Reply {
    |          ^^^^

This solves the issue, but it feels like a code smell:

pub fn rid_reply_all(replies: &Vec<Reply>) {
    replies.iter().for_each(|reply| {
        let reply = reply.clone();
        rid::post(reply);
    });
}

Is there a better way to do this?

thlorenz commented 2 years ago

That is indeed very odd. I'm not fully understanding why the first version doesn't work since clone would create an owned version of each reply. Have you tried something along the lines of replies.iter().cloned().for_each(...?

In general it seems like you're just fighting the Rust compiler here in odd ways.

SecondFlight commented 2 years ago

Sorry about that, I pasted the wrong error. I've updated the post above.

thlorenz commented 2 years ago

Yeah the error seemed odd to me, esp. this part as it doesn't have .clone and looks like it's passing a reference:

 rid::post(reply);
    |         ^^^^^^^^^ the trait `From<&model::store::Reply>` is not implemented for `DartCObject`
thlorenz commented 2 years ago

And yes you'll have to clone, there is no way around that. This will be sent to dart and we cannot risk it going out of scope before (no smell there).

SecondFlight commented 2 years ago

Awesome, I'll continue this direction then. Thanks for the response!