externally_driven_async_rt/event_loop/
error.rs1#[derive(Debug, Clone, Copy, PartialEq, Eq)]
3pub enum EventLoopError {
4 Io(rustix::io::Errno),
6 RootFinished,
9 StorageAlreadyUsed,
14 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}