Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::sync::Arc;
- trait Foo {
- fn get_bars(&mut self) -> Vec<&mut Arc<Bar>>;
- // Solution!
- // fn do_stuff_on_bars(&mut self);
- }
- struct Fooy {
- list: Vec<Arc<Bar>>
- }
- impl Foo for Fooy {
- fn get_bars(&mut self) -> Vec<&mut Arc<Bar>> {
- self.list.iter_mut().collect()
- }
- // Solution!
- // fn do_stuff_on_bars(&mut self) {
- // for mut bar in self.list.iter_mut() {
- // Arc::get_mut(&mut bar).unwrap().stuff();
- // }
- // }
- }
- struct Bar {}
- impl Bar {
- fn stuff(&mut self) { /* do mut things.. */ }
- }
- struct App<'a> {
- foos: Vec<Box<Foo>>,
- bars: Vec<&'a mut Arc<Bar>>
- }
- impl<'a> App<'a> {
- fn load_foo(&mut self, f: Box<Foo>) {
- // self.bars.push_all_as_refs_by_magic(f.get_bars());
- self.foos.push(f);
- }
- fn dream_about_doing_stuff(&mut self) {
- for mut bar in self.bars.iter_mut() {
- Arc::get_mut(&mut bar).unwrap().stuff();
- }
- }
- // Solution!
- // fn do_stuff(&mut self) {
- // for foo in self.foos.iter_mut() {
- // foo.do_stuff_on_bars();
- // }
- // }
- }
- fn main() {
- let mut a = App {
- foos: Vec::new(),
- bars: Vec::new(),
- };
- a.load_foo(Box::new(Fooy {
- list: vec![Arc::new(Bar{}), Arc::new(Bar{})],
- }));
- a.dream_about_doing_stuff();
- // Solution!
- // a.do_stuff();
- println!("Done!");
- }
Add Comment
Please, Sign In to add comment