1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
use std::{
io::{Stdin, StdinLock},
time::Duration,
};
use crate::{
Result,
raw::{SysEvent, wait_for_event},
};
/// The type can wait for system event with the given timeout.
pub trait WaitForEvent {
/// Waits for input with the given timeout.
///
/// - [`Duration::ZERO`] means no blocking.
/// - [`Duration::MAX`] means wait indefinitely.
fn wait_for_event(&self, timeout: Duration) -> Result<SysEvent>;
}
impl WaitForEvent for Stdin {
/// Wait for any event on stdin or other supported event, but not longer than
/// the timeout.
///
/// If timeout is [`Duration::MAX`], this will wait indefinitely.
///
/// # Returns
/// The kind of event detected or `SysEvent::Timeout` if timeout was reached.
///
/// # Support
/// - Unix (Linux)
/// - `SysEvent::Stdin`
/// - `SysEvent::Resize`
/// - Windows (not tested)
/// - `SysEvent::Stdin`
///
/// # Errors
/// - [`Error::NotSupportedOnPlatform`] on unsupported platforms.
/// - [`Error::Io`] on io error.
/// - [`Error::WaitAbandoned`] when unexpected state happens. See error
/// description.
/// - [`Error::IntConvert`] when timeout value is too large (but not
/// [`Duration::MAX`])
fn wait_for_event(&self, timeout: Duration) -> Result<SysEvent> {
wait_for_event(timeout)
}
}
impl WaitForEvent for StdinLock<'static> {
/// Wait for any event on stdin or other supported event, but not longer than
/// the timeout.
///
/// If timeout is [`Duration::MAX`], this will wait indefinitely.
///
/// # Returns
/// The kind of event detected or `SysEvent::Timeout` if timeout was reached.
///
/// # Support
/// - Unix (Linux)
/// - `SysEvent::Stdin`
/// - `SysEvent::Resize`
/// - Windows (not tested)
/// - `SysEvent::Stdin`
///
/// # Errors
/// - [`Error::NotSupportedOnPlatform`] on unsupported platforms.
/// - [`Error::Io`] on io error.
/// - [`Error::WaitAbandoned`] when unexpected state happens. See error
/// description.
/// - [`Error::IntConvert`] when timeout value is too large (but not
/// [`Duration::MAX`])
fn wait_for_event(&self, timeout: Duration) -> Result<SysEvent> {
wait_for_event(timeout)
}
}