Skip to content

Commit 0e20ecb

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 797632e commit 0e20ecb

File tree

2 files changed

+93
-69
lines changed

2 files changed

+93
-69
lines changed

wgpu/src/backend/direct.rs

Lines changed: 43 additions & 32 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, wgc::id::QueueId), 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
}
@@ -123,12 +128,14 @@ impl Context {
123128
desc: &TextureDescriptor,
124129
) -> Texture {
125130
let global = &self.0;
126-
let (id, error) = global.create_texture_from_hal::<A>(
127-
hal_texture,
128-
device.id,
129-
&desc.map_label(|l| l.map(Borrowed)),
130-
(),
131-
);
131+
let (id, error) = unsafe {
132+
global.create_texture_from_hal::<A>(
133+
hal_texture,
134+
device.id,
135+
&desc.map_label(|l| l.map(Borrowed)),
136+
(),
137+
)
138+
};
132139
if let Some(cause) = error {
133140
self.handle_error(
134141
&device.error_sink,
@@ -150,8 +157,10 @@ impl Context {
150157
device: &Device,
151158
hal_device_callback: F,
152159
) -> R {
153-
self.0
154-
.device_as_hal::<A, F, R>(device.id, hal_device_callback)
160+
unsafe {
161+
self.0
162+
.device_as_hal::<A, F, R>(device.id, hal_device_callback)
163+
}
155164
}
156165

157166
#[cfg(any(not(target_arch = "wasm32"), feature = "emscripten"))]
@@ -160,8 +169,10 @@ impl Context {
160169
texture: &Texture,
161170
hal_texture_callback: F,
162171
) {
163-
self.0
164-
.texture_as_hal::<A, F>(texture.id, hal_texture_callback)
172+
unsafe {
173+
self.0
174+
.texture_as_hal::<A, F>(texture.id, hal_texture_callback)
175+
}
165176
}
166177

167178
#[cfg(any(not(target_arch = "wasm32"), feature = "emscripten"))]
@@ -213,7 +224,7 @@ impl Context {
213224
self: &Arc<Self>,
214225
visual: *mut std::ffi::c_void,
215226
) -> crate::Surface {
216-
let id = self.0.instance_create_surface_from_visual(visual, ());
227+
let id = unsafe { self.0.instance_create_surface_from_visual(visual, ()) };
217228
crate::Surface {
218229
context: Arc::clone(self),
219230
id: Surface {
@@ -1174,7 +1185,7 @@ impl crate::Context for Context {
11741185
label: desc.label.map(Borrowed),
11751186
// Doesn't matter the value since spirv shaders aren't mutated to include
11761187
// runtime checks
1177-
shader_bound_checks: wgt::ShaderBoundChecks::unchecked(),
1188+
shader_bound_checks: unsafe { wgt::ShaderBoundChecks::unchecked() },
11781189
};
11791190
let (id, error) = wgc::gfx_select!(
11801191
device.id => global.device_create_shader_module_spirv(device.id, &descriptor, Borrowed(&desc.source), ())

wgpu/src/lib.rs

Lines changed: 50 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
55
#![cfg_attr(docsrs, feature(doc_cfg))] // Allow doc(cfg(feature = "")) for showing in docs that something is feature gated.
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;
@@ -1736,7 +1736,7 @@ impl Instance {
17361736
#[cfg(any(not(target_arch = "wasm32"), feature = "emscripten"))]
17371737
pub unsafe fn from_hal<A: wgc::hub::HalApi>(hal_instance: A::Instance) -> Self {
17381738
Self {
1739-
context: Arc::new(C::from_hal_instance::<A>(hal_instance)),
1739+
context: Arc::new(unsafe { C::from_hal_instance::<A>(hal_instance) }),
17401740
}
17411741
}
17421742

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

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

@@ -1813,7 +1813,7 @@ impl Instance {
18131813
hal_adapter: hal::ExposedAdapter<A>,
18141814
) -> Adapter {
18151815
let context = Arc::clone(&self.context);
1816-
let id = context.create_adapter_from_hal(hal_adapter);
1816+
let id = unsafe { context.create_adapter_from_hal(hal_adapter) };
18171817
Adapter { context, id }
18181818
}
18191819

@@ -1853,7 +1853,7 @@ impl Instance {
18531853
&self,
18541854
layer: *mut std::ffi::c_void,
18551855
) -> Surface {
1856-
self.context.create_surface_from_core_animation_layer(layer)
1856+
unsafe { self.context.create_surface_from_core_animation_layer(layer) }
18571857
}
18581858

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

18691869
/// Creates a surface from a `web_sys::HtmlCanvasElement`.
@@ -1976,20 +1976,22 @@ impl Adapter {
19761976
trace_path: Option<&std::path::Path>,
19771977
) -> Result<(Device, Queue), RequestDeviceError> {
19781978
let context = Arc::clone(&self.context);
1979-
self.context
1980-
.create_device_from_hal(&self.id, hal_device, desc, trace_path)
1981-
.map(|(device_id, queue_id)| {
1982-
(
1983-
Device {
1984-
context: Arc::clone(&context),
1985-
id: device_id,
1986-
},
1987-
Queue {
1988-
context,
1989-
id: queue_id,
1990-
},
1991-
)
1992-
})
1979+
unsafe {
1980+
self.context
1981+
.create_device_from_hal(&self.id, hal_device, desc, trace_path)
1982+
}
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+
})
19931995
}
19941996

19951997
/// Apply a callback to this `Adapter`'s underlying backend adapter.
@@ -2016,8 +2018,10 @@ impl Adapter {
20162018
&self,
20172019
hal_adapter_callback: F,
20182020
) -> R {
2019-
self.context
2020-
.adapter_as_hal::<A, F, R>(self.id, hal_adapter_callback)
2021+
unsafe {
2022+
self.context
2023+
.adapter_as_hal::<A, F, R>(self.id, hal_adapter_callback)
2024+
}
20212025
}
20222026

20232027
/// Returns whether this adapter may present to the passed surface.
@@ -2117,12 +2121,14 @@ impl Device {
21172121
) -> ShaderModule {
21182122
ShaderModule {
21192123
context: Arc::clone(&self.context),
2120-
id: Context::device_create_shader_module(
2121-
&*self.context,
2122-
&self.id,
2123-
desc,
2124-
wgt::ShaderBoundChecks::unchecked(),
2125-
),
2124+
id: unsafe {
2125+
Context::device_create_shader_module(
2126+
&*self.context,
2127+
&self.id,
2128+
desc,
2129+
wgt::ShaderBoundChecks::unchecked(),
2130+
)
2131+
},
21262132
}
21272133
}
21282134

@@ -2140,7 +2146,9 @@ impl Device {
21402146
) -> ShaderModule {
21412147
ShaderModule {
21422148
context: Arc::clone(&self.context),
2143-
id: Context::device_create_shader_module_spirv(&*self.context, &self.id, desc),
2149+
id: unsafe {
2150+
Context::device_create_shader_module_spirv(&*self.context, &self.id, desc)
2151+
},
21442152
}
21452153
}
21462154

@@ -2250,9 +2258,10 @@ impl Device {
22502258
) -> Texture {
22512259
Texture {
22522260
context: Arc::clone(&self.context),
2253-
id: self
2254-
.context
2255-
.create_texture_from_hal::<A>(hal_texture, &self.id, desc),
2261+
id: unsafe {
2262+
self.context
2263+
.create_texture_from_hal::<A>(hal_texture, &self.id, desc)
2264+
},
22562265
owned: true,
22572266
}
22582267
}
@@ -2324,8 +2333,10 @@ impl Device {
23242333
&self,
23252334
hal_device_callback: F,
23262335
) -> R {
2327-
self.context
2328-
.device_as_hal::<A, F, R>(&self.id, hal_device_callback)
2336+
unsafe {
2337+
self.context
2338+
.device_as_hal::<A, F, R>(&self.id, hal_device_callback)
2339+
}
23292340
}
23302341
}
23312342

@@ -2635,8 +2646,10 @@ impl Texture {
26352646
&self,
26362647
hal_texture_callback: F,
26372648
) {
2638-
self.context
2639-
.texture_as_hal::<A, F>(&self.id, hal_texture_callback)
2649+
unsafe {
2650+
self.context
2651+
.texture_as_hal::<A, F>(&self.id, hal_texture_callback)
2652+
}
26402653
}
26412654

26422655
/// Creates a view of this texture.

0 commit comments

Comments
 (0)