Skip to main content

externally_driven_async_rt/event_loop/
error.rs

1/// An error that can be returned from an [`EventLoop`](crate::EventLoop).
2#[derive(Debug, Clone, Copy, PartialEq, Eq)]
3pub enum EventLoopError {
4    /// An epoll or kqueue call failed.
5    Io(rustix::io::Errno),
6    /// [`EventLoop::drain_events_without_blocking`](crate::EventLoop::drain_events_without_blocking)
7    /// is called on the [`EventLoop`](crate::EventLoop) that has already finished
8    RootFinished,
9    /// [`EventLoop::new`](crate::EventLoop::new) is called with a
10    /// [`LoopStorage`](crate::LoopStorage) that has already been used by another
11    /// [`EventLoop`](crate::EventLoop). A storage backs at most one event loop for the whole
12    /// program, even after that loop has finished.
13    StorageAlreadyUsed,
14    /// [`EventLoop::drain_events_without_blocking`](crate::EventLoop::drain_events_without_blocking)
15    /// panicked before, the [`EventLoop`](crate::EventLoop) may be in an inconsistent state, so it
16    /// refuses to run. Its tasks are dropped once it's dropped.
17    Poisoned,
18}
19
20impl core::fmt::Display for EventLoopError {
21    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
22        match self {
23            Self::Io(errno) => write!(f, "event loop IO error: {errno}"),
24            Self::RootFinished => f.write_str("the root future has finished"),
25            Self::StorageAlreadyUsed => {
26                f.write_str("the storage has already been used by another event loop")
27            }
28            Self::Poisoned => f.write_str("the event loop panicked before and is poisoned"),
29        }
30    }
31}
32
33impl core::error::Error for EventLoopError {}
34
35impl From<rustix::io::Errno> for EventLoopError {
36    fn from(errno: rustix::io::Errno) -> Self {
37        Self::Io(errno)
38    }
39}
40
41#[cfg(test)]
42mod tests {
43    use super::EventLoopError;
44
45    #[test]
46    fn errors_display_their_cause() {
47        struct StartsWith<'a>(&'a str);
48
49        impl core::fmt::Write for StartsWith<'_> {
50            fn write_str(&mut self, s: &str) -> core::fmt::Result {
51                match self.0.strip_prefix(s) {
52                    Some(rest) => self.0 = rest,
53                    None if self.0.is_empty() => {}
54                    None => return Err(core::fmt::Error),
55                }
56                Ok(())
57            }
58        }
59
60        for (error, expected) in [
61            (EventLoopError::RootFinished, "the root future has finished"),
62            (
63                EventLoopError::Io(rustix::io::Errno::BADF),
64                "event loop IO error: ",
65            ),
66            (
67                EventLoopError::StorageAlreadyUsed,
68                "the storage has already been used by another event loop",
69            ),
70            (
71                EventLoopError::Poisoned,
72                "the event loop panicked before and is poisoned",
73            ),
74        ] {
75            let mut prefix = StartsWith(expected);
76            core::fmt::write(&mut prefix, format_args!("{error}")).unwrap();
77            assert_eq!(prefix.0, "", "{error:?}");
78        }
79    }
80}