Skip to content

Commit 1503796

Browse files
refactor(hal): Remove unnecessary unsafe Send/Sync impls (gfx-rs#9580)
1 parent dd63d22 commit 1503796

8 files changed

Lines changed: 36 additions & 34 deletions

File tree

wgpu-hal/src/dx12/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1128,8 +1128,8 @@ pub struct Sampler {
11281128

11291129
impl crate::DynSampler for Sampler {}
11301130

1131-
unsafe impl Send for Sampler {}
1132-
unsafe impl Sync for Sampler {}
1131+
#[cfg(send_sync)]
1132+
static_assertions::assert_impl_all!(Sampler: Send, Sync);
11331133

11341134
#[derive(Debug)]
11351135
pub struct QuerySet {

wgpu-hal/src/gles/egl.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,11 @@ struct Inner {
344344
srgb_kind: SrgbFrameBufferKind,
345345
}
346346

347+
#[cfg(send_sync)]
348+
unsafe impl Send for Inner {}
349+
#[cfg(send_sync)]
350+
unsafe impl Sync for Inner {}
351+
347352
// Different calls to `eglGetPlatformDisplay` may return the same `Display`, making it a global
348353
// state of all our `EglContext`s. This forces us to track the number of such context to prevent
349354
// terminating the display if it's currently used by another `EglContext`.
@@ -707,8 +712,8 @@ impl Instance {
707712
}
708713
}
709714

710-
unsafe impl Send for Instance {}
711-
unsafe impl Sync for Instance {}
715+
#[cfg(send_sync)]
716+
static_assertions::assert_impl_all!(Instance: Send, Sync);
712717

713718
impl crate::Instance for Instance {
714719
type A = super::Api;

wgpu-hal/src/gles/mod.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,11 @@ struct PipelineInner {
692692
clip_distance_count: u32,
693693
}
694694

695+
#[cfg(send_sync)]
696+
unsafe impl Sync for PipelineInner {}
697+
#[cfg(send_sync)]
698+
unsafe impl Send for PipelineInner {}
699+
695700
#[derive(Clone, Debug)]
696701
struct DepthState {
697702
function: u32,
@@ -750,9 +755,7 @@ pub struct RenderPipeline {
750755
impl crate::DynRenderPipeline for RenderPipeline {}
751756

752757
#[cfg(send_sync)]
753-
unsafe impl Sync for RenderPipeline {}
754-
#[cfg(send_sync)]
755-
unsafe impl Send for RenderPipeline {}
758+
static_assertions::assert_impl_all!(RenderPipeline: Send, Sync);
756759

757760
#[derive(Debug)]
758761
pub struct ComputePipeline {
@@ -762,9 +765,7 @@ pub struct ComputePipeline {
762765
impl crate::DynComputePipeline for ComputePipeline {}
763766

764767
#[cfg(send_sync)]
765-
unsafe impl Sync for ComputePipeline {}
766-
#[cfg(send_sync)]
767-
unsafe impl Send for ComputePipeline {}
768+
static_assertions::assert_impl_all!(ComputePipeline: Send, Sync);
768769

769770
#[derive(Debug)]
770771
pub struct QuerySet {

wgpu-hal/src/gles/web.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,7 @@ impl Instance {
107107
}
108108

109109
#[cfg(send_sync)]
110-
unsafe impl Sync for Instance {}
111-
#[cfg(send_sync)]
112-
unsafe impl Send for Instance {}
110+
static_assertions::assert_impl_all!(Instance: Send, Sync);
113111

114112
impl crate::Instance for Instance {
115113
type A = super::Api;

wgpu-hal/src/gles/wgl.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ pub struct AdapterContext {
4141
inner: Arc<Mutex<Inner>>,
4242
}
4343

44-
unsafe impl Sync for AdapterContext {}
45-
unsafe impl Send for AdapterContext {}
44+
#[cfg(send_sync)]
45+
static_assertions::assert_impl_all!(AdapterContext: Send, Sync);
4646

4747
impl AdapterContext {
4848
pub fn is_owned(&self) -> bool {
@@ -182,8 +182,8 @@ pub struct Instance {
182182
inner: Arc<Mutex<Inner>>,
183183
}
184184

185-
unsafe impl Send for Instance {}
186-
unsafe impl Sync for Instance {}
185+
#[cfg(send_sync)]
186+
static_assertions::assert_impl_all!(Instance: Send, Sync);
187187

188188
fn load_gl_func(name: &str, module: Option<Foundation::HMODULE>) -> *const c_void {
189189
let addr = CString::new(name.as_bytes()).unwrap();

wgpu-hal/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,6 @@
219219
clippy::single_match,
220220
// Push commands are more regular than macros.
221221
clippy::vec_init_then_push,
222-
// We unsafe impl `Send` for a reason.
223-
clippy::non_send_fields_in_send_ty,
224222
// TODO!
225223
clippy::missing_safety_doc,
226224
// It gets in the way a lot and does not prevent bugs in practice.

wgpu-hal/src/metal/adapter.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,6 @@ const MAX_ACCELERATION_STRUCTURES_PER_SHADER_STAGE: u32 = 1;
6262
// Use the end of the range for vertex buffers.
6363
pub const VERTEX_BUFFER_SLOT_START: u32 = 31 - 8;
6464

65-
unsafe impl Send for super::Adapter {}
66-
unsafe impl Sync for super::Adapter {}
67-
6865
impl super::Adapter {
6966
pub(super) fn new(shared: Arc<super::AdapterShared>) -> Self {
7067
Self { shared }

wgpu-hal/src/metal/mod.rs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -392,8 +392,8 @@ struct AdapterShared {
392392
presentation_timer: time::PresentationTimer,
393393
}
394394

395-
unsafe impl Send for AdapterShared {}
396-
unsafe impl Sync for AdapterShared {}
395+
#[cfg(send_sync)]
396+
static_assertions::assert_impl_all!(AdapterShared: Send, Sync);
397397

398398
impl AdapterShared {
399399
fn new(
@@ -446,13 +446,16 @@ pub struct Adapter {
446446
shared: Arc<AdapterShared>,
447447
}
448448

449+
#[cfg(send_sync)]
450+
static_assertions::assert_impl_all!(Adapter: Send, Sync);
451+
449452
pub struct Queue {
450453
shared: Arc<QueueShared>,
451454
timestamp_period: f32,
452455
}
453456

454-
unsafe impl Send for Queue {}
455-
unsafe impl Sync for Queue {}
457+
#[cfg(send_sync)]
458+
static_assertions::assert_impl_all!(Queue: Send, Sync);
456459

457460
impl Queue {
458461
pub unsafe fn queue_from_raw(
@@ -703,8 +706,8 @@ pub struct Sampler {
703706

704707
impl crate::DynSampler for Sampler {}
705708

706-
unsafe impl Send for Sampler {}
707-
unsafe impl Sync for Sampler {}
709+
#[cfg(send_sync)]
710+
static_assertions::assert_impl_all!(Sampler: Send, Sync);
708711

709712
impl Sampler {
710713
fn as_raw(&self) -> NonNull<ProtocolObject<dyn MTLSamplerState>> {
@@ -887,8 +890,8 @@ pub struct PassthroughShader {
887890
pub num_workgroups: HashMap<String, (u32, u32, u32)>,
888891
}
889892

890-
unsafe impl Send for PassthroughShader {}
891-
unsafe impl Sync for PassthroughShader {}
893+
#[cfg(send_sync)]
894+
static_assertions::assert_impl_all!(PassthroughShader: Send, Sync);
892895

893896
#[derive(Debug)]
894897
pub struct ShaderModule {
@@ -993,8 +996,8 @@ pub struct RenderPipeline {
993996
)>,
994997
}
995998

996-
unsafe impl Send for RenderPipeline {}
997-
unsafe impl Sync for RenderPipeline {}
999+
#[cfg(send_sync)]
1000+
static_assertions::assert_impl_all!(RenderPipeline: Send, Sync);
9981001

9991002
impl crate::DynRenderPipeline for RenderPipeline {}
10001003

@@ -1004,8 +1007,8 @@ pub struct ComputePipeline {
10041007
cs_info: PipelineStageInfo,
10051008
}
10061009

1007-
unsafe impl Send for ComputePipeline {}
1008-
unsafe impl Sync for ComputePipeline {}
1010+
#[cfg(send_sync)]
1011+
static_assertions::assert_impl_all!(ComputePipeline: Send, Sync);
10091012

10101013
impl crate::DynComputePipeline for ComputePipeline {}
10111014

0 commit comments

Comments
 (0)