Path: blob/main/crates/bevy_render/src/renderer/wgpu_wrapper.rs
6596 views
/// A wrapper to safely make `wgpu` types Send / Sync on web with atomics enabled.1///2/// On web with `atomics` enabled the inner value can only be accessed3/// or dropped on the `wgpu` thread or else a panic will occur.4/// On other platforms the wrapper simply contains the wrapped value.5#[derive(Debug, Clone)]6pub struct WgpuWrapper<T>(7#[cfg(not(all(target_arch = "wasm32", target_feature = "atomics")))] T,8#[cfg(all(target_arch = "wasm32", target_feature = "atomics"))] send_wrapper::SendWrapper<T>,9);1011// SAFETY: SendWrapper is always Send + Sync.12#[cfg(all(target_arch = "wasm32", target_feature = "atomics"))]13#[expect(unsafe_code, reason = "Blanket-impl Send requires unsafe.")]14unsafe impl<T> Send for WgpuWrapper<T> {}15#[cfg(all(target_arch = "wasm32", target_feature = "atomics"))]16#[expect(unsafe_code, reason = "Blanket-impl Sync requires unsafe.")]17unsafe impl<T> Sync for WgpuWrapper<T> {}1819impl<T> WgpuWrapper<T> {20/// Constructs a new instance of `WgpuWrapper` which will wrap the specified value.21pub fn new(t: T) -> Self {22#[cfg(not(all(target_arch = "wasm32", target_feature = "atomics")))]23return Self(t);24#[cfg(all(target_arch = "wasm32", target_feature = "atomics"))]25return Self(send_wrapper::SendWrapper::new(t));26}2728/// Unwraps the value.29pub fn into_inner(self) -> T {30#[cfg(not(all(target_arch = "wasm32", target_feature = "atomics")))]31return self.0;32#[cfg(all(target_arch = "wasm32", target_feature = "atomics"))]33return self.0.take();34}35}3637impl<T> core::ops::Deref for WgpuWrapper<T> {38type Target = T;3940fn deref(&self) -> &Self::Target {41&self.042}43}4445impl<T> core::ops::DerefMut for WgpuWrapper<T> {46fn deref_mut(&mut self) -> &mut Self::Target {47&mut self.048}49}505152