use std::collections::HashMap; use std::sync::Mutex; use once_cell::sync::Lazy; pub struct SessionState { pub config_json: String, pub bit_handle: Option, pub application_data: Vec, pub logged_in: bool, } pub static SESSIONS: Lazy>> = Lazy::new(|| Mutex::new(HashMap::new())); static NEXT_SESSION_ID: Lazy> = Lazy::new(|| Mutex::new(1)); pub fn register_session(config_json: String, application_data: Vec) -> i64 { let mut next_id = NEXT_SESSION_ID.lock().unwrap(); let id = *next_id; *next_id += 1; let mut sessions = SESSIONS.lock().unwrap(); sessions.insert(id, SessionState { config_json, bit_handle: None, application_data, logged_in: false, }); id } pub fn with_session(session_id: i64, f: F) -> Option where F: FnOnce(&mut SessionState) -> R, { let mut sessions = SESSIONS.lock().unwrap(); sessions.get_mut(&session_id).map(f) } pub fn remove_session(session_id: i64) { let mut sessions = SESSIONS.lock().unwrap(); sessions.remove(&session_id); }