Skip to content

Commit 7c128a7

Browse files
chore: naively set warn(unsafe_op_in_unsafe_fn) for wgpu
Do the simplest mechanical work necessary to enable and satisfy this lint; only put down `unsafe` blocks, don't try to inspect for correctness or anything else.
1 parent 511c71f commit 7c128a7

File tree

2 files changed

+101
-73
lines changed

2 files changed

+101
-73
lines changed

wgpu/src/backend/direct.rs

Lines changed: 47 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -40,25 +40,26 @@ impl fmt::Debug for Context {
4040
impl Context {
4141
#[cfg(any(not(target_arch = "wasm32"), feature = "emscripten"))]
4242
pub unsafe fn from_hal_instance<A: wgc::hub::HalApi>(hal_instance: A::Instance) -> Self {
43-
Self(wgc::hub::Global::from_hal_instance::<A>(
44-
"wgpu",
45-
wgc::hub::IdentityManagerFactory,
46-
hal_instance,
47-
))
43+
Self(unsafe {
44+
wgc::hub::Global::from_hal_instance::<A>(
45+
"wgpu",
46+
wgc::hub::IdentityManagerFactory,
47+
hal_instance,
48+
)
49+
})
4850
}
4951

5052
/// # Safety
5153
///
5254
/// - The raw instance handle returned must not be manually destroyed.
5355
pub unsafe fn instance_as_hal<A: wgc::hub::HalApi>(&self) -> Option<&A::Instance> {
54-
self.0.instance_as_hal::<A>()
56+
unsafe { self.0.instance_as_hal::<A>() }
5557
}
5658

5759
pub unsafe fn from_core_instance(core_instance: wgc::instance::Instance) -> Self {
58-
Self(wgc::hub::Global::from_instance(
59-
wgc::hub::IdentityManagerFactory,
60-
core_instance,
61-
))
60+
Self(unsafe {
61+
wgc::hub::Global::from_instance(wgc::hub::IdentityManagerFactory, core_instance)
62+
})
6263
}
6364

6465
pub(crate) fn global(&self) -> &wgc::hub::Global<wgc::hub::IdentityManagerFactory> {
@@ -76,16 +77,18 @@ impl Context {
7677
&self,
7778
hal_adapter: hal::ExposedAdapter<A>,
7879
) -> wgc::id::AdapterId {
79-
self.0.create_adapter_from_hal(hal_adapter, ())
80+
unsafe { self.0.create_adapter_from_hal(hal_adapter, ()) }
8081
}
8182

8283
pub unsafe fn adapter_as_hal<A: wgc::hub::HalApi, F: FnOnce(Option<&A::Adapter>) -> R, R>(
8384
&self,
8485
adapter: wgc::id::AdapterId,
8586
hal_adapter_callback: F,
8687
) -> R {
87-
self.0
88-
.adapter_as_hal::<A, F, R>(adapter, hal_adapter_callback)
88+
unsafe {
89+
self.0
90+
.adapter_as_hal::<A, F, R>(adapter, hal_adapter_callback)
91+
}
8992
}
9093

9194
#[cfg(any(not(target_arch = "wasm32"), feature = "emscripten"))]
@@ -97,13 +100,15 @@ impl Context {
97100
trace_dir: Option<&std::path::Path>,
98101
) -> Result<(Device, Queue), crate::RequestDeviceError> {
99102
let global = &self.0;
100-
let (device_id, error) = global.create_device_from_hal(
101-
*adapter,
102-
hal_device,
103-
&desc.map_label(|l| l.map(Borrowed)),
104-
trace_dir,
105-
(),
106-
);
103+
let (device_id, error) = unsafe {
104+
global.create_device_from_hal(
105+
*adapter,
106+
hal_device,
107+
&desc.map_label(|l| l.map(Borrowed)),
108+
trace_dir,
109+
(),
110+
)
111+
};
107112
if let Some(err) = error {
108113
self.handle_error_fatal(err, "Adapter::create_device_from_hal");
109114
}
@@ -128,12 +133,14 @@ impl Context {
128133
desc: &TextureDescriptor,
129134
) -> Texture {
130135
let global = &self.0;
131-
let (id, error) = global.create_texture_from_hal::<A>(
132-
hal_texture,
133-
device.id,
134-
&desc.map_label(|l| l.map(Borrowed)),
135-
(),
136-
);
136+
let (id, error) = unsafe {
137+
global.create_texture_from_hal::<A>(
138+
hal_texture,
139+
device.id,
140+
&desc.map_label(|l| l.map(Borrowed)),
141+
(),
142+
)
143+
};
137144
if let Some(cause) = error {
138145
self.handle_error(
139146
&device.error_sink,
@@ -155,8 +162,10 @@ impl Context {
155162
device: &Device,
156163
hal_device_callback: F,
157164
) -> R {
158-
self.0
159-
.device_as_hal::<A, F, R>(device.id, hal_device_callback)
165+
unsafe {
166+
self.0
167+
.device_as_hal::<A, F, R>(device.id, hal_device_callback)
168+
}
160169
}
161170

162171
#[cfg(any(not(target_arch = "wasm32"), feature = "emscripten"))]
@@ -169,8 +178,10 @@ impl Context {
169178
surface: &Surface,
170179
hal_surface_callback: F,
171180
) -> R {
172-
self.0
173-
.surface_as_hal_mut::<A, F, R>(surface.id, hal_surface_callback)
181+
unsafe {
182+
self.0
183+
.surface_as_hal_mut::<A, F, R>(surface.id, hal_surface_callback)
184+
}
174185
}
175186

176187
#[cfg(any(not(target_arch = "wasm32"), feature = "emscripten"))]
@@ -179,8 +190,10 @@ impl Context {
179190
texture: &Texture,
180191
hal_texture_callback: F,
181192
) {
182-
self.0
183-
.texture_as_hal::<A, F>(texture.id, hal_texture_callback)
193+
unsafe {
194+
self.0
195+
.texture_as_hal::<A, F>(texture.id, hal_texture_callback)
196+
}
184197
}
185198

186199
#[cfg(any(not(target_arch = "wasm32"), feature = "emscripten"))]
@@ -232,7 +245,7 @@ impl Context {
232245
self: &Arc<Self>,
233246
visual: *mut std::ffi::c_void,
234247
) -> crate::Surface {
235-
let id = self.0.instance_create_surface_from_visual(visual, ());
248+
let id = unsafe { self.0.instance_create_surface_from_visual(visual, ()) };
236249
crate::Surface {
237250
context: Arc::clone(self),
238251
id: Surface {
@@ -1265,7 +1278,7 @@ impl crate::Context for Context {
12651278
label: desc.label.map(Borrowed),
12661279
// Doesn't matter the value since spirv shaders aren't mutated to include
12671280
// runtime checks
1268-
shader_bound_checks: wgt::ShaderBoundChecks::unchecked(),
1281+
shader_bound_checks: unsafe { wgt::ShaderBoundChecks::unchecked() },
12691282
};
12701283
let (id, error) = wgc::gfx_select!(
12711284
device.id => global.device_create_shader_module_spirv(device.id, &descriptor, Borrowed(&desc.source), ())

wgpu/src/lib.rs

Lines changed: 54 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
55
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
66
#![doc(html_logo_url = "https://raw.githubusercontent.com/gfx-rs/wgpu/master/logo.png")]
7-
#![warn(missing_docs)]
7+
#![warn(missing_docs, unsafe_op_in_unsafe_fn)]
88

99
mod backend;
1010
pub mod util;
@@ -1738,7 +1738,7 @@ impl Instance {
17381738
#[cfg(any(not(target_arch = "wasm32"), feature = "emscripten"))]
17391739
pub unsafe fn from_hal<A: wgc::hub::HalApi>(hal_instance: A::Instance) -> Self {
17401740
Self {
1741-
context: Arc::new(C::from_hal_instance::<A>(hal_instance)),
1741+
context: Arc::new(unsafe { C::from_hal_instance::<A>(hal_instance) }),
17421742
}
17431743
}
17441744

@@ -1754,7 +1754,7 @@ impl Instance {
17541754
/// [`Instance`]: hal::Api::Instance
17551755
#[cfg(any(not(target_arch = "wasm32"), feature = "webgl"))]
17561756
pub unsafe fn as_hal<A: wgc::hub::HalApi>(&self) -> Option<&A::Instance> {
1757-
self.context.instance_as_hal::<A>()
1757+
unsafe { self.context.instance_as_hal::<A>() }
17581758
}
17591759

17601760
/// Create an new instance of wgpu from a wgpu-core instance.
@@ -1769,7 +1769,7 @@ impl Instance {
17691769
#[cfg(any(not(target_arch = "wasm32"), feature = "webgl"))]
17701770
pub unsafe fn from_core(core_instance: wgc::instance::Instance) -> Self {
17711771
Self {
1772-
context: Arc::new(C::from_core_instance(core_instance)),
1772+
context: Arc::new(unsafe { C::from_core_instance(core_instance) }),
17731773
}
17741774
}
17751775

@@ -1815,7 +1815,7 @@ impl Instance {
18151815
hal_adapter: hal::ExposedAdapter<A>,
18161816
) -> Adapter {
18171817
let context = Arc::clone(&self.context);
1818-
let id = context.create_adapter_from_hal(hal_adapter);
1818+
let id = unsafe { context.create_adapter_from_hal(hal_adapter) };
18191819
Adapter { context, id }
18201820
}
18211821

@@ -1855,7 +1855,7 @@ impl Instance {
18551855
&self,
18561856
layer: *mut std::ffi::c_void,
18571857
) -> Surface {
1858-
self.context.create_surface_from_core_animation_layer(layer)
1858+
unsafe { self.context.create_surface_from_core_animation_layer(layer) }
18591859
}
18601860

18611861
/// Creates a surface from `IDCompositionVisual`.
@@ -1865,7 +1865,7 @@ impl Instance {
18651865
/// - visual must be a valid IDCompositionVisual to create a surface upon.
18661866
#[cfg(target_os = "windows")]
18671867
pub unsafe fn create_surface_from_visual(&self, visual: *mut std::ffi::c_void) -> Surface {
1868-
self.context.create_surface_from_visual(visual)
1868+
unsafe { self.context.create_surface_from_visual(visual) }
18691869
}
18701870

18711871
/// Creates a surface from a `web_sys::HtmlCanvasElement`.
@@ -1978,20 +1978,22 @@ impl Adapter {
19781978
trace_path: Option<&std::path::Path>,
19791979
) -> Result<(Device, Queue), RequestDeviceError> {
19801980
let context = Arc::clone(&self.context);
1981-
self.context
1982-
.create_device_from_hal(&self.id, hal_device, desc, trace_path)
1983-
.map(|(device_id, queue_id)| {
1984-
(
1985-
Device {
1986-
context: Arc::clone(&context),
1987-
id: device_id,
1988-
},
1989-
Queue {
1990-
context,
1991-
id: queue_id,
1992-
},
1993-
)
1994-
})
1981+
unsafe {
1982+
self.context
1983+
.create_device_from_hal(&self.id, hal_device, desc, trace_path)
1984+
}
1985+
.map(|(device_id, queue_id)| {
1986+
(
1987+
Device {
1988+
context: Arc::clone(&context),
1989+
id: device_id,
1990+
},
1991+
Queue {
1992+
context,
1993+
id: queue_id,
1994+
},
1995+
)
1996+
})
19951997
}
19961998

19971999
/// Apply a callback to this `Adapter`'s underlying backend adapter.
@@ -2018,8 +2020,10 @@ impl Adapter {
20182020
&self,
20192021
hal_adapter_callback: F,
20202022
) -> R {
2021-
self.context
2022-
.adapter_as_hal::<A, F, R>(self.id, hal_adapter_callback)
2023+
unsafe {
2024+
self.context
2025+
.adapter_as_hal::<A, F, R>(self.id, hal_adapter_callback)
2026+
}
20232027
}
20242028

20252029
/// Returns whether this adapter may present to the passed surface.
@@ -2119,12 +2123,14 @@ impl Device {
21192123
) -> ShaderModule {
21202124
ShaderModule {
21212125
context: Arc::clone(&self.context),
2122-
id: Context::device_create_shader_module(
2123-
&*self.context,
2124-
&self.id,
2125-
desc,
2126-
wgt::ShaderBoundChecks::unchecked(),
2127-
),
2126+
id: unsafe {
2127+
Context::device_create_shader_module(
2128+
&*self.context,
2129+
&self.id,
2130+
desc,
2131+
wgt::ShaderBoundChecks::unchecked(),
2132+
)
2133+
},
21282134
}
21292135
}
21302136

@@ -2142,7 +2148,9 @@ impl Device {
21422148
) -> ShaderModule {
21432149
ShaderModule {
21442150
context: Arc::clone(&self.context),
2145-
id: Context::device_create_shader_module_spirv(&*self.context, &self.id, desc),
2151+
id: unsafe {
2152+
Context::device_create_shader_module_spirv(&*self.context, &self.id, desc)
2153+
},
21462154
}
21472155
}
21482156

@@ -2252,9 +2260,10 @@ impl Device {
22522260
) -> Texture {
22532261
Texture {
22542262
context: Arc::clone(&self.context),
2255-
id: self
2256-
.context
2257-
.create_texture_from_hal::<A>(hal_texture, &self.id, desc),
2263+
id: unsafe {
2264+
self.context
2265+
.create_texture_from_hal::<A>(hal_texture, &self.id, desc)
2266+
},
22582267
owned: true,
22592268
}
22602269
}
@@ -2326,8 +2335,10 @@ impl Device {
23262335
&self,
23272336
hal_device_callback: F,
23282337
) -> R {
2329-
self.context
2330-
.device_as_hal::<A, F, R>(&self.id, hal_device_callback)
2338+
unsafe {
2339+
self.context
2340+
.device_as_hal::<A, F, R>(&self.id, hal_device_callback)
2341+
}
23312342
}
23322343
}
23332344

@@ -2637,8 +2648,10 @@ impl Texture {
26372648
&self,
26382649
hal_texture_callback: F,
26392650
) {
2640-
self.context
2641-
.texture_as_hal::<A, F>(&self.id, hal_texture_callback)
2651+
unsafe {
2652+
self.context
2653+
.texture_as_hal::<A, F>(&self.id, hal_texture_callback)
2654+
}
26422655
}
26432656

26442657
/// Creates a view of this texture.
@@ -3795,8 +3808,10 @@ impl Surface {
37953808
&mut self,
37963809
hal_surface_callback: F,
37973810
) -> R {
3798-
self.context
3799-
.surface_as_hal_mut::<A, F, R>(&self.id, hal_surface_callback)
3811+
unsafe {
3812+
self.context
3813+
.surface_as_hal_mut::<A, F, R>(&self.id, hal_surface_callback)
3814+
}
38003815
}
38013816
}
38023817

0 commit comments

Comments
 (0)