Advertisement
Guest User

Untitled

a guest
Jul 16th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. use std::collections::HashMap;
  2. use std::cell::RefCell;
  3. // use std::io;
  4. // use std::cell::{Ref, RefMut};
  5.  
  6.  
  7. struct Player {
  8. id: u32,
  9. room_id: u32,
  10. }
  11.  
  12. impl Player {
  13. pub fn writeln<T: AsRef<str>>(&mut self, msg: T) {
  14. }
  15. }
  16.  
  17. struct PlayerStore {
  18. players: HashMap<u32, RefCell<Player>>,
  19. }
  20.  
  21. impl PlayerStore {
  22. pub fn for_each_in_room(&self, room_id: u32, per_player: impl Fn(&mut Player) -> ()) {
  23. self.players.iter()
  24. .filter(|(_pid, p)| p.borrow().room_id == room_id)
  25. .for_each(|(_pid, p)| per_player(&mut p.borrow_mut()));
  26. }
  27. }
  28.  
  29. fn main() -> () {
  30. let player_store = PlayerStore {
  31. players: HashMap::new()
  32. };
  33.  
  34. let my_room_id = 5;
  35. player_store.for_each_in_room(
  36. my_room_id,
  37. |other_player| {
  38. if other_player.id != 44 {
  39. other_player.writeln(format!("{} says: {}", "hey", "hello"));
  40. }
  41. }
  42. );
  43.  
  44.  
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement