NLinker

Calling the `retain` on the part of the game state

Jul 13th, 2018
423
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 2.69 KB | None | 0 0
  1. /// ### The problem
  2.  
  3.         for k in 0..np {
  4.             if k == index {
  5.                 gs.players[k].body_mut().clear();
  6.                 gs.players[k].body_mut().push(new_head);
  7.             } else if gs.players[k].body().is_empty() {
  8.                 panic!("Broken invariant");
  9.             } else {
  10.                 let head = *gs.players[k].head().expect("Broken invariant");
  11.                 // let cells = &gs.field.cells;
  12.                 gs.players[k].body_mut().retain(|ref p| {
  13.                     // remove the points from the body if they are owned by someone else
  14.                     match gs.field.cells[p.0 as usize][p.1 as usize] {
  15.                         Cell::Owned(_) => false,
  16.                         _ => true
  17.                     }
  18.                 });
  19.                 // just in case
  20.                 if gs.players[k].body().is_empty() {
  21.                     gs.players[k].body_mut().push(head);
  22.                 }
  23.             }
  24.         }
  25.  
  26. /*
  27. We get the error:
  28. ```
  29. error[E0502]: cannot borrow `gs` as immutable because `gs.players` is also borrowed as mutable
  30.    --> src/model.rs:723:49
  31.     |
  32. 723 |                 gs.players[k].body_mut().retain(|ref p| {
  33.     |                 ----------                      ^^^^^^^ immutable borrow occurs here
  34.     |                 |
  35.     |                 mutable borrow occurs here
  36. 724 |                     // remove the points from the body if they are owned by someone else
  37. 725 |                     match gs.field.cells[p.0 as usize][p.1 as usize] {
  38.     |                           -- borrow occurs due to use of `gs` in closure
  39. ...
  40. 729 |                 });
  41.     |                  - mutable borrow ends here
  42. */
  43.  
  44. /// ### Fix
  45.  
  46.         for k in 0..np {
  47.             if k == index {
  48.                 gs.players[k].body_mut().clear();
  49.                 gs.players[k].body_mut().push(new_head);
  50.             } else if gs.players[k].body().is_empty() {
  51.                 panic!("Broken invariant");
  52.             } else {
  53.                 let head = *gs.players[k].head().expect("Broken invariant");
  54.                 // make the closure to refer only the part of the game state, not the whole one
  55.                 let cells = &gs.field.cells;
  56.                 gs.players[k].body_mut().retain(|ref p| {
  57.                     // remove the points from the body if they are owned by someone else
  58.                     match cells[p.0 as usize][p.1 as usize] {
  59.                         Cell::Owned(_) => false,
  60.                         _ => true
  61.                     }
  62.                 });
  63.                 // just in case
  64.                 if gs.players[k].body().is_empty() {
  65.                     gs.players[k].body_mut().push(head);
  66.                 }
  67.             }
  68.         }
Add Comment
Please, Sign In to add comment