Access to the hashmap field value as & mut inside a struct impl

a simple structure is given:

struct Server {
  clients: HashMap<usize, Client>
}

What is the best way to access Clienthow &mut? Consider the following code:

use std::collections::HashMap;

struct Client {
  pub poked: bool
}

impl Client {
  pub fn poked(&self) -> bool {
    self.poked
  }

  pub fn set_poked(&mut self) {
    self.poked = true;
  }
}

struct Server {
  clients: HashMap<usize, Client>
}

impl Server {
  pub fn poke_client(&mut self, token: usize) {
    let client = self.clients.get_mut(&token).unwrap();
    self.poke(client);
  }

  fn poke(&self, c: &mut Client) {
    c.set_poked();
  }
}

fn main() {
    let mut s = Server { clients: HashMap::new() };
    s.clients.insert(1, Client { poked: false });

    s.poke_client(1);

    assert!(s.clients.get(&1).unwrap().poked() == true);
}

The only thing I see is to use RefCell/ Cellinside the Client, which makes things look really awful:

pub struct Client {
    nickname: RefCell<Option<String>>,
    username: RefCell<Option<String>>,
    realname: RefCell<Option<String>>,
    hostname: RefCell<Option<String>>,
    out_socket: RefCell<Box<Write>>,
}

Or wrap clientsin RefCell, which makes it impossible to use simple methods like this for Server:

pub fn client_by_token(&self, token: usize) -> Option<&Client> {
    self.clients_tok.get(&token)
}

forcing me to use closures (like with_client_by_token(|c| ...)).

+4
source share
1 answer

As the error message says, you cannot reborrow selfwhen it is already skillfully borrowed:

<anon>:24:5: 24:9 error: cannot borrow `*self` as immutable because `self.clients` is also borrowed as mutable
<anon>:24     self.poke(client);
              ^~~~

:

pub fn poke_client(&mut self, token: usize) {
    let client = self.clients.get_mut(&token).unwrap();
    self.poke(client);
}

self , , poke. - Client::set_poked :

pub fn poke_client(&mut self, token: usize) {
    let client = self.clients.get_mut(&token).unwrap();
    client.set_poked();
}

, self:

impl Server {
    pub fn poke_client(&mut self, token: usize) {
        let client = self.clients.get_mut(&token).unwrap();
        Server::poke(client);
    }

    fn poke(c: &mut Client) {
        c.set_poked();
    }
}

self, poke. , Server Client.

+1

Source: https://habr.com/ru/post/1584949/


All Articles