fix: use entry API for `update_fixer_state`

This commit is contained in:
Harsh Shandilya 2024-04-29 06:02:36 +05:30
parent a68a8204fe
commit 61928ddea7
1 changed files with 5 additions and 10 deletions

View File

@ -58,21 +58,16 @@ async fn check_authorized(bot: &Bot, message: &Message) -> Result<bool, AsyncErr
fn update_fixer_state<F>(message: &Message, update_state: F)
where
F: FnOnce(&mut FixerState) -> (),
F: FnOnce(&mut FixerState) -> () + Copy,
{
if let Ok(ref mut map) = FIXER_STATE.try_lock() {
let result = match map.get_mut(&message.chat.id) {
Some(state) => {
update_state(state);
*state
}
None => {
map.entry(message.chat.id)
.and_modify(update_state)
.or_insert_with(|| {
let mut state = FixerState::default();
update_state(&mut state);
state
}
};
map.insert(message.chat.id, result);
});
}
}