use std::sync::Arc;
use crate::{ComputePass, Framebuffer, GraphicsContext, RenderPass, WindowContext};
pub trait AsWgpu {
type WgpuType;
fn as_wgpu(&self) -> &Self::WgpuType;
}
pub trait AsWgpuMut: AsWgpu {
fn as_wgpu_mut(&mut self) -> &mut Self::WgpuType;
}
pub trait IntoWgpu {
type WgpuType;
fn into_wgpu(self) -> Self::WgpuType;
}
impl AsWgpu for GraphicsContext {
type WgpuType = wgpu::Device;
fn as_wgpu(&self) -> &Self::WgpuType {
self.device()
}
}
impl AsWgpu for Arc<GraphicsContext> {
type WgpuType = wgpu::Device;
fn as_wgpu(&self) -> &Self::WgpuType {
self.device()
}
}
impl<'a> AsWgpu for RenderPass<'a> {
type WgpuType = wgpu::RenderPass<'static>;
fn as_wgpu(&self) -> &Self::WgpuType {
self.wgpu_pass_ref()
}
}
impl<'a> AsWgpuMut for RenderPass<'a> {
fn as_wgpu_mut(&mut self) -> &mut Self::WgpuType {
self.wgpu_pass()
}
}
impl<'a> AsWgpu for ComputePass<'a> {
type WgpuType = wgpu::ComputePass<'static>;
fn as_wgpu(&self) -> &Self::WgpuType {
self.wgpu_pass_ref()
}
}
impl<'a> AsWgpuMut for ComputePass<'a> {
fn as_wgpu_mut(&mut self) -> &mut Self::WgpuType {
self.wgpu_pass()
}
}
impl AsWgpu for Framebuffer {
type WgpuType = wgpu::Texture;
fn as_wgpu(&self) -> &Self::WgpuType {
self.color_texture()
}
}
impl AsWgpu for WindowContext {
type WgpuType = wgpu::Surface<'static>;
fn as_wgpu(&self) -> &Self::WgpuType {
&self.surface
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_trait_object_safety() {
fn _takes_as_wgpu<T: AsWgpu>(_: &T) {}
fn _takes_as_wgpu_mut<T: AsWgpuMut>(_: &mut T) {}
fn _takes_into_wgpu<T: IntoWgpu>(_: T) {}
}
}