//! Contains functions and structures useful to the general web server use std::collections::{HashSet, VecDeque}; use tokio::sync::{broadcast, Mutex}; /// Our shared state #[derive(Debug)] pub struct State { /// Contains the history of the last 100 messages sent pub message_history: Mutex>, /// Channel used to send messages to all connected clients. pub tx: broadcast::Sender, /// We require unique usernames. This tracks which usernames have been /// taken. pub user_set: Mutex>, } /// Doc pub async fn check_username(state: &State, string: &mut String, name: &str) { let mut user_set = state.user_set.lock().await; let name = name.trim(); if !name.is_empty() && !user_set.contains(name) { user_set.insert(name.to_owned()); drop(user_set); string.push_str(name); } }