medea_jason/platform/
callback.rs

1//! Functionality for calling platform callbacks.
2
3use std::cell::RefCell;
4
5use super::Function;
6
7/// Wrapper for a single argument callback function.
8#[derive(Debug)]
9pub struct Callback<A>(pub RefCell<Option<Function<A>>>);
10
11impl<A> Callback<A> {
12    /// Sets the inner [`Function`].
13    pub fn set_func(&self, f: Function<A>) {
14        drop(self.0.borrow_mut().replace(f));
15    }
16
17    /// Indicates whether this [`Callback`] is set.
18    #[must_use]
19    pub fn is_set(&self) -> bool {
20        self.0.borrow().as_ref().is_some()
21    }
22}
23
24impl Callback<()> {
25    /// Invokes the underlying [`Function`] (if any) passing no arguments to it.
26    pub fn call0(&self) {
27        if let Some(f) = self.0.borrow().as_ref() {
28            f.call0();
29        }
30    }
31}
32
33// Implemented manually to omit redundant `A: Default` trait bound, imposed by
34// `#[derive(Default)]`.
35impl<A> Default for Callback<A> {
36    fn default() -> Self {
37        Self(RefCell::new(None))
38    }
39}