dioxus
dioxus copied to clipboard
Deriving borrowed props without a lifetime parameter
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>>,
}
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.
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?
Closed by #1791 - there's no more lifetimes!