astrelis_render/
extension.rs1use std::sync::Arc;
28
29use crate::{
30 ComputePass, FrameContext, Framebuffer, GraphicsContext, RenderPass, WindowContext,
31};
32
33pub trait AsWgpu {
41 type WgpuType;
43
44 fn as_wgpu(&self) -> &Self::WgpuType;
46}
47
48pub trait AsWgpuMut: AsWgpu {
52 fn as_wgpu_mut(&mut self) -> &mut Self::WgpuType;
54}
55
56pub trait IntoWgpu {
60 type WgpuType;
62
63 fn into_wgpu(self) -> Self::WgpuType;
65}
66
67impl AsWgpu for GraphicsContext {
72 type WgpuType = wgpu::Device;
73
74 fn as_wgpu(&self) -> &Self::WgpuType {
75 self.device()
76 }
77}
78
79impl AsWgpu for Arc<GraphicsContext> {
80 type WgpuType = wgpu::Device;
81
82 fn as_wgpu(&self) -> &Self::WgpuType {
83 self.device()
84 }
85}
86
87impl AsWgpu for FrameContext {
88 type WgpuType = wgpu::CommandEncoder;
89
90 fn as_wgpu(&self) -> &Self::WgpuType {
91 self.encoder.as_ref().expect("FrameContext encoder already taken - ensure finish() wasn't called early")
92 }
93}
94
95impl AsWgpuMut for FrameContext {
96 fn as_wgpu_mut(&mut self) -> &mut Self::WgpuType {
97 self.encoder.as_mut().expect("FrameContext encoder already taken - ensure finish() wasn't called early")
98 }
99}
100
101impl<'a> AsWgpu for RenderPass<'a> {
102 type WgpuType = wgpu::RenderPass<'static>;
103
104 fn as_wgpu(&self) -> &Self::WgpuType {
105 self.pass.as_ref().expect("RenderPass already consumed - ensure it wasn't dropped early")
106 }
107}
108
109impl<'a> AsWgpuMut for RenderPass<'a> {
110 fn as_wgpu_mut(&mut self) -> &mut Self::WgpuType {
111 self.pass.as_mut().expect("RenderPass already consumed - ensure it wasn't dropped early")
112 }
113}
114
115impl<'a> AsWgpu for ComputePass<'a> {
116 type WgpuType = wgpu::ComputePass<'static>;
117
118 fn as_wgpu(&self) -> &Self::WgpuType {
119 self.pass.as_ref().expect("ComputePass already consumed - ensure it wasn't dropped early")
120 }
121}
122
123impl<'a> AsWgpuMut for ComputePass<'a> {
124 fn as_wgpu_mut(&mut self) -> &mut Self::WgpuType {
125 self.pass.as_mut().expect("ComputePass already consumed - ensure it wasn't dropped early")
126 }
127}
128
129impl AsWgpu for Framebuffer {
130 type WgpuType = wgpu::Texture;
131
132 fn as_wgpu(&self) -> &Self::WgpuType {
133 self.color_texture()
134 }
135}
136
137impl AsWgpu for WindowContext {
138 type WgpuType = wgpu::Surface<'static>;
139
140 fn as_wgpu(&self) -> &Self::WgpuType {
141 &self.surface
142 }
143}
144
145#[cfg(test)]
146mod tests {
147 use super::*;
148
149 #[test]
153 fn test_trait_object_safety() {
154 fn _takes_as_wgpu<T: AsWgpu>(_: &T) {}
156 fn _takes_as_wgpu_mut<T: AsWgpuMut>(_: &mut T) {}
157 fn _takes_into_wgpu<T: IntoWgpu>(_: T) {}
158 }
159}