I have the following sketch of an implementation:
trait Listener { fn some_action(&mut self); fn commit(self); } struct FooListener {} impl Listener for FooListener { fn some_action(&mut self) { println!("{:?}", "Action!!"); } fn commit(self) { println!("{:?}", "Commit"); } } struct Transaction { listeners: Vec<Box<Listener>>, } impl Transaction { fn commit(self) { // How would I consume the listeners and call commit() on each of them? } } fn listener() { let transaction = Transaction { listeners: vec![Box::new(FooListener {})], }; transaction.commit(); }
I can have Transaction
s with listeners on them that will call the listener when something happens on that transaction. Since Listener
is a trait, I store a Vec<Box<Listener>>
.
I'm having a hard time implementing commit
for Transaction
. Somehow I have to consume the boxes by calling commit
on each of the stored Listener
s, but I can't move stuff out of a box as far as I know.
How would I consume my listeners on commit?