use std::sync::RwLock;
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct Config {
pub max_packet_size: usize,
}
impl Default for Config {
fn default() -> Self {
Self {
max_packet_size: 1 << 20,
}
}
}
static CONFIG: RwLock<Config> = RwLock::new(Config {
max_packet_size: 1 << 20,
});
#[inline]
pub fn set_config(config: Config) {
let mut c = CONFIG.write().unwrap();
*c = config;
}
#[inline]
pub fn get_config() -> Config {
let c = CONFIG.read().unwrap();
(*c).clone()
}
#[inline]
pub fn get_max_packet_size() -> usize {
let c = CONFIG.read().unwrap();
(*c).max_packet_size
}
#[cfg(test)]
mod test {
use crate::config::{Config, get_max_packet_size, set_config};
#[test]
fn get() {
let _ = get_max_packet_size();
}
#[test]
fn set() {
set_config(Config { max_packet_size: 1 });
assert_eq!(1, get_max_packet_size());
}
#[test]
fn set_twice() {
set_config(Config { max_packet_size: 1 });
assert_eq!(1, get_max_packet_size());
set_config(Config { max_packet_size: 2 });
assert_eq!(2, get_max_packet_size());
}
}