-
-
Notifications
You must be signed in to change notification settings - Fork 74
Make closures convert to blocks automatically? #838
Copy link
Copy link
Open
Labels
A-block2Affects the `block2` crateAffects the `block2` crateA-frameworkAffects the framework crates and the translator for themAffects the framework crates and the translator for themenhancementNew feature or requestNew feature or requestquestionThere is no such thing as a dumb one!There is no such thing as a dumb one!
Description
Metadata
Metadata
Assignees
Labels
A-block2Affects the `block2` crateAffects the `block2` crateA-frameworkAffects the framework crates and the translator for themAffects the framework crates and the translator for themenhancementNew feature or requestNew feature or requestquestionThere is no such thing as a dumb one!There is no such thing as a dumb one!
When using methods with callbacks, it is common to create a block for the sole purpose of passing it to that callback. For example, in Winit we do:
It's a lot more rare to re-use the block, or choose a different kind of storage (like
global_block!) for it, because the performance of that usually doesn't matter. It might be nice if we could avoid the&RcBlock::new(...), and simply pass the closure to the method?I wouldn't want to just change the signature to
impl Fn(&NSNotification), because that would mean we would always have to allocate a new block even if one isn't needed (and that goes against the spirit of this project).One way to implement this would be to add a trait to convert from
impl Fn(T) -> Rto&Block<dyn Fn(T) -> R>:I'm yet unsure if this is worth it?
It would also conflict with rust-lang/rust#29625, we wouldn't be able to implement
FnforBlockif that becomes possible in the future.