Open thinkrapido opened 2 years ago
all the examples need to be improved due to this pull request
Using closures instead of fn
s would make things easier, indeed. In my personal project, for example, I am using thread_local!
to pass a pre-computed value to the fn
. However, there are certain things in your PR I do not understand:
Arc<dyn Fn(...)>
? Shared Fn
closures are a pretty rare thing in Rust. You probably want to use Box<dyn FnMut(...)>
.fn foo(foo_fn: impl FnMut()) { let boxed = Box::new(foo_fn) }
. If you want some kind of name aliasing you could do the following:
trait FooFn {
fn foo(&mut self);
}
impl<F: FnMut()> FooFn for F { fn foo(&mut self) { self() } }
fn consumes_foo(foo_fn: impl FooFn) {
let boxed: Box
fn calls_foo() { consumes_foo(|| {}) }
@thinkrapido Did you make any progresses with this PR? I just ran into the issue of passing state from outside nannou::app()
, which it would solve.
@abey79 , no I didn't. For me the pull request solved to problem.
I like to suggest a pull request to make the default functions like ModelFn, UpdateFn, etc... usable as a closure.
Related: https://github.com/nannou-org/nannou/issues/793
It is a very easy fix and we gain much more flexibility in developing complex programs.
A call would be via an macro
and to respective functions.
Also read, how it is meant: https://medium.com/@romeo-disca/smart-pointers-in-rust-158046006f15