mirror of
https://github.com/actix/examples
synced 2025-02-24 18:13:21 +01:00
remove unneeded RefCells (#62)
This commit is contained in:
parent
e30915d98d
commit
8a81a3bb58
@ -3,8 +3,7 @@
|
|||||||
//! room through `ChatServer`.
|
//! room through `ChatServer`.
|
||||||
|
|
||||||
use actix::prelude::*;
|
use actix::prelude::*;
|
||||||
use rand::{self, Rng, ThreadRng};
|
use rand::{self, rngs::ThreadRng, Rng};
|
||||||
use std::cell::RefCell;
|
|
||||||
use std::collections::{HashMap, HashSet};
|
use std::collections::{HashMap, HashSet};
|
||||||
|
|
||||||
/// Chat server sends this messages to session
|
/// Chat server sends this messages to session
|
||||||
@ -58,7 +57,7 @@ pub struct Join {
|
|||||||
pub struct ChatServer {
|
pub struct ChatServer {
|
||||||
sessions: HashMap<usize, Recipient<Message>>,
|
sessions: HashMap<usize, Recipient<Message>>,
|
||||||
rooms: HashMap<String, HashSet<usize>>,
|
rooms: HashMap<String, HashSet<usize>>,
|
||||||
rng: RefCell<ThreadRng>,
|
rng: ThreadRng,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for ChatServer {
|
impl Default for ChatServer {
|
||||||
@ -70,7 +69,7 @@ impl Default for ChatServer {
|
|||||||
ChatServer {
|
ChatServer {
|
||||||
sessions: HashMap::new(),
|
sessions: HashMap::new(),
|
||||||
rooms: rooms,
|
rooms: rooms,
|
||||||
rng: RefCell::new(rand::thread_rng()),
|
rng: rand::thread_rng(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -110,7 +109,7 @@ impl Handler<Connect> for ChatServer {
|
|||||||
self.send_message(&"Main".to_owned(), "Someone joined", 0);
|
self.send_message(&"Main".to_owned(), "Someone joined", 0);
|
||||||
|
|
||||||
// register session with random id
|
// register session with random id
|
||||||
let id = self.rng.borrow_mut().gen::<usize>();
|
let id = self.rng.gen::<usize>();
|
||||||
self.sessions.insert(id, msg.addr);
|
self.sessions.insert(id, msg.addr);
|
||||||
|
|
||||||
// auto join session to Main room
|
// auto join session to Main room
|
||||||
|
@ -3,8 +3,7 @@
|
|||||||
//! room through `ChatServer`.
|
//! room through `ChatServer`.
|
||||||
|
|
||||||
use actix::prelude::*;
|
use actix::prelude::*;
|
||||||
use rand::{self, Rng, ThreadRng};
|
use rand::{self, rngs::ThreadRng, Rng};
|
||||||
use std::cell::RefCell;
|
|
||||||
use std::collections::{HashMap, HashSet};
|
use std::collections::{HashMap, HashSet};
|
||||||
|
|
||||||
use session;
|
use session;
|
||||||
@ -56,7 +55,7 @@ pub struct Join {
|
|||||||
pub struct ChatServer {
|
pub struct ChatServer {
|
||||||
sessions: HashMap<usize, Recipient<session::Message>>,
|
sessions: HashMap<usize, Recipient<session::Message>>,
|
||||||
rooms: HashMap<String, HashSet<usize>>,
|
rooms: HashMap<String, HashSet<usize>>,
|
||||||
rng: RefCell<ThreadRng>,
|
rng: ThreadRng,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for ChatServer {
|
impl Default for ChatServer {
|
||||||
@ -68,7 +67,7 @@ impl Default for ChatServer {
|
|||||||
ChatServer {
|
ChatServer {
|
||||||
sessions: HashMap::new(),
|
sessions: HashMap::new(),
|
||||||
rooms: rooms,
|
rooms: rooms,
|
||||||
rng: RefCell::new(rand::thread_rng()),
|
rng: rand::thread_rng(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -108,7 +107,7 @@ impl Handler<Connect> for ChatServer {
|
|||||||
self.send_message(&"Main".to_owned(), "Someone joined", 0);
|
self.send_message(&"Main".to_owned(), "Someone joined", 0);
|
||||||
|
|
||||||
// register session with random id
|
// register session with random id
|
||||||
let id = self.rng.borrow_mut().gen::<usize>();
|
let id = self.rng.gen::<usize>();
|
||||||
self.sessions.insert(id, msg.addr);
|
self.sessions.insert(id, msg.addr);
|
||||||
|
|
||||||
// auto join session to Main room
|
// auto join session to Main room
|
||||||
|
Loading…
x
Reference in New Issue
Block a user