Skip to content

Commit

Permalink
Use () instead of PhantomData as IdentityManager's Input type.
Browse files Browse the repository at this point in the history
PhantomData suggests that there's some sort of persuasion required
for lifetime variance inference or other sorts of arcana, but it
doesn't seem to be necessary at all. `()` works just fine.
  • Loading branch information
jimblandy committed Aug 19, 2022
1 parent c6ca2a7 commit e67872c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 41 deletions.
2 changes: 1 addition & 1 deletion wgpu-core/src/hub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ pub trait IdentityHandler<I>: Debug {
}

impl<I: id::TypedId + Debug> IdentityHandler<I> for Mutex<IdentityManager> {
type Input = PhantomData<I>;
type Input = ();
fn process(&self, _id: Self::Input, backend: Backend) -> I {
self.lock().alloc(backend)
}
Expand Down
73 changes: 33 additions & 40 deletions wgpu/src/backend/direct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ use std::{
error::Error,
fmt,
future::{ready, Ready},
marker::PhantomData,
ops::Range,
slice,
sync::Arc,
Expand Down Expand Up @@ -69,17 +68,15 @@ impl Context {
#[cfg(any(not(target_arch = "wasm32"), feature = "emscripten"))]
pub fn enumerate_adapters(&self, backends: wgt::Backends) -> Vec<wgc::id::AdapterId> {
self.0
.enumerate_adapters(wgc::instance::AdapterInputs::Mask(backends, |_| {
PhantomData
}))
.enumerate_adapters(wgc::instance::AdapterInputs::Mask(backends, |_| ()))
}

#[cfg(any(not(target_arch = "wasm32"), feature = "emscripten"))]
pub unsafe fn create_adapter_from_hal<A: wgc::hub::HalApi>(
&self,
hal_adapter: hal::ExposedAdapter<A>,
) -> wgc::id::AdapterId {
self.0.create_adapter_from_hal(hal_adapter, PhantomData)
self.0.create_adapter_from_hal(hal_adapter, ())
}

pub unsafe fn adapter_as_hal<A: wgc::hub::HalApi, F: FnOnce(Option<&A::Adapter>) -> R, R>(
Expand All @@ -105,7 +102,7 @@ impl Context {
hal_device,
&desc.map_label(|l| l.map(Borrowed)),
trace_dir,
PhantomData,
(),
);
if let Some(err) = error {
self.handle_error_fatal(err, "Adapter::create_device_from_hal");
Expand All @@ -130,7 +127,7 @@ impl Context {
hal_texture,
device.id,
&desc.map_label(|l| l.map(Borrowed)),
PhantomData,
(),
);
if let Some(cause) = error {
self.handle_error(
Expand Down Expand Up @@ -177,7 +174,7 @@ impl Context {
self: &Arc<Self>,
layer: *mut std::ffi::c_void,
) -> crate::Surface {
let id = self.0.instance_create_surface_metal(layer, PhantomData);
let id = self.0.instance_create_surface_metal(layer, ());
crate::Surface {
context: Arc::clone(self),
id: Surface {
Expand All @@ -192,7 +189,7 @@ impl Context {
self: &Arc<Self>,
canvas: &web_sys::HtmlCanvasElement,
) -> Surface {
let id = self.0.create_surface_webgl_canvas(canvas, PhantomData);
let id = self.0.create_surface_webgl_canvas(canvas, ());
Surface {
id,
configured_device: Mutex::default(),
Expand All @@ -204,9 +201,7 @@ impl Context {
self: &Arc<Self>,
canvas: &web_sys::OffscreenCanvas,
) -> Surface {
let id = self
.0
.create_surface_webgl_offscreen_canvas(canvas, PhantomData);
let id = self.0.create_surface_webgl_offscreen_canvas(canvas, ());
Surface {
id,
configured_device: Mutex::default(),
Expand All @@ -218,9 +213,7 @@ impl Context {
self: &Arc<Self>,
visual: *mut std::ffi::c_void,
) -> crate::Surface {
let id = self
.0
.instance_create_surface_from_visual(visual, PhantomData);
let id = self.0.instance_create_surface_from_visual(visual, ());
crate::Surface {
context: Arc::clone(self),
id: Surface {
Expand Down Expand Up @@ -853,7 +846,7 @@ impl crate::Context for Context {
handle: &(impl raw_window_handle::HasRawWindowHandle + raw_window_handle::HasRawDisplayHandle),
) -> Self::SurfaceId {
Surface {
id: self.0.instance_create_surface(handle, PhantomData),
id: self.0.instance_create_surface(handle, ()),
configured_device: Mutex::new(None),
}
}
Expand All @@ -868,7 +861,7 @@ impl crate::Context for Context {
force_fallback_adapter: options.force_fallback_adapter,
compatible_surface: options.compatible_surface.map(|surface| surface.id.id),
},
wgc::instance::AdapterInputs::Mask(wgt::Backends::all(), |_| PhantomData),
wgc::instance::AdapterInputs::Mask(wgt::Backends::all(), |_| ()),
);
ready(id.ok())
}
Expand All @@ -892,7 +885,7 @@ impl crate::Context for Context {
*adapter,
&desc.map_label(|l| l.map(Borrowed)),
trace_dir,
PhantomData
()
));
if let Some(err) = error {
log::error!("Error in Adapter::request_device: {}", err);
Expand Down Expand Up @@ -1022,7 +1015,7 @@ impl crate::Context for Context {
.lock()
.expect("Surface was not configured?");
match wgc::gfx_select!(
device_id => global.surface_get_current_texture(surface.id, PhantomData)
device_id => global.surface_get_current_texture(surface.id, ())
) {
Ok(wgc::present::SurfaceOutput { status, texture_id }) => (
texture_id.map(|id| Texture {
Expand Down Expand Up @@ -1127,7 +1120,7 @@ impl crate::Context for Context {
ShaderSource::Naga(module) => wgc::pipeline::ShaderModuleSource::Naga(module),
};
let (id, error) = wgc::gfx_select!(
device.id => global.device_create_shader_module(device.id, &descriptor, source, PhantomData)
device.id => global.device_create_shader_module(device.id, &descriptor, source, ())
);
if let Some(cause) = error {
self.handle_error(
Expand All @@ -1154,7 +1147,7 @@ impl crate::Context for Context {
shader_bound_checks: wgt::ShaderBoundChecks::unchecked(),
};
let (id, error) = wgc::gfx_select!(
device.id => global.device_create_shader_module_spirv(device.id, &descriptor, Borrowed(&desc.source), PhantomData)
device.id => global.device_create_shader_module_spirv(device.id, &descriptor, Borrowed(&desc.source), ())
);
if let Some(cause) = error {
self.handle_error(
Expand All @@ -1179,7 +1172,7 @@ impl crate::Context for Context {
entries: Borrowed(desc.entries),
};
let (id, error) = wgc::gfx_select!(
device.id => global.device_create_bind_group_layout(device.id, &descriptor, PhantomData)
device.id => global.device_create_bind_group_layout(device.id, &descriptor, ())
);
if let Some(cause) = error {
self.handle_error(
Expand Down Expand Up @@ -1280,7 +1273,7 @@ impl crate::Context for Context {
let (id, error) = wgc::gfx_select!(device.id => global.device_create_bind_group(
device.id,
&descriptor,
PhantomData
()
));
if let Some(cause) = error {
self.handle_error(
Expand Down Expand Up @@ -1323,7 +1316,7 @@ impl crate::Context for Context {
let (id, error) = wgc::gfx_select!(device.id => global.device_create_pipeline_layout(
device.id,
&descriptor,
PhantomData
()
));
if let Some(cause) = error {
self.handle_error(
Expand Down Expand Up @@ -1358,8 +1351,8 @@ impl crate::Context for Context {
let implicit_pipeline_ids = match desc.layout {
Some(_) => None,
None => Some(wgc::device::ImplicitPipelineIds {
root_id: PhantomData,
group_ids: &[PhantomData; wgc::MAX_BIND_GROUPS],
root_id: (),
group_ids: &[(); wgc::MAX_BIND_GROUPS],
}),
};
let descriptor = pipe::RenderPipelineDescriptor {
Expand Down Expand Up @@ -1389,7 +1382,7 @@ impl crate::Context for Context {
let (id, error) = wgc::gfx_select!(device.id => global.device_create_render_pipeline(
device.id,
&descriptor,
PhantomData,
(),
implicit_pipeline_ids
));
if let Some(cause) = error {
Expand Down Expand Up @@ -1418,8 +1411,8 @@ impl crate::Context for Context {
let implicit_pipeline_ids = match desc.layout {
Some(_) => None,
None => Some(wgc::device::ImplicitPipelineIds {
root_id: PhantomData,
group_ids: &[PhantomData; wgc::MAX_BIND_GROUPS],
root_id: (),
group_ids: &[(); wgc::MAX_BIND_GROUPS],
}),
};
let descriptor = pipe::ComputePipelineDescriptor {
Expand All @@ -1435,7 +1428,7 @@ impl crate::Context for Context {
let (id, error) = wgc::gfx_select!(device.id => global.device_create_compute_pipeline(
device.id,
&descriptor,
PhantomData,
(),
implicit_pipeline_ids
));
if let Some(cause) = error {
Expand Down Expand Up @@ -1467,7 +1460,7 @@ impl crate::Context for Context {
let (id, error) = wgc::gfx_select!(device.id => global.device_create_buffer(
device.id,
&desc.map_label(|l| l.map(Borrowed)),
PhantomData
()
));
if let Some(cause) = error {
self.handle_error(
Expand All @@ -1493,7 +1486,7 @@ impl crate::Context for Context {
let (id, error) = wgc::gfx_select!(device.id => global.device_create_texture(
device.id,
&desc.map_label(|l| l.map(Borrowed)),
PhantomData
()
));
if let Some(cause) = error {
self.handle_error(
Expand Down Expand Up @@ -1536,7 +1529,7 @@ impl crate::Context for Context {
let (id, error) = wgc::gfx_select!(device.id => global.device_create_sampler(
device.id,
&descriptor,
PhantomData
()
));
if let Some(cause) = error {
self.handle_error(
Expand All @@ -1559,7 +1552,7 @@ impl crate::Context for Context {
let (id, error) = wgc::gfx_select!(device.id => global.device_create_query_set(
device.id,
&desc.map_label(|l| l.map(Borrowed)),
PhantomData
()
));
if let Some(cause) = error {
self.handle_error_nolabel(&device.error_sink, cause, "Device::create_query_set");
Expand All @@ -1576,7 +1569,7 @@ impl crate::Context for Context {
let (id, error) = wgc::gfx_select!(device.id => global.device_create_command_encoder(
device.id,
&desc.map_label(|l| l.map(Borrowed)),
PhantomData
()
));
if let Some(cause) = error {
self.handle_error(
Expand Down Expand Up @@ -1742,7 +1735,7 @@ impl crate::Context for Context {
};
let global = &self.0;
let (id, error) = wgc::gfx_select!(
texture.id => global.texture_create_view(texture.id, &descriptor, PhantomData)
texture.id => global.texture_create_view(texture.id, &descriptor, ())
);
if let Some(cause) = error {
self.handle_error(
Expand Down Expand Up @@ -1848,7 +1841,7 @@ impl crate::Context for Context {
index: u32,
) -> Self::BindGroupLayoutId {
let global = &self.0;
let (id, error) = wgc::gfx_select!(*pipeline => global.compute_pipeline_get_bind_group_layout(*pipeline, index, PhantomData));
let (id, error) = wgc::gfx_select!(*pipeline => global.compute_pipeline_get_bind_group_layout(*pipeline, index, ()));
if let Some(err) = error {
panic!("Error reflecting bind group {}: {}", index, err);
}
Expand All @@ -1860,7 +1853,7 @@ impl crate::Context for Context {
index: u32,
) -> Self::BindGroupLayoutId {
let global = &self.0;
let (id, error) = wgc::gfx_select!(*pipeline => global.render_pipeline_get_bind_group_layout(*pipeline, index, PhantomData));
let (id, error) = wgc::gfx_select!(*pipeline => global.render_pipeline_get_bind_group_layout(*pipeline, index, ()));
if let Some(err) = error {
panic!("Error reflecting bind group {}: {}", index, err);
}
Expand Down Expand Up @@ -2183,7 +2176,7 @@ impl crate::Context for Context {
let (id, error) = wgc::gfx_select!(encoder.parent() => global.render_bundle_encoder_finish(
encoder,
&desc.map_label(|l| l.map(Borrowed)),
PhantomData
()
));
if let Some(err) = error {
self.handle_error_fatal(err, "RenderBundleEncoder::finish");
Expand Down Expand Up @@ -2230,7 +2223,7 @@ impl crate::Context for Context {
) -> QueueWriteBuffer {
let global = &self.0;
match wgc::gfx_select!(
*queue => global.queue_create_staging_buffer(*queue, size, PhantomData)
*queue => global.queue_create_staging_buffer(*queue, size, ())
) {
Ok((buffer_id, ptr)) => QueueWriteBuffer {
buffer_id,
Expand Down

0 comments on commit e67872c

Please sign in to comment.