Skip to content

Commit

Permalink
Introduce boxes and vectors to reduce Result sizes.
Browse files Browse the repository at this point in the history
The following error variants were large enough to trip Clippy's
`result_large_err` lint:

- RenderPassCompatibilityError::IncompatibleColorAttachment
- CreateShaderModuleError::Validation

Large error types are a problem, because they make the `Result` type
large, requiring the program to copy around large numbers of bytes
even in the success case. Since the failure case is rare, it's usually
not a problem to just heap-allocate the contents to keep the `Ok` case
small.
  • Loading branch information
jimblandy committed Nov 22, 2022
1 parent f41a1c2 commit dcb48dd
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 10 deletions.
1 change: 0 additions & 1 deletion .clippy.toml

This file was deleted.

13 changes: 5 additions & 8 deletions wgpu-core/src/device/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,7 @@ pub(crate) struct RenderPassContext {
#[derive(Clone, Debug, Error)]
pub enum RenderPassCompatibilityError {
#[error("Incompatible color attachment: the renderpass expected {0:?} but was given {1:?}")]
IncompatibleColorAttachment(
ArrayVec<Option<TextureFormat>, { hal::MAX_COLOR_ATTACHMENTS }>,
ArrayVec<Option<TextureFormat>, { hal::MAX_COLOR_ATTACHMENTS }>,
),
IncompatibleColorAttachment(Vec<Option<TextureFormat>>, Vec<Option<TextureFormat>>),
#[error(
"Incompatible depth-stencil attachment: the renderpass expected {0:?} but was given {1:?}"
)]
Expand All @@ -102,8 +99,8 @@ impl RenderPassContext {
) -> Result<(), RenderPassCompatibilityError> {
if self.attachments.colors != other.attachments.colors {
return Err(RenderPassCompatibilityError::IncompatibleColorAttachment(
self.attachments.colors.clone(),
other.attachments.colors.clone(),
self.attachments.colors.iter().cloned().collect(),
other.attachments.colors.iter().cloned().collect(),
));
}
if self.attachments.depth_stencil != other.attachments.depth_stencil {
Expand Down Expand Up @@ -1245,7 +1242,7 @@ impl<A: HalApi> Device<A> {
pipeline::CreateShaderModuleError::Parsing(pipeline::ShaderError {
source: code.to_string(),
label: desc.label.as_ref().map(|l| l.to_string()),
inner,
inner: Box::new(inner),
})
})?;
(Cow::Owned(module), code.into_owned())
Expand Down Expand Up @@ -1308,7 +1305,7 @@ impl<A: HalApi> Device<A> {
pipeline::CreateShaderModuleError::Validation(pipeline::ShaderError {
source,
label: desc.label.as_ref().map(|l| l.to_string()),
inner,
inner: Box::new(inner),
})
})?;
let interface =
Expand Down
2 changes: 1 addition & 1 deletion wgpu-core/src/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl<A: hal::Api> Resource for ShaderModule<A> {
pub struct ShaderError<E> {
pub source: String,
pub label: Option<String>,
pub inner: E,
pub inner: Box<E>,
}
#[cfg(feature = "wgsl")]
impl fmt::Display for ShaderError<naga::front::wgsl::ParseError> {
Expand Down

0 comments on commit dcb48dd

Please sign in to comment.