derefmut for confighandle

This commit is contained in:
Alex Janka 2024-01-22 10:42:21 +11:00
parent fc3f7103d1
commit 84c999a662
2 changed files with 19 additions and 4 deletions

View file

@ -1,4 +1,5 @@
use std::{
ops::DerefMut,
path::PathBuf,
sync::{OnceLock, RwLock, RwLockReadGuard, RwLockWriteGuard},
};
@ -21,13 +22,27 @@ pub fn access_config<'a>() -> RwLockReadGuard<'a, Config> {
}
pub struct ConfigHandle<'a> {
pub handle: RwLockWriteGuard<'a, Config>,
handle: RwLockWriteGuard<'a, Config>,
}
impl<'a> core::ops::Deref for ConfigHandle<'a> {
type Target = RwLockWriteGuard<'a, Config>;
fn deref(&self) -> &Self::Target {
&self.handle
}
}
impl<'a> DerefMut for ConfigHandle<'a> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.handle
}
}
impl<'a> Drop for ConfigHandle<'a> {
fn drop(&mut self) {
log::warn!("saving config...");
let _ = self.handle.save().some_or_print_with("saving config");
let _ = self.save().some_or_print_with("saving config");
}
}

View file

@ -143,14 +143,14 @@ async fn enable_control(state: &State<ServerState>) {
#[post("/set-max/<limit>")]
async fn set_max(limit: i64) -> String {
let limit = limit.clamp(access_config().min_rate, 15);
write_to_config().handle.max_rate = limit;
write_to_config().max_rate = limit;
format!("set upper limit to {limit}")
}
#[post("/set-min/<limit>")]
async fn set_min(limit: i64) -> String {
let limit = limit.clamp(3, access_config().max_rate);
write_to_config().handle.min_rate = limit;
write_to_config().min_rate = limit;
format!("set lower limit to {limit}")
}