-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Add CallbackRef that takes ref in argument instead of value #3419
Conversation
Visit the preview URL for this PR (updated for commit 844093c): https://yew-rs-api--pr3419-callback-ref-eocw38vx.web.app (expires Wed, 04 Oct 2023 12:51:40 GMT) 🔥 via Firebase Hosting GitHub Action 🌎 |
Benchmark - SSRYew Master
Pull Request
|
Size Comparison
✅ None of the examples has changed their size significantly. |
impl<IN> $callback<IN> { | ||
/// Creates a "no-op" callback which can be used when it is not suitable to use an | ||
/// `Option<$callback>`. | ||
pub fn noop() -> Self { | ||
Self::from(|_: $in_ty| ()) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe this is actually wrong because creating 2 "noop" callbacks and comparing them for equality will yield false
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is fine as it is trying to compare the address of 2 functions are not the content of the functions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I should have written a test -_-
The PartialEq implementation uses Rc::ptr_eq()
so it's actually comparing that it is the same RC but we create one each time we call noop (or Default).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I made this PR to address this: #3430
What prevents you from doing |
It forces me to provide the lifetime which I have to put in the struct etc... but it's actually not necessary because it's an argument of a function but Rust does not know that |
Ah, I forgot to include the important bit from @kirillsemyonkin lol :
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
Description
It's sometimes useful to pass callback functions to components. The struct
Callback can be used for that but it only accept passing the input argument by
value. It is useful sometimes to pass the input argument by reference instead.
Alternative explanation from @kirillsemyonkin :
Its for doing
Fn(&IN) -> OUT
instead ofFn(IN) -> OUT
since some rust weirdness.Doing Callback<&IN, OUT> says that the type should have a 'a which is wrong because its Fn under the hood.
It would've worked if we were able to write
Callback<&IN> -> OUT
or whatever but I think Rust allows that syntax only forFn
family.Checklist