Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deriving borrowed props without a lifetime parameter #447

Closed
AaronC81 opened this issue Jun 15, 2022 · 3 comments
Closed

Deriving borrowed props without a lifetime parameter #447

AaronC81 opened this issue Jun 15, 2022 · 3 comments
Labels
core relating to the core implementation of the virtualdom enhancement New feature or request

Comments

@AaronC81
Copy link

Specific Demand

Some of my components need to take an Arc<RwLock<_>> as a prop:

#[derive(Props)]
struct MyProps {
    something: Arc<RwLock<String>>,
}

This doesn't implement PartialEq, so my understanding is that the props need to be borrowed props rather than owned props (as described here).

However, it seems like the Props derive macro still tries to derive owned props, which causes this compile error:

error[E0369]: binary operation `==` cannot be applied to type `&MyProps`
  --> src/main.rs:46:10
   |
46 | #[derive(Props)]
   |          ^^^^^
   |
   = note: this error originates in the derive macro `Props` (in Nightly builds, run with -Z macro-backtrace for more info)

If I give the struct an unused lifetime parameter instead, then the code works fine:

#[derive(Props)]
struct MyProps<'a> {
    something: Arc<RwLock<String>>,
    phantom: PhantomData<&'a ()>,
}

It would be handy if there was a way to derive borrowed props without needing a lifetime parameter - I couldn't spot one in the docs.

Implement Suggestion

Perhaps this could be a parameter to the #[props] attribute:

#[derive(Props)]
#[props(borrowed)]
struct MyProps {
    something: Arc<RwLock<String>>,
}
@jkelleyrtp
Copy link
Member

You're right - this is a missing feature! I think the Props macro can be updated with an attribute that enables/disables memoization (the requirement for 'static to be PartialEq).

We might call it 'no_memo` or something like that.

@ealmloff ealmloff added the enhancement New feature or request label Jun 29, 2022
@jkelleyrtp jkelleyrtp added the core relating to the core implementation of the virtualdom label Jul 7, 2022
@jkelleyrtp
Copy link
Member

Revisiting this, the typical way would be to implement PartialEq manually:

impl PartialEq for MyProps {
    fn eq(&self, other: &Self) -> bool {
        false
    }
}

I'm not sure if adding a no_memo attribute would still be worth it, especially since it might be useful to do a comparison via pointer?

@jkelleyrtp
Copy link
Member

Closed by #1791 - there's no more lifetimes!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core relating to the core implementation of the virtualdom enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants