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.

83 lines
2.1 KiB

5 years ago
//! Queues for recv and send
5 years ago
use std::collections::VecDeque;
use mio::Token;
use crate::state::State;
5 years ago
/// 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.
/// * `Option<State>` to have the player be put into a different play state
5 years ago
#[derive(Debug)]
pub struct SendQueue(pub VecDeque<(Token, String, bool, Option<State>)>);
5 years ago
impl<S: Into<String>> From<(Token, S)> for SendQueue {
fn from(tuple: (Token, S)) -> Self {
let (t, s) = tuple;
Self(vec![(t, s.into(), true, None)].into())
}
}
5 years ago
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, Option<State>)> {
5 years ago
self.0.pop_front()
}
pub fn push<S: Into<String>>(
&mut self,
token: Token,
s: S,
prompt: bool,
state: Option<State>,
) {
self.0.push_back((token, s.into(), prompt, state));
5 years ago
}
pub fn error(token: Token) -> Self {
Self(vec![(token, "Error".to_string(), true, None)].into())
5 years ago
}
pub fn ok(token: Token) -> Self {
Self(vec![(token, "Ok".to_string(), true, None)].into())
5 years ago
}
}