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

Make equality works on noop callbacks of same type #3430

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 31 additions & 4 deletions packages/yew/src/callback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
//! - [Counter](https://github.com/yewstack/yew/tree/master/examples/counter)
//! - [Timer](https://github.com/yewstack/yew/tree/master/examples/timer)

use std::fmt;
use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Rc;
use std::{any, fmt};

use crate::html::ImplicitClone;

Expand Down Expand Up @@ -33,6 +35,8 @@ macro_rules! generate_callback_impls {
}
}

impl<IN, OUT> Eq for $callback<IN, OUT> {}

impl<IN, OUT> fmt::Debug for $callback<IN, OUT> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "$callback<_>")
Expand All @@ -46,15 +50,31 @@ macro_rules! generate_callback_impls {
}
}

impl<IN> $callback<IN> {
impl<IN: 'static> $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| ())
thread_local! {
static NOOP: RefCell<HashMap<any::TypeId, Box<dyn any::Any>>> = RefCell::new(Default::default());
}

Self {
cb: NOOP.with(|noop| {
noop.borrow_mut()
.entry(any::TypeId::of::<$in_ty>())
.or_insert_with(|| {
let func: Rc<dyn Fn($in_ty)> = Rc::new(|_: $in_ty| ());
Box::new(func) as Box<dyn any::Any>
})
.downcast_ref::<Rc<dyn Fn($in_ty)>>()
.unwrap()
.clone()
}),
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there really no other solution other than unwrapping and modifying a HashMap in a RefCell? that seems to me like too much work for a Default::default() value

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the function is doing nothing I think it might be possible to do something with unsafe that would be much faster? But I'm really no expert so I won't write that lol

One alternative would be to wrap the Rc into an Option like this:

pub struct Callback<IN, OUT = ()> {
    /// A callback which can be called multiple times
    pub(crate) cb: Option<Rc<dyn Fn(IN) -> OUT>>,
}

(Or using an enum instead of struct)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But I'm afraid using Option will have a cost at runtime because every call will need to check the option.

}
}

impl<IN> Default for $callback<IN> {
impl<IN: 'static> Default for $callback<IN> {
fn default() -> Self {
Self::noop()
}
Expand Down Expand Up @@ -300,4 +320,11 @@ mod test {
reformed.emit(&mut value).expect("is some");
assert_eq!(value, 45);
}

#[test]
fn test_noop_eq() {
assert_eq!(Callback::<u32>::noop(), Callback::<u32>::noop());
assert_eq!(CallbackRef::<u32>::noop(), CallbackRef::<u32>::noop());
assert_eq!(CallbackRefMut::<u32>::noop(), CallbackRefMut::<u32>::noop());
}
}
Loading