Skip to content

Instantly share code, notes, and snippets.

@olsonjeffery
Last active August 29, 2015 14:01
Show Gist options
  • Save olsonjeffery/0fcf9e0fc724aaa2034b to your computer and use it in GitHub Desktop.
Save olsonjeffery/0fcf9e0fc724aaa2034b to your computer and use it in GitHub Desktop.
issue w/ borrows within a loop
jeff@mbp:~/src/dungeongame (master)$ make
check: formatting
make -C p2d -f Makefile
check: formatting
rustc -L lib --opt-level=3 --out-dir lib -D warnings --dep-info .rust/p2dux.deps.mk src/p2dux/lib.rs
src/p2dux/view/mod.rs:61:29: 61:37 error: cannot borrow `*passives` as mutable more than once at a time
src/p2dux/view/mod.rs:61 for view in passives.mut_iter() {
^~~~~~~~
src/p2dux/view/mod.rs:71:67: 71:75 note: previous borrow of `*passives` occurs here; the mutable borrow prevents subsequent moves, borrows, or modification of
`*passives` until the borrow ends
src/p2dux/view/mod.rs:71 match active.handle(display, events.as_slice(), time, passives, self) {
^~~~~~~~
src/p2dux/view/mod.rs:77:6: 77:6 note: previous borrow ends here
src/p2dux/view/mod.rs:54 passives: &'a mut Vec<&'a mut PassiveView>) -> TOut {
...
src/p2dux/view/mod.rs:77 }
^
src/p2dux/view/mod.rs:71:19: 71:25 error: cannot borrow `*active` as mutable more than once at a time
src/p2dux/view/mod.rs:71 match active.handle(display, events.as_slice(), time, passives, self) {
^~~~~~
src/p2dux/view/mod.rs:71:19: 71:25 note: previous borrow of `*active` occurs here; the mutable borrow prevents subsequent moves, borrows, or modification of $
*active` until the borrow ends
src/p2dux/view/mod.rs:71 match active.handle(display, events.as_slice(), time, passives, self) {
^~~~~~
src/p2dux/view/mod.rs:77:6: 77:6 note: previous borrow ends here
src/p2dux/view/mod.rs:54 passives: &'a mut Vec<&'a mut PassiveView>) -> TOut {
...
src/p2dux/view/mod.rs:77 }
^
src/p2dux/view/mod.rs:71:67: 71:75 error: cannot borrow `*passives` as mutable more than once at a time
src/p2dux/view/mod.rs:71 match active.handle(display, events.as_slice(), time, passives, self) {
^~~~~~~~
src/p2dux/view/mod.rs:71:67: 71:75 note: previous borrow of `*passives` occurs here; the mutable borrow prevents subsequent moves, borrows, or modification o$
`*passives` until the borrow ends
src/p2dux/view/mod.rs:71 match active.handle(display, events.as_slice(), time, passives, self) {
^~~~~~~~
src/p2dux/view/mod.rs:77:6: 77:6 note: previous borrow ends here
src/p2dux/view/mod.rs:54 passives: &'a mut Vec<&'a mut PassiveView>) -> TOut {
...
src/p2dux/view/mod.rs:77 }
^
src/p2dux/view/mod.rs:71:77: 71:81 error: cannot borrow `*self` as mutable more than once at a time
src/p2dux/view/mod.rs:71 match active.handle(display, events.as_slice(), time, passives, self) {
^~~~
src/p2dux/view/mod.rs:71:77: 71:81 note: previous borrow of `*self` occurs here; the mutable borrow prevents subsequent moves, borrows, or modification of `*$
elf` until the borrow ends
src/p2dux/view/mod.rs:71 match active.handle(display, events.as_slice(), time, passives, self) {
^~~~
src/p2dux/view/mod.rs:77:6: 77:6 note: previous borrow ends here
src/p2dux/view/mod.rs:54 passives: &'a mut Vec<&'a mut PassiveView>) -> TOut {
...
src/p2dux/view/mod.rs:77 }
^
error: aborting due to 4 previous errors
impl ViewManager {
pub fn enter_once<'a, TOut: Send>(&'a mut self, display: &GameDisplay,
yielding: &'a mut PassiveView,
active: &'a mut ActiveView<TOut>,
passives: &'a mut Vec<&'a mut PassiveView>) -> Option<TOut> {
passives.push(yielding);
let (sender, receiver) = channel();
loop {
let time = 0;
let mut events = Vec::new();
{
for view in passives.mut_iter() {
view.update(display, time);
}
}
loop {
match poll_event() {
NoEvent => { break; },
event => { events.push(event); }
}
}
match active.handle(display, events.as_slice(), time, passives, self) {
Some(out) => { sender.send(out); break },
_ => {}
}
}
receiver.recv()
}
}
// this builds
pub trait ActiveView<TOut> : PassiveView {
fn handle<'a> (&'a mut self, display: &GameDisplay, events: &[Event], time: u64,
passives: &'a mut Vec<&'a mut PassiveView>, yielder: &'a mut ViewManager)
-> Option<TOut>;
}
pub trait PassiveView {
fn update(&mut self, display: &GameDisplay, time: u64);
}
pub struct ViewManager;
impl ViewManager {
pub fn enter_once<'a, TOut: Send>(&'a mut self, display: &GameDisplay,
yielding: &'a mut PassiveView,
active: &'a mut ActiveView<TOut>,
passives: &'a mut Vec<&'a mut PassiveView>) -> Option<TOut> {
passives.push(yielding);
let time = 0;
let mut events = Vec::new();
{
for view in passives.mut_iter() {
view.update(display, time);
}
}
loop {
match poll_event() {
NoEvent => { break; },
event => { events.push(event); }
}
}
active.handle(display, events.as_slice(), time, passives, self)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment