Perform callbacks in no_std environment

I've got an object with a bunch of registered handlers for callback events. I'm getting the error

error[E0596]: cannot borrow `*callback` as mutable, as it is behind a `&` reference
   --> src/main.rs:106:33
    |
106 | ...                   (callback)();
    |                       ^^^^^^^^^^ `callback` is a `&` reference, so the data it refers to cannot be borrowed as mutable

Why? I've seen examples where this works and I don't see what I'm doing wrong.

I got this type defined ..

    pub enum ObjectCatalog {
        String(String),
        Callback(Box<dyn FnMut()>),
        Mcu(mcu::MCU),
    }
    pub type VectorOfObjectCatalog = Rc<RefCell<Vec<ObjectCatalog>>>;

In the object, there is a variable of type (below) with a vector of objects that want to know about event X.

event_handlers: HashMap<&'a str, shared_types::VectorOfObjectCatalog>,

In my function that sends out events, it looks like this ...

pub fn send_event(&self, event: &'a str) {
            let set = self.event_handlers.get(event);
            match set {
                Some(vec) => {
                    for e in vec.borrow().iter() {
                        match e {
                            shared_types::ObjectCatalog::Callback(callback) => {
                                (callback)();
                            }
                            _ => {}
                        }
                    }
                }
                None => {}
            }
        }

Looks like you found your answer. Please provide a reference if you cross post on SO or somewhere else.

Yes, the answer is thus (the key was vec.borrow_mut().iter_mut() ) :

        pub fn send_event(&self, event: &'a str) {
            let set = self.event_handlers.get(event);
            match set {
                Some(vec) => {
                    for mut e in vec.borrow_mut().iter_mut() {
                        match e {
                            shared_types::ObjectCatalog::Callback(callback) => {
                                (callback)();
                            }
                            _ => {}
                        }
                    }
                }
                None => {}
            }

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.