Skip to main content

thedes_tui_core/runtime/
device.rs

1use std::fmt;
2
3use thiserror::Error;
4use tokio::io;
5
6use crate::{
7    audio::device::AudioDevice,
8    input::device::InputDevice,
9    panic::restore::PanicRestoreGuard,
10    screen::device::ScreenDevice,
11};
12
13pub mod native;
14pub mod null;
15
16#[cfg(feature = "testing")]
17pub mod mock;
18
19#[derive(Debug, Error)]
20pub enum Error {
21    #[error("Already initialized")]
22    AlreadyInit,
23    #[error("Not initialized yet or already shut down")]
24    NotInit,
25    #[error(transparent)]
26    Io(#[from] io::Error),
27}
28
29pub trait RuntimeDevice: fmt::Debug + Send + Sync {
30    fn blocking_init(&mut self) -> Result<(), Error>;
31
32    fn blocking_shutdown(&mut self) -> Result<(), Error>;
33
34    fn open_screen_device(&mut self) -> Box<dyn ScreenDevice>;
35
36    fn open_input_device(&mut self) -> Box<dyn InputDevice>;
37
38    fn open_audio_device(&mut self) -> Box<dyn AudioDevice>;
39
40    fn open_panic_restore_guard(&mut self) -> Box<dyn PanicRestoreGuard>;
41}
42
43impl<'a, T> RuntimeDevice for &'a mut T
44where
45    T: RuntimeDevice + ?Sized,
46{
47    fn blocking_init(&mut self) -> Result<(), Error> {
48        (**self).blocking_init()
49    }
50
51    fn blocking_shutdown(&mut self) -> Result<(), Error> {
52        (**self).blocking_shutdown()
53    }
54
55    fn open_screen_device(&mut self) -> Box<dyn ScreenDevice> {
56        (**self).open_screen_device()
57    }
58
59    fn open_input_device(&mut self) -> Box<dyn InputDevice> {
60        (**self).open_input_device()
61    }
62
63    fn open_audio_device(&mut self) -> Box<dyn AudioDevice> {
64        (**self).open_audio_device()
65    }
66
67    fn open_panic_restore_guard(&mut self) -> Box<dyn PanicRestoreGuard> {
68        (**self).open_panic_restore_guard()
69    }
70}
71
72impl<T> RuntimeDevice for Box<T>
73where
74    T: RuntimeDevice + ?Sized,
75{
76    fn blocking_init(&mut self) -> Result<(), Error> {
77        (**self).blocking_init()
78    }
79
80    fn blocking_shutdown(&mut self) -> Result<(), Error> {
81        (**self).blocking_shutdown()
82    }
83
84    fn open_screen_device(&mut self) -> Box<dyn ScreenDevice> {
85        (**self).open_screen_device()
86    }
87
88    fn open_input_device(&mut self) -> Box<dyn InputDevice> {
89        (**self).open_input_device()
90    }
91
92    fn open_audio_device(&mut self) -> Box<dyn AudioDevice> {
93        (**self).open_audio_device()
94    }
95
96    fn open_panic_restore_guard(&mut self) -> Box<dyn PanicRestoreGuard> {
97        (**self).open_panic_restore_guard()
98    }
99}