Skip to content

Commit 93e923e

Browse files
committed
Use less Label and more String
1 parent 4e4145f commit 93e923e

File tree

8 files changed

+47
-42
lines changed

8 files changed

+47
-42
lines changed

wgpu-core/src/command/allocator.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ impl<B: GfxBackend> CommandAllocator<B> {
132132
None
133133
},
134134
#[cfg(debug_assertions)]
135-
label: label.as_static(),
135+
label: label.to_string_or_default(),
136136
})
137137
}
138138
}

wgpu-core/src/command/bundle.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1026,7 +1026,7 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
10261026
},
10271027
used: state.trackers,
10281028
context: bundle_encoder.context,
1029-
life_guard: LifeGuard::new(desc.label.as_static()),
1029+
life_guard: LifeGuard::new(desc.label.to_string_or_default()),
10301030
}
10311031
};
10321032

wgpu-core/src/command/mod.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@ use crate::{
2828
Label, PrivateFeatures, Stored,
2929
};
3030

31-
#[cfg(debug_assertions)]
32-
use crate::LabelHelpers;
33-
3431
use hal::command::CommandBuffer as _;
3532
use thiserror::Error;
3633

@@ -51,7 +48,7 @@ pub struct CommandBuffer<B: hal::Backend> {
5148
#[cfg(feature = "trace")]
5249
pub(crate) commands: Option<Vec<crate::device::trace::Command>>,
5350
#[cfg(debug_assertions)]
54-
pub(crate) label: Label<'static>,
51+
pub(crate) label: String,
5552
}
5653

5754
impl<B: GfxBackend> CommandBuffer<B> {
@@ -115,7 +112,7 @@ impl<B: hal::Backend> crate::hub::Resource for CommandBuffer<B> {
115112

116113
fn label(&self) -> &str {
117114
#[cfg(debug_assertions)]
118-
return &self.label.borrow_or_default();
115+
return &self.label;
119116
#[cfg(not(debug_assertions))]
120117
return "";
121118
}

wgpu-core/src/device/mod.rs

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,10 @@ impl<B: GfxBackend> Device<B> {
273273
mem_allocator: Mutex::new(mem_allocator),
274274
desc_allocator: Mutex::new(descriptors),
275275
queue_group,
276-
life_guard: LifeGuard::new(Label::new_borrowed("<device>")),
276+
#[cfg(debug_assertions)]
277+
life_guard: LifeGuard::new("<device>".to_string()),
278+
#[cfg(not(debug_assertions))]
279+
life_guard: LifeGuard::new(String::new()),
277280
active_submission_index: 0,
278281
trackers: Mutex::new(TrackerSet::new(B::VARIANT)),
279282
render_passes: Mutex::new(FastHashMap::default()),
@@ -467,7 +470,7 @@ impl<B: GfxBackend> Device<B> {
467470
_ => panic!("failed to create buffer: {}", err),
468471
},
469472
)?;
470-
if let Some(ref label) = desc.label.borrow() {
473+
if let Some(ref label) = desc.label {
471474
unsafe { self.raw.set_buffer_name(&mut buffer, label) };
472475
}
473476

@@ -489,7 +492,7 @@ impl<B: GfxBackend> Device<B> {
489492
full_range: (),
490493
sync_mapped_writes: None,
491494
map_state: resource::BufferMapState::Idle,
492-
life_guard: LifeGuard::new(desc.label.as_static()),
495+
life_guard: LifeGuard::new(desc.label.to_string_or_default()),
493496
})
494497
}
495498

@@ -552,7 +555,7 @@ impl<B: GfxBackend> Device<B> {
552555
hal::image::CreationError::OutOfMemory(_) => DeviceError::OutOfMemory,
553556
_ => panic!("failed to create texture: {}", err),
554557
})?;
555-
if let Some(ref label) = desc.label.borrow() {
558+
if let Some(ref label) = desc.label {
556559
self.raw.set_image_name(&mut image, label);
557560
}
558561
image
@@ -581,7 +584,7 @@ impl<B: GfxBackend> Device<B> {
581584
levels: 0..desc.mip_level_count as hal::image::Level,
582585
layers: 0..kind.num_layers(),
583586
},
584-
life_guard: LifeGuard::new(desc.label.as_static()),
587+
life_guard: LifeGuard::new(desc.label.to_string_or_default()),
585588
})
586589
}
587590

