wgpu_hal/
validation_canary.rs

1use alloc::{string::String, vec::Vec};
2
3use wgpu_sync::Mutex;
4
5/// Stores the text of any validation errors that have occurred since
6/// the last call to `get_and_reset`.
7///
8/// Each value is a validation error and a message associated with it,
9/// or `None` if the error has no message from the api.
10///
11/// This is used for internal wgpu testing only and _must not_ be used
12/// as a way to check for errors.
13///
14/// This works as a static because `cargo nextest` runs all of our
15/// tests in separate processes, so each test gets its own canary.
16///
17/// This prevents the issue of one validation error terminating the
18/// entire process.
19pub static VALIDATION_CANARY: ValidationCanary = ValidationCanary {
20    // Mutex::new is not const for the minimum version of lock_api used.
21    inner: Mutex::const_new(wgpu_sync::RawMutex::new(), Vec::new()),
22};
23
24/// Flag for internal testing.
25#[derive(Debug)]
26pub struct ValidationCanary {
27    inner: Mutex<Vec<String>>,
28}
29
30impl ValidationCanary {
31    #[allow(dead_code, reason = "in some configurations this function is dead")]
32    pub(crate) fn add(&self, msg: String) {
33        self.inner.lock().push(msg);
34    }
35
36    /// Returns any API validation errors that have occurred in this process
37    /// since the last call to this function.
38    pub fn get_and_reset(&self) -> Vec<String> {
39        self.inner.lock().drain(..).collect()
40    }
41}