Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::collections::HashMap;
- use std::cell::RefCell;
- // use std::io;
- // use std::cell::{Ref, RefMut};
- struct Player {
- id: u32,
- room_id: u32,
- }
- impl Player {
- pub fn writeln<T: AsRef<str>>(&mut self, msg: T) {
- }
- }
- struct PlayerStore {
- players: HashMap<u32, RefCell<Player>>,
- }
- impl PlayerStore {
- pub fn for_each_in_room(&self, room_id: u32, per_player: Fn(&Player) -> ()) {
- self.players.iter()
- .filter(|(_pid, p)| p.borrow().room_id == room_id)
- .for_each(|(_pid, p)| per_player(&p.borrow_mut()));
- }
- }
- fn main() -> () {
- let player_store = PlayerStore {
- players: HashMap::new()
- };
- let my_room_id = 5;
- player_store.for_each_in_room(
- my_room_id,
- |other_player| {
- if (&other_player).id != 44 {
- (&mut other_player).writeln(format!("{} says: {}", "hey", "hello"));
- }
- }
- );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement