Skip to content

Const support for configuration types #9923

Description

@Wtoll

Problem

The wgpu api has a lot of struct types that exist almost entirely to express how various things should be configured, however, instantiating these types inline can get very verbose and repetitive, so in some scenarios it may be desirable to use constants to shorten common configurations. While many of these types are conveniently Default for doing just this at runtime, const traits are not yet stabilized meaning Default::default cannot be called in the same way from these const contexts. While some of these types have entirely public fields which could be explicitly assigned to create a constant, some do not have any safe method of instantiating them in a const context. A good example of this is ForceShaderModelToken which is required in the following instantiation tree:

InstanceDescriptor
- BackendOptions
    - Dx12BackendOptions
        - ForceShaderModelToken

It has a single field of type Option<DxcShaderModel>, so the default value is clear, but the field is private so it cannot be directly assigned to. This means its impossible using safe rust to specify even something as simple as the configuration for a wgpu::Instance from a const context.

Solutions

Ultimately, I'm asking for greater const support for these configuration types. At the very least the field of ForceShaderModelToken should probably be made public, but there's likely other types like it that cause the same problem. Even with that though, its not ergonomic to have to instantiate each individual field of a struct in a const context, and any future api additions could similarly recreate the exact same problem over and over again if they are not careful. When const traits get stabilized this will become redundant, but in the short term it might be nice to have an associated constant on each of these types that is just the default value, or perhaps a pub const fn const_default() -> Self; function on each type as a placeholder. When const traits do become stabilized these methods could then just be changed to internally call Default::default without creating a breaking change.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

Status
Todo

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions