Skip to main content

molgfx_gpu/
device_loss.rs

1//! A lost device as a typed value rather than a message string.
2
3/// Why the backend reported the device lost.
4#[derive(Clone, Copy, Debug, PartialEq, Eq)]
5#[non_exhaustive]
6pub enum DeviceLossReason {
7    /// The device was destroyed on purpose, by the application or on teardown.
8    Destroyed,
9    /// The driver, adapter or browser dropped the device: a reset, a removed
10    /// adapter, a timeout or an unrecoverable fault.
11    Unknown,
12}
13
14impl DeviceLossReason {
15    /// A stable lowercase name for logs and the browser event.
16    #[must_use]
17    pub const fn as_str(self) -> &'static str {
18        match self {
19            Self::Destroyed => "destroyed",
20            Self::Unknown => "unknown",
21        }
22    }
23}
24
25/// The recorded cause of a device loss.
26#[derive(Clone, Debug, PartialEq, Eq)]
27pub struct DeviceLoss {
28    /// Classified reason.
29    pub reason: DeviceLossReason,
30    /// Backend-supplied detail, possibly empty.
31    pub message: String,
32}
33
34impl DeviceLoss {
35    /// Whether opening a fresh device and rebuilding every resource from the
36    /// scene is expected to succeed.
37    ///
38    /// A device the application destroyed is gone by intent; any other loss
39    /// leaves the adapter available to open again.
40    #[must_use]
41    pub const fn recoverable(&self) -> bool {
42        match self.reason {
43            DeviceLossReason::Destroyed => false,
44            DeviceLossReason::Unknown => true,
45        }
46    }
47}
48
49#[cfg(test)]
50#[path = "device_loss_tests.rs"]
51mod tests;