naga/common/
diagnostic_debug.rs1#[cfg(any(feature = "wgsl-in", feature = "wgsl-out"))]
4use crate::common::wgsl::TypeContext;
5
6use crate::proc::TypeResolution;
7use crate::{Handle, Scalar, Type, TypeInner, UniqueArena};
8
9use core::fmt;
10
11pub struct DiagnosticDebug<T>(pub T);
20
21impl fmt::Debug for DiagnosticDebug<(Handle<Type>, &UniqueArena<Type>)> {
22    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
23        let (handle, ctx) = self.0;
24
25        #[cfg(any(feature = "wgsl-in", feature = "wgsl-out"))]
26        ctx.write_type(handle, f)?;
27
28        #[cfg(not(any(feature = "wgsl-in", feature = "wgsl-out")))]
29        {
30            let _ = ctx;
31            write!(f, "{handle:?}")?;
32        }
33
34        Ok(())
35    }
36}
37
38impl fmt::Debug for DiagnosticDebug<(&TypeInner, &UniqueArena<Type>)> {
39    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
40        let (inner, ctx) = self.0;
41
42        #[cfg(any(feature = "wgsl-in", feature = "wgsl-out"))]
43        ctx.write_type_inner(inner, f)?;
44
45        #[cfg(not(any(feature = "wgsl-in", feature = "wgsl-out")))]
46        {
47            let _ = ctx;
48            write!(f, "{inner:?}")?;
49        }
50
51        Ok(())
52    }
53}
54
55impl fmt::Debug for DiagnosticDebug<(&TypeResolution, &UniqueArena<Type>)> {
56    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
57        let (resolution, ctx) = self.0;
58
59        #[cfg(any(feature = "wgsl-in", feature = "wgsl-out"))]
60        ctx.write_type_resolution(resolution, f)?;
61
62        #[cfg(not(any(feature = "wgsl-in", feature = "wgsl-out")))]
63        {
64            let _ = ctx;
65            write!(f, "{resolution:?}")?;
66        }
67
68        Ok(())
69    }
70}
71
72impl fmt::Debug for DiagnosticDebug<Scalar> {
73    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
74        let scalar = self.0;
75
76        #[cfg(any(feature = "wgsl-in", feature = "wgsl-out"))]
77        f.write_str(&crate::common::wgsl::TryToWgsl::to_wgsl_for_diagnostics(
78            scalar,
79        ))?;
80
81        #[cfg(not(any(feature = "wgsl-in", feature = "wgsl-out")))]
82        write!(f, "{scalar:?}")?;
83
84        Ok(())
85    }
86}
87
88pub trait ForDebug: Sized {
89    fn for_debug(self) -> DiagnosticDebug<Self> {
99        DiagnosticDebug(self)
100    }
101}
102
103impl ForDebug for Scalar {}
104
105pub trait ForDebugWithTypes: Sized {
106    fn for_debug(self, types: &UniqueArena<Type>) -> DiagnosticDebug<(Self, &UniqueArena<Type>)> {
120        DiagnosticDebug((self, types))
121    }
122}
123
124impl ForDebugWithTypes for Handle<Type> {}
125impl ForDebugWithTypes for &TypeInner {}
126impl ForDebugWithTypes for &TypeResolution {}