Split states.rs

This commit is contained in:
Mo 2022-10-29 00:40:54 +02:00
parent 764ebab7b5
commit 047b5de2d1

View file

@ -1,6 +1,6 @@
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex, MutexGuard};
pub struct CaptchaSolutions { struct CaptchaSolutions {
last_id: u16, last_id: u16,
solutions: Vec<Option<String>>, solutions: Vec<Option<String>>,
} }
@ -23,8 +23,12 @@ impl SharedCaptchaSolutions {
} }
} }
fn lock(&self) -> MutexGuard<CaptchaSolutions> {
self.arc.lock().unwrap()
}
pub fn store_solution(&self, solution: &str) -> u16 { pub fn store_solution(&self, solution: &str) -> u16 {
let mut captcha_solutions = self.arc.lock().unwrap(); let mut captcha_solutions = self.lock();
let new_id = captcha_solutions.last_id.wrapping_add(1); let new_id = captcha_solutions.last_id.wrapping_add(1);
captcha_solutions.last_id = new_id; captcha_solutions.last_id = new_id;
@ -36,7 +40,7 @@ impl SharedCaptchaSolutions {
pub fn check_answer(&self, id: u16, answer: &str) -> bool { pub fn check_answer(&self, id: u16, answer: &str) -> bool {
{ {
let mut captcha_solutions = self.arc.lock().unwrap(); let mut captcha_solutions = self.lock();
let id = id as usize; let id = id as usize;