@@ -811,7 +814,7 @@ impl<B: GfxBackend> Device<B> {
811814
.raw
812815
.create_pipeline_layout(descriptor_set_layouts, push_constants)
813816
.or(Err(DeviceError::OutOfMemory))?;
814-
if let Some(_) = desc.label.borrow() {
817+
if let Some(_) = desc.label {
815818
//TODO-0.6: needs gfx changes published
816819
//self.raw.set_pipeline_layout_name(&mut raw_layout, label);
817820
}
@@ -824,7 +827,7 @@ impl<B: GfxBackend> Device<B> {
824827
value: id::Valid(self_id),
825828
ref_count: self.life_guard.add_ref(),
826829
},
827-
life_guard: LifeGuard::new(desc.label.as_static()),
830+
life_guard: LifeGuard::new(desc.label.to_string_or_default()),
828831
bind_group_layout_ids: desc
829832
.bind_group_layouts
830833
.iter()
@@ -1597,7 +1600,7 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
15971600
extent: texture.kind.extent().at_level(desc.base_mip_level as _),
15981601
samples: texture.kind.num_samples(),
15991602
selector,
1600-
life_guard: LifeGuard::new(desc.label.as_static()),
1603+
life_guard: LifeGuard::new(desc.label.to_string_or_default()),
16011604
};
16021605
let ref_count = view.life_guard.add_ref();
16031606

@@ -1759,7 +1762,7 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
17591762
value: id::Valid(device_id),
17601763
ref_count: device.life_guard.add_ref(),
17611764
},
1762-
life_guard: LifeGuard::new(desc.label.as_static()),
1765+
life_guard: LifeGuard::new(desc.label.to_string_or_default()),
17631766
comparison: info.comparison.is_some(),
17641767
};
17651768
let ref_count = sampler.life_guard.add_ref();
@@ -1858,7 +1861,11 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
18581861
}
18591862
}
18601863

1861-
let layout = device.create_bind_group_layout(device_id, desc.label.borrow(), entry_map)?;
1864+
let layout = device.create_bind_group_layout(
1865+
device_id,
1866+
desc.label.as_ref().map(|cow| cow.as_ref()),
1867+
entry_map,
1868+
)?;
18621869

18631870
let id = hub
18641871
.bind_group_layouts
@@ -2426,7 +2433,7 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
24262433
ref_count: device.life_guard.add_ref(),
24272434
},
24282435
layout_id: id::Valid(desc.layout),
2429-
life_guard: LifeGuard::new(desc.label.as_static()),
2436+
life_guard: LifeGuard::new(desc.label.to_string_or_default()),
24302437
used,
24312438
dynamic_binding_info,
24322439
};
@@ -3202,7 +3209,7 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
32023209
flags,
32033210
index_format: desc.vertex_state.index_format,
32043211
vertex_strides,
3205-
life_guard: LifeGuard::new(desc.label.as_static()),
3212+
life_guard: LifeGuard::new(desc.label.to_string_or_default()),
32063213
};
32073214

32083215
let id = hub
@@ -3426,7 +3433,7 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
34263433
value: id::Valid(device_id),
34273434
ref_count: device.life_guard.add_ref(),
34283435
},
3429-
life_guard: LifeGuard::new(desc.label.as_static()),
3436+
life_guard: LifeGuard::new(desc.label.to_string_or_default()),
34303437
};
34313438
let id = hub
34323439
.compute_pipelines
@@ -3631,7 +3638,10 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
36313638
}
36323639

