Explicitly implement Send and Sync for all wgpu dispatch types. (#10177)

This commit is contained in:
Kevin Reid authored and GitHub committed 2026-08-29 17:46:33 -04:00
1 parent ef0a0a230f
commit 4292f44fcd
3 files changed
+137

No files matched your search

+4
View File
@@ -190,6 +190,10 @@ By @sagudev in [#10115](https://github.com/gfx-rs/wgpu/pull/10115).
- Recognize `GPUInternalError` when converting a WebGPU error, mapping it to `Error::Internal` instead of panicking with "Unexpected error". By @evilpies in [#9919](https://github.com/gfx-rs/wgpu/pull/9919).
- Fix `pop_error_scope` panics when it returns `null`. By @beicause in [#10039](https://github.com/gfx-rs/wgpu/pull/10039).
### Performance
- Added explicit `Send` and `Sync` implementations to key `wgpu` types so that the compiler can do less work checking those bounds. If you previously added a `#![recursion_limit = ...]` attribute to your crate due to overflow errors involving `wgpu` types, you may now be able to remove it. By @kpreid in [#10177](https://github.com/gfx-rs/wgpu/pull/10177).
### Documentation
#### General
+50
View File
@@ -670,6 +670,52 @@ pub trait BufferMappedRangeInterface: CommonTraits {
fn as_uint8array(&self) -> &js_sys::Uint8Array;
}
/// Generates a `Send` and `Sync` implementation for a dispatch enum generated by `dispatch_types!`.
macro_rules! explicit_send_sync_impl {
($name:ident) => {
/// Implement [`Send`] + [`Sync`] for the dispatch type, and check that all of its fields
/// are [`Send`] + [`Sync`] so that that implementation is sound.
///
/// This is identical to the “auto trait” implementation that Rust would provide, except
/// that it is eager rather than lazy: its requirements are checked now (in
/// `_fields_are_send_sync`), when this crate is compiled, rather than whenever a dependent
/// wants to know whether `$name: Send` holds.
///
/// This improves compilation performance and avoids a risk of dependents running into the
/// default [`recursion_limit`] when checking types containing wgpu API types. This risk
/// will become greater when Rust’s “next solver” is stabilized.
///
/// The effectiveness of this strategy is tested by
/// ../tests/send_sync_recursion.rs.
#[cfg(send_sync)]
const _: () = {
// SAFETY: Bounds checked below
unsafe impl Send for $name {}
// SAFETY: Bounds checked below
unsafe impl Sync for $name {}
/// This code will fail to compile if any field is not `Send + Sync`, or if a new field
/// is added to the dispatch type.
///
/// This technique is modeled after the macro library `non_structural_derive`, with
/// permission (see <https://github.com/fee1-dead/non_structural_derive/issues/1#issuecomment-5250905440>).
/// We only need it in this very narrow situation, so we can use a simpler macro.
fn _fields_are_send_sync(dispatch_enum: &$name) {
fn _check_bound<T: Send + Sync>(_: &T) {}
// Must dereference to handle the case where there are no enabled variants.
match *dispatch_enum {
#[cfg(wgpu_core)]
$name::Core(ref value) => _check_bound(value),
#[cfg(webgpu)]
$name::WebGPU(ref value) => _check_bound(value),
#[cfg(custom)]
$name::Custom(ref value) => _check_bound(value),
}
}
};
};
}
/// Generates a dispatch type for some `wgpu` API type.
///
/// Invocations of this macro take one of the following forms:
@@ -846,6 +892,8 @@ macro_rules! dispatch_types {
}
}
}
explicit_send_sync_impl!($name);
};
(
mut type $name:ident: $interface:ident = $core_type:ident,$webgpu_type:ident,$custom_type:ident
@@ -1012,6 +1060,8 @@ macro_rules! dispatch_types {
}
}
}
explicit_send_sync_impl!($name);
};
}
+83
View File
@@ -0,0 +1,83 @@
//! Tests that evaluating `wgpu::SomeType: Send + Sync` is cheap.
//!
//! This test must be located within the `wgpu` package so that it can use `cfg(send_sync)`.
//! It is a separate test target so that, if it fails to compile, it doesn’t block other builds.
//!
//! The implementations that supply the property this test is testing for are located in
//! `../src/dispatch.rs`, in the macro `explicit_send_sync_impl`.
#![cfg(send_sync)]
#![allow(clippy::type_complexity, clippy::explicit_auto_deref)]
/// 50 `Box`es that have to be looked through to evaluate auto traits.
///
/// This count is chosen so that if `wgpu` uses a significant amount of the default
/// recursion limit, this code will fail to compile.
#[rustfmt::skip]
struct Tower<T: ?Sized>(
Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<
Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<
Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<
Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<
Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<
T
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>
);
impl<T: ?Sized> core::ops::Deref for Tower<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
// We could let auto-deref handle this, but where’s the fun in that --
// er, I mean, rust-analyzer wouldn’t type check it properly.
&**************************************************self.0
}
}
const fn assert_send_sync_is_cheap<T>()
where
Tower<T>: Send,
Tower<T>: Sync,
{
}
const _: () = {
assert_send_sync_is_cheap::<wgpu::Adapter>();
assert_send_sync_is_cheap::<wgpu::BindGroup>();
assert_send_sync_is_cheap::<wgpu::BindGroupLayout>();
assert_send_sync_is_cheap::<wgpu::Blas>();
assert_send_sync_is_cheap::<wgpu::Buffer>();
assert_send_sync_is_cheap::<wgpu::BufferView>();
assert_send_sync_is_cheap::<wgpu::BufferViewMut>();
assert_send_sync_is_cheap::<wgpu::CommandBuffer>();
assert_send_sync_is_cheap::<wgpu::CommandEncoder>();
assert_send_sync_is_cheap::<wgpu::CompilationInfo>();
assert_send_sync_is_cheap::<wgpu::ComputePass>();
assert_send_sync_is_cheap::<wgpu::ComputePipeline>();
assert_send_sync_is_cheap::<wgpu::Device>();
assert_send_sync_is_cheap::<wgpu::Error>();
assert_send_sync_is_cheap::<wgpu::ExternalTexture>();
assert_send_sync_is_cheap::<wgpu::Features>();
assert_send_sync_is_cheap::<wgpu::Instance>();
assert_send_sync_is_cheap::<wgpu::Limits>();
assert_send_sync_is_cheap::<wgpu::PipelineCache>();
assert_send_sync_is_cheap::<wgpu::PipelineLayout>();
assert_send_sync_is_cheap::<wgpu::QuerySet>();
assert_send_sync_is_cheap::<wgpu::Queue>();
assert_send_sync_is_cheap::<wgpu::QueueWriteBufferView>();
assert_send_sync_is_cheap::<wgpu::RenderBundle>();
// RenderBundleEncoder is never Send or Sync
assert_send_sync_is_cheap::<wgpu::RenderPass>();
assert_send_sync_is_cheap::<wgpu::RenderPipeline>();
assert_send_sync_is_cheap::<wgpu::Sampler>();
assert_send_sync_is_cheap::<wgpu::ShaderModule>();
assert_send_sync_is_cheap::<wgpu::Surface>();
assert_send_sync_is_cheap::<wgpu::SurfaceTexture>();
assert_send_sync_is_cheap::<wgpu::Texture>();
assert_send_sync_is_cheap::<wgpu::TextureView>();
assert_send_sync_is_cheap::<wgpu::Tlas>();
};