Skip to content

Commit 03db77c

Browse files
authored
[core] Replace id transmute method with explicit functions. (#5509)
Replace the `wgpu_core::id::Id::transmute` method, the `transmute` private module, and the `Transmute` sealed trait with some associated functions with obvious names.
1 parent 17ef6ca commit 03db77c

File tree

10 files changed

+54
-49
lines changed

10 files changed

+54
-49
lines changed

player/src/bin/play.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ fn main() {
8787
&desc,
8888
None,
8989
Some(id),
90-
Some(id.transmute())
90+
Some(id.into_queue_id())
9191
));
9292
if let Some(e) = error {
9393
panic!("{:?}", e);

player/src/lib.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ impl GlobalPlay for wgc::global::Global {
336336
let bin = std::fs::read(dir.join(data)).unwrap();
337337
let size = (range.end - range.start) as usize;
338338
if queued {
339-
self.queue_write_buffer::<A>(device.transmute(), id, range.start, &bin)
339+
self.queue_write_buffer::<A>(device.into_queue_id(), id, range.start, &bin)
340340
.unwrap();
341341
} else {
342342
self.device_wait_for_buffer::<A>(device, id).unwrap();
@@ -351,23 +351,27 @@ impl GlobalPlay for wgc::global::Global {
351351
size,
352352
} => {
353353
let bin = std::fs::read(dir.join(data)).unwrap();
354-
self.queue_write_texture::<A>(device.transmute(), &to, &bin, &layout, &size)
354+
self.queue_write_texture::<A>(device.into_queue_id(), &to, &bin, &layout, &size)
355355
.unwrap();
356356
}
357357
Action::Submit(_index, ref commands) if commands.is_empty() => {
358-
self.queue_submit::<A>(device.transmute(), &[]).unwrap();
358+
self.queue_submit::<A>(device.into_queue_id(), &[]).unwrap();
359359
}
360360
Action::Submit(_index, commands) => {
361361
let (encoder, error) = self.device_create_command_encoder::<A>(
362362
device,
363363
&wgt::CommandEncoderDescriptor { label: None },
364-
Some(comb_manager.process(device.backend()).transmute()),
364+
Some(
365+
comb_manager
366+
.process(device.backend())
367+
.into_command_encoder_id(),
368+
),
365369
);
366370
if let Some(e) = error {
367371
panic!("{e}");
368372
}
369373
let cmdbuf = self.encode_commands::<A>(encoder, commands);
370-
self.queue_submit::<A>(device.transmute(), &[cmdbuf])
374+
self.queue_submit::<A>(device.into_queue_id(), &[cmdbuf])
371375
.unwrap();
372376
}
373377
}

player/tests/test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ impl Test<'_> {
115115
},
116116
None,
117117
Some(device_id),
118-
Some(device_id.transmute())
118+
Some(device_id.into_queue_id())
119119
));
120120
if let Some(e) = error {
121121
panic!("{:?}", e);

wgpu-core/src/command/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ impl<A: HalApi> CommandBuffer<A> {
253253
id: id::CommandEncoderId,
254254
) -> Result<Arc<Self>, CommandEncoderError> {
255255
let storage = hub.command_buffers.read();
256-
match storage.get(id.transmute()) {
256+
match storage.get(id.into_command_buffer_id()) {
257257
Ok(cmd_buf) => match cmd_buf.data.lock().as_ref().unwrap().status {
258258
CommandEncoderStatus::Recording => Ok(cmd_buf.clone()),
259259
CommandEncoderStatus::Finished => Err(CommandEncoderError::NotRecording),
@@ -418,7 +418,7 @@ impl Global {
418418

419419
let hub = A::hub(self);
420420

421-
let error = match hub.command_buffers.get(encoder_id.transmute()) {
421+
let error = match hub.command_buffers.get(encoder_id.into_command_buffer_id()) {
422422
Ok(cmd_buf) => {
423423
let mut cmd_buf_data = cmd_buf.data.lock();
424424
let cmd_buf_data = cmd_buf_data.as_mut().unwrap();
@@ -444,7 +444,7 @@ impl Global {
444444
Err(_) => Some(CommandEncoderError::Invalid),
445445
};
446446

447-
(encoder_id.transmute(), error)
447+
(encoder_id.into_command_buffer_id(), error)
448448
}
449449

450450
pub fn command_encoder_push_debug_group<A: HalApi>(
@@ -700,7 +700,7 @@ impl PrettyError for PassErrorScope {
700700
// This error is not in the error chain, only notes are needed
701701
match *self {
702702
Self::Pass(id) => {
703-
fmt.command_buffer_label(&id.transmute());
703+
fmt.command_buffer_label(&id.into_command_buffer_id());
704704
}
705705
Self::SetBindGroup(id) => {
706706
fmt.bind_group_label(&id);

wgpu-core/src/command/render.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2409,7 +2409,10 @@ impl Global {
24092409
(trackers, pending_discard_init_fixups)
24102410
};
24112411

2412-
let cmd_buf = hub.command_buffers.get(encoder_id.transmute()).unwrap();
2412+
let cmd_buf = hub
2413+
.command_buffers
2414+
.get(encoder_id.into_command_buffer_id())
2415+
.unwrap();
24132416
let mut cmd_buf_data = cmd_buf.data.lock();
24142417
let cmd_buf_data = cmd_buf_data.as_mut().unwrap();
24152418

wgpu-core/src/device/global.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1334,7 +1334,9 @@ impl Global {
13341334
profiling::scope!("Device::create_command_encoder");
13351335

13361336
let hub = A::hub(self);
1337-
let fid = hub.command_buffers.prepare(id_in.map(|id| id.transmute()));
1337+
let fid = hub
1338+
.command_buffers
1339+
.prepare(id_in.map(|id| id.into_command_buffer_id()));
13381340

13391341
let error = loop {
13401342
let device = match hub.devices.get(device_id) {
@@ -1369,11 +1371,11 @@ impl Global {
13691371

13701372
let (id, _) = fid.assign(Arc::new(command_buffer));
13711373
api_log!("Device::create_command_encoder -> {id:?}");
1372-
return (id.transmute(), None);
1374+
return (id.into_command_encoder_id(), None);
13731375
};
13741376

13751377
let id = fid.assign_error(desc.label.borrow_or_default());
1376-
(id.transmute(), Some(error))
1378+
(id.into_command_encoder_id(), Some(error))
13771379
}
13781380

13791381
pub fn command_buffer_label<A: HalApi>(&self, id: id::CommandBufferId) -> String {
@@ -1388,7 +1390,7 @@ impl Global {
13881390

13891391
if let Some(cmd_buf) = hub
13901392
.command_buffers
1391-
.unregister(command_encoder_id.transmute())
1393+
.unregister(command_encoder_id.into_command_buffer_id())
13921394
{
13931395
cmd_buf.data.lock().as_mut().unwrap().encoder.discard();
13941396
cmd_buf
@@ -1400,7 +1402,7 @@ impl Global {
14001402
pub fn command_buffer_drop<A: HalApi>(&self, command_buffer_id: id::CommandBufferId) {
14011403
profiling::scope!("CommandBuffer::drop");
14021404
api_log!("CommandBuffer::drop {command_buffer_id:?}");
1403-
self.command_encoder_drop::<A>(command_buffer_id.transmute())
1405+
self.command_encoder_drop::<A>(command_buffer_id.into_command_encoder_id())
14041406
}
14051407

14061408
pub fn device_create_render_bundle_encoder(
@@ -2121,7 +2123,7 @@ impl Global {
21212123
.map_err(|_| DeviceError::Invalid)?;
21222124

21232125
if let wgt::Maintain::WaitForSubmissionIndex(submission_index) = maintain {
2124-
if submission_index.queue_id != device_id.transmute() {
2126+
if submission_index.queue_id != device_id.into_queue_id() {
21252127
return Err(WaitIdleError::WrongSubmissionIndex(
21262128
submission_index.queue_id,
21272129
device_id,

wgpu-core/src/device/queue.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -707,7 +707,7 @@ impl Global {
707707
.get(destination.texture)
708708
.map_err(|_| TransferError::InvalidTexture(destination.texture))?;
709709

710-
if dst.device.as_info().id() != queue_id.transmute() {
710+
if dst.device.as_info().id().into_queue_id() != queue_id {
711711
return Err(DeviceError::WrongDevice.into());
712712
}
713713

@@ -1191,7 +1191,7 @@ impl Global {
11911191
Err(_) => continue,
11921192
};
11931193

1194-
if cmdbuf.device.as_info().id() != queue_id.transmute() {
1194+
if cmdbuf.device.as_info().id().into_queue_id() != queue_id {
11951195
return Err(DeviceError::WrongDevice.into());
11961196
}
11971197

wgpu-core/src/id.rs

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -182,15 +182,6 @@ where
182182
self.0.backend()
183183
}
184184

185-
/// Transmute this identifier to one with a different marker trait.
186-
///
187-
/// Legal use is governed through a sealed trait, however it's correctness
188-
/// depends on the current implementation of `wgpu-core`.
189-
#[inline]
190-
pub const fn transmute<U: self::transmute::Transmute<T>>(self) -> Id<U> {
191-
Id(self.0, PhantomData)
192-
}
193-
194185
#[inline]
195186
pub fn zip(index: Index, epoch: Epoch, backend: Backend) -> Self {
196187
Id(RawId::zip(index, epoch, backend), PhantomData)
@@ -202,20 +193,6 @@ where
202193
}
203194
}
204195

205-
pub(crate) mod transmute {
206-
// This trait is effectively sealed to prevent illegal transmutes.
207-
pub trait Transmute<U>: super::Marker {}
208-
209-
// Self-transmute is always legal.
210-
impl<T> Transmute<T> for T where T: super::Marker {}
211-
212-
// TODO: Remove these once queues have their own identifiers.
213-
impl Transmute<super::markers::Queue> for super::markers::Device {}
214-
impl Transmute<super::markers::Device> for super::markers::Queue {}
215-
impl Transmute<super::markers::CommandBuffer> for super::markers::CommandEncoder {}
216-
impl Transmute<super::markers::CommandEncoder> for super::markers::CommandBuffer {}
217-
}
218-
219196
impl<T> Copy for Id<T> where T: Marker {}
220197

221198
impl<T> Clone for Id<T>
@@ -349,6 +326,24 @@ ids! {
349326
pub type QuerySetId QuerySet;
350327
}
351328

329+
impl CommandEncoderId {
330+
pub fn into_command_buffer_id(self) -> CommandBufferId {
331+
Id(self.0, PhantomData)
332+
}
333+
}
334+
335+
impl CommandBufferId {
336+
pub fn into_command_encoder_id(self) -> CommandEncoderId {
337+
Id(self.0, PhantomData)
338+
}
339+
}
340+
341+
impl DeviceId {
342+
pub fn into_queue_id(self) -> QueueId {
343+
Id(self.0, PhantomData)
344+
}
345+
}
346+
352347
#[test]
353348
fn test_id_backend() {
354349
for &b in &[

wgpu-core/src/resource.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1043,7 +1043,10 @@ impl Global {
10431043
profiling::scope!("CommandEncoder::as_hal");
10441044

10451045
let hub = A::hub(self);
1046-
let cmd_buf = hub.command_buffers.get(id.transmute()).unwrap();
1046+
let cmd_buf = hub
1047+
.command_buffers
1048+
.get(id.into_command_buffer_id())
1049+
.unwrap();
10471050
let mut cmd_buf_data = cmd_buf.data.lock();
10481051
let cmd_buf_data = cmd_buf_data.as_mut().unwrap();
10491052
let cmd_buf_raw = cmd_buf_data.encoder.open().ok();

wgpu/src/backend/wgpu_core.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@ impl crate::Context for ContextWgpuCore {
631631
id: queue_id,
632632
error_sink,
633633
};
634-
ready(Ok((device_id, device, device_id.transmute(), queue)))
634+
ready(Ok((device_id, device, device_id.into_queue_id(), queue)))
635635
}
636636

637637
fn instance_poll_all_devices(&self, force_wait: bool) -> bool {
@@ -1839,8 +1839,7 @@ impl crate::Context for ContextWgpuCore {
18391839
if let Err(cause) = wgc::gfx_select!(
18401840
encoder => self.0.command_encoder_run_compute_pass(*encoder, pass_data)
18411841
) {
1842-
let name =
1843-
wgc::gfx_select!(encoder => self.0.command_buffer_label(encoder.transmute()));
1842+
let name = wgc::gfx_select!(encoder => self.0.command_buffer_label(encoder.into_command_buffer_id()));
18441843
self.handle_error(
18451844
&encoder_data.error_sink,
18461845
cause,
@@ -1923,8 +1922,7 @@ impl crate::Context for ContextWgpuCore {
19231922
if let Err(cause) =
19241923
wgc::gfx_select!(encoder => self.0.command_encoder_run_render_pass(*encoder, pass_data))
19251924
{
1926-
let name =
1927-
wgc::gfx_select!(encoder => self.0.command_buffer_label(encoder.transmute()));
1925+
let name = wgc::gfx_select!(encoder => self.0.command_buffer_label(encoder.into_command_buffer_id()));
19281926
self.handle_error(
19291927
&encoder_data.error_sink,
19301928
cause,

0 commit comments

Comments
 (0)