oxiui_compute_wgpu/
error.rs1#[derive(Debug)]
12pub enum ComputeError {
13 NoAdapter,
17
18 DeviceRequest(String),
22
23 OutOfMemory,
26
27 ShaderCompilation(String),
32
33 Operation {
38 op: &'static str,
40 detail: String,
42 },
43}
44
45impl std::fmt::Display for ComputeError {
46 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
47 match self {
48 Self::NoAdapter => write!(f, "no suitable GPU adapter found"),
49 Self::DeviceRequest(e) => write!(f, "device request failed: {e}"),
50 Self::OutOfMemory => write!(f, "GPU out of memory"),
51 Self::ShaderCompilation(msg) => write!(f, "WGSL compilation error: {msg}"),
52 Self::Operation { op, detail } => {
53 write!(f, "compute operation '{op}' failed: {detail}")
54 }
55 }
56 }
57}
58
59impl std::error::Error for ComputeError {}
60
61#[cfg(test)]
64mod tests {
65 use super::*;
66
67 #[test]
68 fn no_adapter_display() {
69 let e = ComputeError::NoAdapter;
70 let s = e.to_string();
71 assert!(s.contains("no suitable GPU adapter"), "got: {s}");
72 }
73
74 #[test]
75 fn device_request_display() {
76 let e = ComputeError::DeviceRequest("timeout".to_string());
77 let s = e.to_string();
78 assert!(s.contains("device request failed"), "got: {s}");
79 assert!(s.contains("timeout"), "got: {s}");
80 }
81
82 #[test]
83 fn display_out_of_memory() {
84 let e = ComputeError::OutOfMemory;
85 let s = e.to_string();
86 assert!(s.contains("out of memory"), "got: {s}");
87 }
88
89 #[test]
90 fn display_shader_compilation() {
91 let e = ComputeError::ShaderCompilation("line 3, col 5: unknown identifier".to_string());
92 let s = e.to_string();
93 assert!(s.contains("WGSL compilation error"), "got: {s}");
94 assert!(s.contains("unknown identifier"), "got: {s}");
95 }
96
97 #[test]
98 fn display_operation() {
99 let e = ComputeError::Operation {
100 op: "read_back",
101 detail: "buffer label=staging size=4096".to_string(),
102 };
103 let s = e.to_string();
104 assert!(s.contains("read_back"), "got: {s}");
105 assert!(s.contains("staging"), "got: {s}");
106 }
107
108 #[test]
109 fn compute_error_is_std_error() {
110 fn assert_error<E: std::error::Error>(_: &E) {}
111 assert_error(&ComputeError::NoAdapter);
112 assert_error(&ComputeError::DeviceRequest("x".to_string()));
113 assert_error(&ComputeError::OutOfMemory);
114 assert_error(&ComputeError::ShaderCompilation("msg".to_string()));
115 assert_error(&ComputeError::Operation {
116 op: "dispatch",
117 detail: "x".to_string(),
118 });
119 }
120}