In one of my projects, I would like to store a function pointer used as a callback to change the state of a struct. I've tried different things, but always encountered errors.
Consider the following situation (playground):
struct CallbackStruct {
    callback: fn(i32) -> i32,
}
struct MainStruct {
    pub callback_struct: Vec<CallbackStruct>,
    pub intern_state: i32,
}
impl MainStruct {
    pub fn new() -> MainStruct {
        let result = MainStruct {
            callback_struct: Vec::new(),
            intern_state: 0,
        };
        // push a new call back struct
        result.callback_struct.push(CallbackStruct{callback: result.do_stuff});
        
        return result;
    }
    pub fn do_stuff(&mut self, i: i32) -> i32 {
        self.intern_state = i * 2 + 1;
        self.intern_state
    }
}
fn main() {
    let my_struct = MainStruct::new();
}
Here, I'm trying to keep a callback to the MainStruct, that can change it's internal state. This callback will only be stored by other structs owned by this main structure, so I don't think I'm having lifetimes issues - as long as the callback exists, the main struct does as well as it kind of own it.
Now, I'm wondering if having such a callback (with the &mut self reference) isn't a borrow of the main struct, preventing me from having multiple of them, or even keeping them?
In the example, I'm keeping a Vec of the CallbackStruct because I may have different structs all having these kinds of callbacks.
In c/c++, I can go with functions pointers, and I couldn't find a way to store such things in Rust.
How would one implement such a thing?
 
    