Skip to content

making default functions to closures#899

Open
thinkrapido wants to merge 1 commit intonannou-org:masterfrom
i-think-rapido:feature/tr/functions-as-closures
Open

making default functions to closures#899
thinkrapido wants to merge 1 commit intonannou-org:masterfrom
i-think-rapido:feature/tr/functions-as-closures

Conversation

@thinkrapido
Copy link
Copy Markdown

I like to suggest a pull request to make the default functions like ModelFn, UpdateFn, etc... usable as a closure.

Related: #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 thinkrapido force-pushed the feature/tr/functions-as-closures branch from bf3f0ac to 3d7f49e Compare November 28, 2022 13:22
@thinkrapido thinkrapido force-pushed the feature/tr/functions-as-closures branch from 3d7f49e to bc457e0 Compare November 28, 2022 13:25
@thinkrapido
Copy link
Copy Markdown
Author

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

@Woyten
Copy link
Copy Markdown
Contributor

Woyten commented Nov 30, 2022

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:

  • Why are the types Arc<dyn Fn(...)>? Shared Fn closures are a pretty rare thing in Rust. You probably want to use Box<dyn FnMut(...)>.
  • What is the reason to use macros? You could just use regular generics instead e.g. 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<dyn FooFn> = Box::new(foo_fn);
}

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

@abey79
Copy link
Copy Markdown

abey79 commented Feb 21, 2023

@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
Copy link
Copy Markdown
Author

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

@thinkrapido thinkrapido changed the title making default functions a closures making default functions to closures Feb 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants