Skip to main content

retroglyph_window/winit/
error.rs

1//! [`EventLoopError`]: an opaque wrapper around winit's own event-loop error.
2
3use core::fmt;
4
5/// The error winit's event loop can fail with when starting or running.
6///
7/// Wraps `winit`'s own `EventLoopError` opaquely rather than re-exporting it: a `winit` point
8/// release can add or remove variants on its error type without that being a breaking change for
9/// `winit` itself, but re-exporting it verbatim would make that a breaking change here too, since
10/// downstream code matching on it would stop compiling. Nothing in this crate's public API names
11/// a raw `winit` type as a result.
12///
13/// Use [`fmt::Display`] for a human-readable message, or
14/// [`std::error::Error::source`] to reach the underlying `winit` error for programmatic
15/// inspection (downcasting, logging its `Debug` form, etc.).
16#[derive(Debug)]
17pub struct EventLoopError(::winit::error::EventLoopError);
18
19impl fmt::Display for EventLoopError {
20    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
21        fmt::Display::fmt(&self.0, f)
22    }
23}
24
25impl std::error::Error for EventLoopError {
26    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
27        Some(&self.0)
28    }
29}
30
31impl From<::winit::error::EventLoopError> for EventLoopError {
32    fn from(err: ::winit::error::EventLoopError) -> Self {
33        Self(err)
34    }
35}
36
37#[cfg(test)]
38mod tests {
39    use super::EventLoopError;
40
41    // `RecreationAttempt` is the one raw `winit::error::EventLoopError` variant with no private
42    // fields, so it's the only one constructible outside `winit` itself.
43    fn recreation_attempt() -> EventLoopError {
44        EventLoopError::from(::winit::error::EventLoopError::RecreationAttempt)
45    }
46
47    #[test]
48    fn display_matches_the_wrapped_winit_error() {
49        assert_eq!(
50            recreation_attempt().to_string(),
51            ::winit::error::EventLoopError::RecreationAttempt.to_string()
52        );
53    }
54
55    #[test]
56    fn source_reaches_the_wrapped_winit_error() {
57        let err = recreation_attempt();
58        let source = std::error::Error::source(&err).expect("wraps a source");
59        assert_eq!(
60            source.to_string(),
61            ::winit::error::EventLoopError::RecreationAttempt.to_string()
62        );
63    }
64}