Rust callback idiom

Posted 金庆

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Rust callback idiom相关的知识,希望对你有一定的参考价值。

Rust callback idiom

Code is from: https://stackoverflow.com/questions/41081240/idiomatic-callbacks-in-rust
and
https://morestina.net/blog/793/closure-lifetimes-in-rust

struct Processor<'a> {
    callback: Box<dyn FnMut() + 'a>,
}

impl<'a> Processor<'a> {
    fn new() -> Processor<'a> {
        Processor {
            callback: Box::new(|| ()),
        }
    }

    fn set_callback(&mut self, c: impl FnMut() + 'a) {
        self.callback = Box::new(c);
    }

    fn process_events(&mut self) {
        (self.callback)();
    }
}

fn simple_callback() {
    println!("hello");
}

fn main() {
    let _ = Processor::new();
    
    let mut p = Processor {
        callback: Box::new(simple_callback),
    };
    p.process_events();
    let s = "world!".to_string();
    let callback2 = move || println!("hello {}", s);
    p.set_callback(callback2);
    p.process_events();
}

Note:

  • “impl FnMut()” can only used in function declaration, not in struct declaration.
  • dyn FnMut() is unsized, so it must be stored in Box
  • set_callback(&mut self, c: impl FnMut()) need a lifetime for c to tell compiler that c outlives structure
    • rustc suggests impl FnMut() + 'static, but that is too restrictive
      • In most cases, we do not have a static lifetimed callback
  • FnMut() is more restrictive than FnOnce(), but FnOnce() can only be called once
  • set_callback(…) is a template method, because each closure has a different type

以上是关于Rust callback idiom的主要内容,如果未能解决你的问题,请参考以下文章

实现 pimpl idiom 时出现链接器错误

rust - 将闭包传递给特征方法: expected type parameter,发现闭包

Usage and Idioms——Theories

Rust 中的惯用回调

如何创建 Rust 回调函数以传递给 FFI 函数?

C++: The PIMPL idiom