1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
use std::rc::Rc; pub struct Mailbox<Message: 'static> { func: Rc<dyn Fn(Option<Message>)>, } impl<Ms> Mailbox<Ms> { pub fn new(func: impl Fn(Option<Ms>) + 'static) -> Self { Mailbox { func: Rc::new(func), } } pub fn send(&self, message: Option<Ms>) { (self.func)(message) } } impl<Ms> Clone for Mailbox<Ms> { fn clone(&self) -> Self { Mailbox { func: self.func.clone(), } } }