add_ed/ui/lock.rs
1use super::UI;
2
3/// A simple object that locks UI and calls unlock_ui on it when being dropped
4/// (Since it holds a mutable ref to UI its existence locks UI interaction)
5///
6/// Exists to provide a mutex mechanism for UIs which cannot do anything else
7/// while an [`crate::IO`] is using the process' tty.
8pub struct UILock<'a> {
9 inner: &'a mut dyn UI,
10}
11impl <'a> UILock<'a> {
12 /// Construct a lock containing the given mutable reference
13 ///
14 /// Locks the given [`UI`] (due to the borrow checker) until the created lock
15 /// is dropped, upon which it will call [`UI::unlock_ui`] before disappearing.
16 pub fn new(ui: &'a mut dyn UI) -> Self {
17 Self{inner: ui}
18 }
19}
20impl Drop for UILock<'_> {
21 fn drop(&mut self) {
22 self.inner.unlock_ui();
23 }
24}