36333640
let swap_chain = swap_chain::SwapChain {
3634-
life_guard: LifeGuard::new(Label::new_borrowed("<SwapChain>")),
3641+
#[cfg(debug_assertions)]
3642+
life_guard: LifeGuard::new("<SwapChain>".to_string()),
3643+
#[cfg(not(debug_assertions))]
3644+
life_guard: LifeGuard::new(String::new()),
36353645
device_id: Stored {
36363646
value: id::Valid(device_id),
36373647
ref_count: device.life_guard.add_ref(),

wgpu-core/src/hub.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@ use crate::{
2020
Epoch, Index,
2121
};
2222

23-
#[cfg(debug_assertions)]
24-
use crate::LabelHelpers;
25-
2623
use parking_lot::{Mutex, RwLock, RwLockReadGuard, RwLockWriteGuard};
2724
use wgt::Backend;
2825

@@ -394,7 +391,7 @@ pub trait Resource {
394391
fn life_guard(&self) -> &crate::LifeGuard;
395392
fn label(&self) -> &str {
396393
#[cfg(debug_assertions)]
397-
return self.life_guard().label.borrow_or_default();
394+
return &self.life_guard().label;
398395
#[cfg(not(debug_assertions))]
399396
return "";
400397
}

wgpu-core/src/instance.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::{
77
device::Device,
88
hub::{GfxBackend, Global, GlobalIdentityHandlerFactory, Input, Token},
99
id::{AdapterId, DeviceId, SurfaceId, Valid},
10-
span, Label, LabelHelpers, LifeGuard, PrivateFeatures, Stored, MAX_BIND_GROUPS,
10+
span, LifeGuard, PrivateFeatures, Stored, MAX_BIND_GROUPS,
1111
};
1212

1313
use wgt::{Backend, BackendBit, DeviceDescriptor, PowerPreference, BIND_BUFFER_ALIGNMENT};
@@ -224,7 +224,10 @@ impl<B: hal::Backend> Adapter<B> {
224224
raw,
225225
features,
226226
limits,
227-
life_guard: LifeGuard::new(Label::new_borrowed("<Adapter>")),
227+
#[cfg(debug_assertions)]
228+
life_guard: LifeGuard::new("<Adapter>".to_string()),
229+
#[cfg(not(debug_assertions))]
230+
life_guard: LifeGuard::new(String::new()),
228231
}
229232
}
230233
}

wgpu-core/src/lib.rs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -60,22 +60,17 @@ pub type Label<'a> = Option<Cow<'a, str>>;
6060

6161
trait LabelHelpers<'a> {
6262
fn new_borrowed(s: &'a str) -> Label<'a>;
63-
fn as_static(&self) -> Label<'static>;
64-
fn borrow(&'a self) -> Option<&'a str>;
65-
fn borrow_or_default(&'a self) -> &'a str;
63+
fn to_string_or_default(&'a self) -> String;
6664
}
6765
impl<'a> LabelHelpers<'a> for Label<'a> {
6866
fn new_borrowed(s: &'a str) -> Label<'a> {
6967
Some(Cow::Borrowed(s))
7068
}
71-
fn as_static(&self) -> Label<'static> {
72-
self.as_ref().map(|cow| Cow::Owned(cow.to_string()))
73-
}
74-
fn borrow(&'a self) -> Option<&'a str> {
75-
self.as_ref().map(|cow| cow.as_ref())
76-
}
77-
fn borrow_or_default(&'a self) -> &'a str {
78-
self.borrow().unwrap_or("")
69+
fn to_string_or_default(&'a self) -> String {
70+
self.as_ref()
71+
.map(|cow| cow.as_ref())
72+
.unwrap_or("")
73+
.to_string()
7974
}
8075
}
8176

@@ -188,12 +183,12 @@ pub struct LifeGuard {
188183
ref_count: Option<RefCount>,
189184
submission_index: AtomicUsize,
190185
#[cfg(debug_assertions)]
191-
pub(crate) label: Label<'static>,
186+
pub(crate) label: String,
192187
}
193188

194189
impl LifeGuard {
195190
#[allow(unused_variables)]
196-
fn new(label: Label<'static>) -> Self {
191+
fn new(label: String) -> Self {
197192
let bx = Box::new(AtomicUsize::new(1));
198193
Self {
199194
ref_count: ptr::NonNull::new(Box::into_raw(bx)).map(RefCount),

wgpu-core/src/swap_chain.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ use crate::{
4141
id::{DeviceId, SwapChainId, TextureViewId, Valid},
4242
resource, span,
4343
track::TextureSelector,
44-
Label, LabelHelpers, LifeGuard, PrivateFeatures, Stored, SubmissionIndex,
44+
LifeGuard, PrivateFeatures, Stored, SubmissionIndex,
4545
};
4646

4747
use hal::{self, device::Device as _, queue::CommandQueue as _, window::PresentationSurface as _};
@@ -191,7 +191,10 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
191191
layers: 0..1,
192192
levels: 0..1,
193193
},
194-
life_guard: LifeGuard::new(Label::new_borrowed("<SwapChain View>")),
194+
#[cfg(debug_assertions)]
195+
life_guard: LifeGuard::new("<SwapChain View>".to_string()),
196+
#[cfg(not(debug_assertions))]
197+
life_guard: LifeGuard::new(String::new()),
195198
};
196199

197200
let ref_count = view.life_guard.add_ref();

0 commit comments

Comments
 (0)