Place a member of the function type in a struct or in an actual function

I want to do something like this:

'''Struct MYSTRUCT{ func: fn}"'


"'fn my_fn( func: fn){}"

Knowing that it is possible that there is not a specific number of parameters for the func function, and the type of parametes in the func function can be any type, and the func function can return a value of any type or not return any value.

How are you planning to call it if you don't know how many arguments are there or what their types are? What exactly are you trying to implement?

It works in nightly (you need a working Tuple trait bound), but the function is not directly usable.

struct MyStruct<T: Fn<S, Output = U>, S: std::marker::Tuple, U> {
    func: T,
    func_args: core::marker::PhantomData<S>,
    func_ret: core::marker::PhantomData<U>,

fn my_fn<T: Fn<S, Output = U>, S: std::marker::Tuple, U>(func: T) -> ! {
    // what to do with func?

fn test_func(a: i32, b:f64) -> String {
    format!("{} {}", a, b)

fn test() -> MyStruct<fn(i32, f64) -> String, (i32, f64), String> {
        func: test_func, 
        func_args: core::marker::PhantomData, 
        func_ret: core::marker::PhantomData

This is a topic related to gui. I thought of this as I am trying to understand how a user-defined function can be executed when an event related to one of the GUI elements occurs, such as pressing the mouse on a button element.

Like what happens when you press the button element in fltk

callback in the example you shown works with a specific closure type: FnMut(&mut Self), no magic is needed here. If you are designing something on your own - types are also limited. There's limited amount of values a button can generate: a reference to itself, its id, or even (). And a button itself won't use any value returned so callback should produce () as well.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.