You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
66 lines
1.8 KiB
66 lines
1.8 KiB
5 years ago
|
//! Queues for recv and send
|
||
|
use std::collections::VecDeque;
|
||
|
|
||
|
use mio::Token;
|
||
|
|
||
|
/// Queue of clients with a message to receive.
|
||
|
///
|
||
|
/// * The client is designated with the [`Token`](../../mio/struct.Token.html).
|
||
|
/// * [`String`](https://doc.rust-lang.org/nightly/alloc/string/struct.String.html)
|
||
|
/// is used to store the message.
|
||
|
#[derive(Debug)]
|
||
|
pub struct RecvQueue(VecDeque<(Token, String)>);
|
||
|
|
||
|
impl RecvQueue {
|
||
|
/// Create a new, empty `RecvQueue`.
|
||
|
pub fn new() -> Self {
|
||
|
RecvQueue(VecDeque::new())
|
||
|
}
|
||
|
|
||
|
/// Remove and return the first message in the `RecvQueue`.
|
||
|
pub fn pop(&mut self) -> Option<(Token, String)> {
|
||
|
self.0.pop_front()
|
||
|
}
|
||
|
|
||
|
/// Add a message to the end of the `RecvQueue`.
|
||
|
pub fn push<S: Into<String>>(&mut self, token: Token, s: S) {
|
||
|
self.0.push_back((token, s.into()));
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// Queue of messages to send to clients.
|
||
|
///
|
||
|
/// * The client is designated with the [`Token`](../../mio/struct.Token.html).
|
||
|
/// * [`String`](https://doc.rust-lang.org/nightly/alloc/string/struct.String.html)
|
||
|
/// is used to store the message.
|
||
|
/// * [`bool`](https://doc.rust-lang.org/nightly/std/primitive.bool.html) is set to `true` if a
|
||
|
/// prompt is to be displayed following the message.
|
||
|
#[derive(Debug)]
|
||
|
pub struct SendQueue(pub VecDeque<(Token, String, bool)>);
|
||
|
|
||
|
impl SendQueue {
|
||
|
pub fn new() -> Self {
|
||
|
SendQueue(VecDeque::new())
|
||
|
}
|
||
|
|
||
|
pub fn append(&mut self, queue: &mut Self) {
|
||
|
self.0.append(&mut queue.0);
|
||
|
}
|
||
|
|
||
|
pub fn pop(&mut self) -> Option<(Token, String, bool)> {
|
||
|
self.0.pop_front()
|
||
|
}
|
||
|
|
||
|
pub fn push<S: Into<String>>(&mut self, token: Token, s: S, prompt: bool) {
|
||
|
self.0.push_back((token, s.into(), prompt));
|
||
|
}
|
||
|
|
||
|
pub fn error(token: Token) -> Self {
|
||
|
Self(vec![(token, "Error".to_string(), true)].into())
|
||
|
}
|
||
|
|
||
|
pub fn ok(token: Token) -> Self {
|
||
|
Self(vec![(token, "Ok".to_string(), true)].into())
|
||
|
}
|
||
|
}
|