Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Callbacks

(Supported by c, cpp, kotlin, and nanobind, queried with #[diplomat::cfg(supports=callbacks)])

Diplomat has limited, experimental support for exposing and working with callbacks. See tracking issue

Functions taking callbacks as parameters take the form

#![allow(unused)]
fn main() {
#[diplomat::bridge]
impl MyType{
    pub fn acceptsCallback(&self, impl Fn())
}
}

or

#![allow(unused)]
fn main() {
impl MyType{
    pub fn acceptsCallback(&self, impl FnMut())
}
}

The callback’s parameters & return types may be limited depending on the target language for now. Additional traits are not supported, however + 'static may be given, which allows for moving the trait object into a Box<dyn Fn()>.

Some backends may support callbacks accepting [mutable] references to opaque types as parameters, but lifetime parsing for these is not yet supported by any backend. Only anonymous lifetimes are allowed as a result, meaning that no such parameter may be guaranteed to live longer than a single invocation of the callback.