Skip to content

Commit e68d7b4

Browse files
chore: naively set warn(unsafe_ops_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 3dc12fd commit e68d7b4

File tree

2 files changed

+92
-68
lines changed

2 files changed

+92
-68
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 {
@@ -1163,7 +1174,7 @@ impl crate::Context for Context {
11631174
label: desc.label.map(Borrowed),
11641175
// Doesn't matter the value since spirv shaders aren't mutated to include
11651176
// runtime checks
1166-
shader_bound_checks: wgt::ShaderBoundChecks::unchecked(),
1177+
shader_bound_checks: unsafe { wgt::ShaderBoundChecks::unchecked() },
11671178
};
11681179
let (id, error) = wgc::gfx_select!(
11691180
device.id => global.device_create_shader_module_spirv(device.id, &descriptor, Borrowed(&desc.source), ())

wgpu/src/lib.rs

Lines changed: 49 additions & 36 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;
@@ -1730,7 +1730,7 @@ impl Instance {
17301730
#[cfg(any(not(target_arch = "wasm32"), feature = "emscripten"))]
17311731
pub unsafe fn from_hal<A: wgc::hub::HalApi>(hal_instance: A::Instance) -> Self {
17321732
Self {
1733-
context: Arc::new(C::from_hal_instance::<A>(hal_instance)),
1733+
context: Arc::new(unsafe { C::from_hal_instance::<A>(hal_instance) }),
17341734
}
17351735
}
17361736

@@ -1746,7 +1746,7 @@ impl Instance {
17461746
/// [`Instance`]: hal::Api::Instance
17471747
#[cfg(any(not(target_arch = "wasm32"), feature = "webgl"))]
17481748
pub unsafe fn as_hal<A: wgc::hub::HalApi>(&self) -> Option<&A::Instance> {
1749-
self.context.instance_as_hal::<A>()
1749+
unsafe { self.context.instance_as_hal::<A>() }
17501750
}
17511751

17521752
/// Create an new instance of wgpu from a wgpu-core instance.
@@ -1761,7 +1761,7 @@ impl Instance {
17611761
#[cfg(any(not(target_arch = "wasm32"), feature = "webgl"))]
17621762
pub unsafe fn from_core(core_instance: wgc::instance::Instance) -> Self {
17631763
Self {
1764-
context: Arc::new(C::from_core_instance(core_instance)),
1764+
context: Arc::new(unsafe { C::from_core_instance(core_instance) }),
17651765
}
17661766
}
17671767

@@ -1807,7 +1807,7 @@ impl Instance {
18071807
hal_adapter: hal::ExposedAdapter<A>,
18081808
) -> Adapter {
18091809
let context = Arc::clone(&self.context);
1810-
let id = context.create_adapter_from_hal(hal_adapter);
1810+
let id = unsafe { context.create_adapter_from_hal(hal_adapter) };
18111811
Adapter { context, id }
18121812
}
18131813

@@ -1854,7 +1854,7 @@ impl Instance {
18541854
/// - visual must be a valid IDCompositionVisual to create a surface upon.
18551855
#[cfg(target_os = "windows")]
18561856
pub unsafe fn create_surface_from_visual(&self, visual: *mut std::ffi::c_void) -> Surface {
1857-
self.context.create_surface_from_visual(visual)
1857+
unsafe { self.context.create_surface_from_visual(visual) }
18581858
}
18591859

18601860
/// Creates a surface from a `web_sys::HtmlCanvasElement`.
@@ -1967,20 +1967,22 @@ impl Adapter {
19671967
trace_path: Option<&std::path::Path>,
19681968
) -> Result<(Device, Queue), RequestDeviceError> {
19691969
let context = Arc::clone(&self.context);
1970-
self.context
1971-
.create_device_from_hal(&self.id, hal_device, desc, trace_path)
1972-
.map(|(device_id, queue_id)| {
1973-
(
1974-
Device {
1975-
context: Arc::clone(&context),
1976-
id: device_id,
1977-
},
1978-
Queue {
1979-
context,
1980-
id: queue_id,
1981-
},
1982-
)
1983-
})
1970+
unsafe {
1971+
self.context
1972+
.create_device_from_hal(&self.id, hal_device, desc, trace_path)
1973+
}
1974+
.map(|(device_id, queue_id)| {
1975+
(
1976+
Device {
1977+
context: Arc::clone(&context),
1978+
id: device_id,
1979+
},
1980+
Queue {
1981+
context,
1982+
id: queue_id,
1983+
},
1984+
)
1985+
})
19841986
}
19851987

19861988
/// Apply a callback to this `Adapter`'s underlying backend adapter.
@@ -2007,8 +2009,10 @@ impl Adapter {
20072009
&self,
20082010
hal_adapter_callback: F,
20092011
) -> R {
2010-
self.context
2011-
.adapter_as_hal::<A, F, R>(self.id, hal_adapter_callback)
2012+
unsafe {
2013+
self.context
2014+
.adapter_as_hal::<A, F, R>(self.id, hal_adapter_callback)
2015+
}
20122016
}
20132017

20142018
/// Returns whether this adapter may present to the passed surface.
@@ -2108,12 +2112,14 @@ impl Device {
21082112
) -> ShaderModule {
21092113
ShaderModule {
21102114
context: Arc::clone(&self.context),
2111-
id: Context::device_create_shader_module(
2112-
&*self.context,
2113-
&self.id,
2114-
desc,
2115-
wgt::ShaderBoundChecks::unchecked(),
2116-
),
2115+
id: unsafe {
2116+
Context::device_create_shader_module(
2117+
&*self.context,
2118+
&self.id,
2119+
desc,
2120+
wgt::ShaderBoundChecks::unchecked(),
2121+
)
2122+
},
21172123
}
21182124
}
21192125

@@ -2131,7 +2137,9 @@ impl Device {
21312137
) -> ShaderModule {
21322138
ShaderModule {
21332139
context: Arc::clone(&self.context),
2134-
id: Context::device_create_shader_module_spirv(&*self.context, &self.id, desc),
2140+
id: unsafe {
2141+
Context::device_create_shader_module_spirv(&*self.context, &self.id, desc)
2142+
},
21352143
}
21362144
}
21372145

@@ -2241,9 +2249,10 @@ impl Device {
22412249
) -> Texture {
22422250
Texture {
22432251
context: Arc::clone(&self.context),
2244-
id: self
2245-
.context
2246-
.create_texture_from_hal::<A>(hal_texture, &self.id, desc),
2252+
id: unsafe {
2253+
self.context
2254+
.create_texture_from_hal::<A>(hal_texture, &self.id, desc)
2255+
},
22472256
owned: true,
22482257
}
22492258
}
@@ -2315,8 +2324,10 @@ impl Device {
23152324
&self,
23162325
hal_device_callback: F,
23172326
) -> R {
2318-
self.context
2319-
.device_as_hal::<A, F, R>(&self.id, hal_device_callback)
2327+
unsafe {
2328+
self.context
2329+
.device_as_hal::<A, F, R>(&self.id, hal_device_callback)
2330+
}
23202331
}
23212332
}
23222333

@@ -2626,8 +2637,10 @@ impl Texture {
26262637
&self,
26272638
hal_texture_callback: F,
26282639
) {
2629-
self.context
2630-
.texture_as_hal::<A, F>(&self.id, hal_texture_callback)
2640+
unsafe {
2641+
self.context
2642+
.texture_as_hal::<A, F>(&self.id, hal_texture_callback)
2643+
}
26312644
}
26322645

26332646
/// Creates a view of this texture.

0 commit comments

Comments
 (0)