diff --git a/wgpu-core/src/binding_model.rs b/wgpu-core/src/binding_model.rs index bea63b0ba..732c152dc 100644 --- a/wgpu-core/src/binding_model.rs +++ b/wgpu-core/src/binding_model.rs @@ -514,8 +514,8 @@ impl Resource for BindGroupLayout { &mut self.info } - fn label(&self) -> String { - self.label.clone() + fn label(&self) -> &str { + &self.label } } impl BindGroupLayout { diff --git a/wgpu-core/src/command/mod.rs b/wgpu-core/src/command/mod.rs index b517902ab..5730960e5 100644 --- a/wgpu-core/src/command/mod.rs +++ b/wgpu-core/src/command/mod.rs @@ -333,13 +333,7 @@ impl CommandBuffer { device: device.clone(), limits: device.limits.clone(), support_clear_texture: device.features.contains(wgt::Features::CLEAR_TEXTURE), - info: ResourceInfo::new( - label - .as_ref() - .unwrap_or(&String::from("")) - .as_str(), - None, - ), + info: ResourceInfo::new(label.as_deref().unwrap_or(""), None), data: Mutex::new( rank::COMMAND_BUFFER_DATA, Some(CommandBufferMutable { @@ -477,14 +471,6 @@ impl Resource for CommandBuffer { fn as_info_mut(&mut self) -> &mut ResourceInfo { &mut self.info } - - fn label(&self) -> String { - let str = match self.data.lock().as_ref().unwrap().encoder.label.as_ref() { - Some(label) => label.clone(), - _ => String::new(), - }; - str - } } #[derive(Copy, Clone, Debug)] diff --git a/wgpu-core/src/device/global.rs b/wgpu-core/src/device/global.rs index 9ebecd80c..660f90279 100644 --- a/wgpu-core/src/device/global.rs +++ b/wgpu-core/src/device/global.rs @@ -1363,9 +1363,7 @@ impl Global { &device, #[cfg(feature = "trace")] device.trace.lock().is_some(), - desc.label - .to_hal(device.instance_flags) - .map(|s| s.to_string()), + desc.label.to_hal(device.instance_flags).map(str::to_owned), ); let (id, _) = fid.assign(Arc::new(command_buffer)); diff --git a/wgpu-core/src/instance.rs b/wgpu-core/src/instance.rs index 83680b890..5d21ed039 100644 --- a/wgpu-core/src/instance.rs +++ b/wgpu-core/src/instance.rs @@ -159,8 +159,8 @@ impl Resource for Surface { &mut self.info } - fn label(&self) -> String { - String::from("") + fn label(&self) -> &str { + "" } } diff --git a/wgpu-core/src/pipeline.rs b/wgpu-core/src/pipeline.rs index d70b118d7..3c80929e6 100644 --- a/wgpu-core/src/pipeline.rs +++ b/wgpu-core/src/pipeline.rs @@ -84,8 +84,8 @@ impl Resource for ShaderModule { &mut self.info } - fn label(&self) -> String { - self.label.clone() + fn label(&self) -> &str { + &self.label } } diff --git a/wgpu-core/src/registry.rs b/wgpu-core/src/registry.rs index f0f5674da..f5abb76df 100644 --- a/wgpu-core/src/registry.rs +++ b/wgpu-core/src/registry.rs @@ -182,7 +182,7 @@ impl Registry { if label.is_empty() { format!("<{}-{:?}>", type_name, id.unzip()) } else { - label + label.to_owned() } } Err(_) => format!( diff --git a/wgpu-core/src/resource.rs b/wgpu-core/src/resource.rs index 0779305f4..67e756c10 100644 --- a/wgpu-core/src/resource.rs +++ b/wgpu-core/src/resource.rs @@ -84,7 +84,8 @@ impl Drop for ResourceInfo { } impl ResourceInfo { - #[allow(unused_variables)] + // Note: Abstractly, this function should take `label: String` to minimize string cloning. + // But as actually used, every input is a literal or borrowed `&str`, so this is convenient. pub(crate) fn new( label: &str, tracker_indices: Option>, @@ -149,9 +150,16 @@ pub(crate) trait Resource: 'static + Sized + WasmNotSendSync { const TYPE: ResourceType; fn as_info(&self) -> &ResourceInfo; fn as_info_mut(&mut self) -> &mut ResourceInfo; - fn label(&self) -> String { - self.as_info().label.clone() + + /// Returns a string identifying this resource for logging and errors. + /// + /// It may be a user-provided string or it may be a placeholder from wgpu. + /// + /// It is non-empty unless the user-provided string was empty. + fn label(&self) -> &str { + &self.as_info().label } + fn ref_count(self: &Arc) -> usize { Arc::strong_count(self) } @@ -718,8 +726,8 @@ impl Resource for StagingBuffer { &mut self.info } - fn label(&self) -> String { - String::from("") + fn label(&self) -> &str { + "" } }