release/1.3.1
rs 31 lines 974 Bytes
Raw
1 //! Which session and message a running turn belongs to.
2
3 use std::sync::{Arc, Mutex};
4
5 /// Shared between the sink and the approval handler so that events emitted
6 /// from inside Ed (which knows nothing about sessions) can still say which
7 /// session they belong to.
8 ///
9 /// Ed runs one turn at a time, so one slot is enough.
10 #[derive(Clone, Default)]
11 pub(crate) struct TurnScope(Arc<Mutex<Option<(String, String)>>>);
12
13 impl TurnScope {
14 pub(crate) fn set(&self, session: &str, id: &str) {
15 *self.0.lock().unwrap_or_else(|e| e.into_inner()) =
16 Some((session.to_owned(), id.to_owned()));
17 }
18
19 pub(crate) fn clear(&self) {
20 *self.0.lock().unwrap_or_else(|e| e.into_inner()) = None;
21 }
22
23 /// `(session, id)`, both empty outside a `submit`.
24 pub(crate) fn get(&self) -> (String, String) {
25 self.0
26 .lock()
27 .unwrap_or_else(|e| e.into_inner())
28 .clone()
29 .unwrap_or_default()
30 }
31 }