Relax Send and Sync bounds for wgpu::Primitive on Wasm

This commit is contained in:
Héctor Ramón Jiménez 2025-09-06 06:16:00 +02:00
parent 355f0e0921
commit ebe223cb5d
No known key found for this signature in database
GPG key ID: 7CC46565708259A7

View file

@ -1,6 +1,7 @@
//! Draw custom primitives. //! Draw custom primitives.
use crate::core::{self, Rectangle}; use crate::core::{self, Rectangle};
use crate::graphics::Viewport; use crate::graphics::Viewport;
use crate::graphics::futures::{MaybeSend, MaybeSync};
use rustc_hash::FxHashMap; use rustc_hash::FxHashMap;
use std::any::{Any, TypeId}; use std::any::{Any, TypeId};
@ -10,7 +11,7 @@ use std::fmt::Debug;
pub type Batch = Vec<Instance>; pub type Batch = Vec<Instance>;
/// A set of methods which allows a [`Primitive`] to be rendered. /// A set of methods which allows a [`Primitive`] to be rendered.
pub trait Primitive: Debug + Send + Sync + 'static { pub trait Primitive: Debug + MaybeSend + MaybeSync + 'static {
/// Processes the [`Primitive`], allowing for GPU buffer allocation. /// Processes the [`Primitive`], allowing for GPU buffer allocation.
fn prepare( fn prepare(
&self, &self,
@ -59,9 +60,10 @@ pub trait Renderer: core::Renderer {
} }
/// Stores custom, user-provided types. /// Stores custom, user-provided types.
#[derive(Default, Debug)] #[derive(Default)]
#[allow(missing_debug_implementations)]
pub struct Storage { pub struct Storage {
pipelines: FxHashMap<TypeId, Box<dyn Any + Send + Sync>>, pipelines: FxHashMap<TypeId, Box<dyn AnyConcurrent>>,
} }
impl Storage { impl Storage {
@ -71,14 +73,14 @@ impl Storage {
} }
/// Inserts the data `T` in to [`Storage`]. /// Inserts the data `T` in to [`Storage`].
pub fn store<T: 'static + Send + Sync>(&mut self, data: T) { pub fn store<T: 'static + MaybeSend + MaybeSync>(&mut self, data: T) {
let _ = self.pipelines.insert(TypeId::of::<T>(), Box::new(data)); let _ = self.pipelines.insert(TypeId::of::<T>(), Box::new(data));
} }
/// Returns a reference to the data with type `T` if it exists in [`Storage`]. /// Returns a reference to the data with type `T` if it exists in [`Storage`].
pub fn get<T: 'static>(&self) -> Option<&T> { pub fn get<T: 'static>(&self) -> Option<&T> {
self.pipelines.get(&TypeId::of::<T>()).map(|pipeline| { self.pipelines.get(&TypeId::of::<T>()).map(|pipeline| {
pipeline (pipeline.as_ref() as &dyn Any)
.downcast_ref::<T>() .downcast_ref::<T>()
.expect("Value with this type does not exist in Storage.") .expect("Value with this type does not exist in Storage.")
}) })
@ -87,9 +89,13 @@ impl Storage {
/// Returns a mutable reference to the data with type `T` if it exists in [`Storage`]. /// Returns a mutable reference to the data with type `T` if it exists in [`Storage`].
pub fn get_mut<T: 'static>(&mut self) -> Option<&mut T> { pub fn get_mut<T: 'static>(&mut self) -> Option<&mut T> {
self.pipelines.get_mut(&TypeId::of::<T>()).map(|pipeline| { self.pipelines.get_mut(&TypeId::of::<T>()).map(|pipeline| {
pipeline (pipeline.as_mut() as &mut dyn Any)
.downcast_mut::<T>() .downcast_mut::<T>()
.expect("Value with this type does not exist in Storage.") .expect("Value with this type does not exist in Storage.")
}) })
} }
} }
trait AnyConcurrent: Any + MaybeSend + MaybeSync {}
impl<T> AnyConcurrent for T where T: Any + MaybeSend + MaybeSync {}