nannou-org / nannou

A Creative Coding Framework for Rust.
https://nannou.cc/
6.05k stars 307 forks source link

making default functions to closures #899

Open thinkrapido opened 2 years ago

thinkrapido commented 2 years ago

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

fn main() {
    nannou::sketch(view!(|app: &App, frame: Frame| {
        //...
    })).run()
}

and to respective functions.

Also read, how it is meant: https://medium.com/@romeo-disca/smart-pointers-in-rust-158046006f15

thinkrapido commented 2 years ago

all the examples need to be improved due to this pull request

Woyten commented 1 year ago

Using closures instead of fns 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:

impl<F: FnMut()> FooFn for F { fn foo(&mut self) { self() } }

fn consumes_foo(foo_fn: impl FooFn) { let boxed: Box = Box::new(foo_fn); }

fn calls_foo() { consumes_foo(|| {}) }

abey79 commented 1 year ago

@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.

thinkrapido commented 1 year ago

@abey79 , no I didn't. For me the pull request solved to problem.