Skip to content
This repository has been archived by the owner on Jan 29, 2025. It is now read-only.

Commit

Permalink
Make use of new language features (#1841)
Browse files Browse the repository at this point in the history
* use strip_prefix

* make fn const (resolving TODO)

* make use of nested OR patterns in match arms

* warn on clippy::missing_const_for_fn

* constify functions

* ignore clippy::missing_const_for_fn for into_inner functions
  • Loading branch information
teoxoy authored Apr 17, 2022
1 parent ef387f7 commit 7ce98dc
Show file tree
Hide file tree
Showing 48 changed files with 221 additions and 246 deletions.
4 changes: 2 additions & 2 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ fn run() -> Result<(), Box<dyn std::error::Error>> {
}
}
}
ext @ "vert" | ext @ "frag" | ext @ "comp" => {
ext @ ("vert" | "frag" | "comp") => {
let input = String::from_utf8(input)?;
let mut parser = naga::front::glsl::Parser::default();

Expand Down Expand Up @@ -429,7 +429,7 @@ fn run() -> Result<(), Box<dyn std::error::Error>> {

fs::write(output_path, bytes.as_slice())?;
}
stage @ "vert" | stage @ "frag" | stage @ "comp" => {
stage @ ("vert" | "frag" | "comp") => {
use naga::back::glsl;

let pipeline_options = glsl::PipelineOptions {
Expand Down
9 changes: 5 additions & 4 deletions src/arena.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,15 @@ impl<T> Handle<T> {
marker: PhantomData,
};

pub(crate) fn new(index: Index) -> Self {
pub(crate) const fn new(index: Index) -> Self {
Handle {
index,
marker: PhantomData,
}
}

/// Returns the zero-based index of this handle.
pub fn index(self) -> usize {
pub const fn index(self) -> usize {
let index = self.index.get() - 1;
index as usize
}
Expand All @@ -106,7 +106,7 @@ impl<T> Handle<T> {
}

/// Convert a `usize` index into a `Handle<T>`, without range checks.
unsafe fn from_usize_unchecked(index: usize) -> Self {
const unsafe fn from_usize_unchecked(index: usize) -> Self {
Handle::new(Index::new_unchecked((index + 1) as u32))
}
}
Expand Down Expand Up @@ -187,7 +187,7 @@ impl<T: fmt::Debug> fmt::Debug for Arena<T> {

impl<T> Arena<T> {
/// Create a new arena with no initial capacity allocated.
pub fn new() -> Self {
pub const fn new() -> Self {
Arena {
data: Vec::new(),
#[cfg(feature = "span")]
Expand All @@ -196,6 +196,7 @@ impl<T> Arena<T> {
}

/// Extracts the inner vector.
#[allow(clippy::missing_const_for_fn)] // ignore due to requirement of #![feature(const_precise_live_drops)]
pub fn into_inner(self) -> Vec<T> {
self.data
}
Expand Down
2 changes: 1 addition & 1 deletion src/back/glsl/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub struct FeaturesManager(Features);

impl FeaturesManager {
/// Creates a new [`FeaturesManager`] instance
pub fn new() -> Self {
pub const fn new() -> Self {
Self(Features::empty())
}

Expand Down
24 changes: 12 additions & 12 deletions src/back/glsl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ pub const SUPPORTED_ES_VERSIONS: &[u16] = &[300, 310, 320];
pub type BindingMap = std::collections::BTreeMap<crate::ResourceBinding, u8>;

impl crate::AtomicFunction {
fn to_glsl(self) -> &'static str {
const fn to_glsl(self) -> &'static str {
match self {
Self::Add | Self::Subtract => "Add",
Self::And => "And",
Expand All @@ -88,15 +88,15 @@ impl crate::AtomicFunction {
}

impl crate::AddressSpace {
fn is_buffer(&self) -> bool {
const fn is_buffer(&self) -> bool {
match *self {
crate::AddressSpace::Uniform | crate::AddressSpace::Storage { .. } => true,
_ => false,
}
}

/// Whether a variable with this address space can be initialized
fn initializable(&self) -> bool {
const fn initializable(&self) -> bool {
match *self {
crate::AddressSpace::Function | crate::AddressSpace::Private => true,
crate::AddressSpace::WorkGroup
Expand All @@ -121,7 +121,7 @@ pub enum Version {

impl Version {
/// Returns true if self is `Version::Embedded` (i.e. is a es version)
fn is_es(&self) -> bool {
const fn is_es(&self) -> bool {
match *self {
Version::Desktop(_) => false,
Version::Embedded(_) => true,
Expand Down Expand Up @@ -306,7 +306,7 @@ impl fmt::Display for VaryingName<'_> {
}

impl ShaderStage {
fn to_str(self) -> &'static str {
const fn to_str(self) -> &'static str {
match self {
ShaderStage::Compute => "cs",
ShaderStage::Fragment => "fs",
Expand Down Expand Up @@ -3153,7 +3153,7 @@ struct ScalarString<'a> {
///
/// # Errors
/// If a [`Float`](crate::ScalarKind::Float) with an width that isn't 4 or 8
fn glsl_scalar(
const fn glsl_scalar(
kind: crate::ScalarKind,
width: crate::Bytes,
) -> Result<ScalarString<'static>, Error> {
Expand Down Expand Up @@ -3187,7 +3187,7 @@ fn glsl_scalar(
}

/// Helper function that returns the glsl variable name for a builtin
fn glsl_built_in(built_in: crate::BuiltIn, output: bool) -> &'static str {
const fn glsl_built_in(built_in: crate::BuiltIn, output: bool) -> &'static str {
use crate::BuiltIn as Bi;

match built_in {
Expand Down Expand Up @@ -3230,7 +3230,7 @@ fn glsl_built_in(built_in: crate::BuiltIn, output: bool) -> &'static str {
}

/// Helper function that returns the string corresponding to the address space
fn glsl_storage_qualifier(space: crate::AddressSpace) -> Option<&'static str> {
const fn glsl_storage_qualifier(space: crate::AddressSpace) -> Option<&'static str> {
use crate::AddressSpace as As;

match space {
Expand All @@ -3245,7 +3245,7 @@ fn glsl_storage_qualifier(space: crate::AddressSpace) -> Option<&'static str> {
}

/// Helper function that returns the string corresponding to the glsl interpolation qualifier
fn glsl_interpolation(interpolation: crate::Interpolation) -> &'static str {
const fn glsl_interpolation(interpolation: crate::Interpolation) -> &'static str {
use crate::Interpolation as I;

match interpolation {
Expand All @@ -3256,7 +3256,7 @@ fn glsl_interpolation(interpolation: crate::Interpolation) -> &'static str {
}

/// Return the GLSL auxiliary qualifier for the given sampling value.
fn glsl_sampling(sampling: crate::Sampling) -> Option<&'static str> {
const fn glsl_sampling(sampling: crate::Sampling) -> Option<&'static str> {
use crate::Sampling as S;

match sampling {
Expand All @@ -3267,7 +3267,7 @@ fn glsl_sampling(sampling: crate::Sampling) -> Option<&'static str> {
}

/// Helper function that returns the glsl dimension string of [`ImageDimension`](crate::ImageDimension)
fn glsl_dimension(dim: crate::ImageDimension) -> &'static str {
const fn glsl_dimension(dim: crate::ImageDimension) -> &'static str {
use crate::ImageDimension as IDim;

match dim {
Expand All @@ -3279,7 +3279,7 @@ fn glsl_dimension(dim: crate::ImageDimension) -> &'static str {
}

/// Helper function that returns the glsl storage format string of [`StorageFormat`](crate::StorageFormat)
fn glsl_storage_format(format: crate::StorageFormat) -> &'static str {
const fn glsl_storage_format(format: crate::StorageFormat) -> &'static str {
use crate::StorageFormat as Sf;

match format {
Expand Down
12 changes: 6 additions & 6 deletions src/back/hlsl/conv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ impl crate::ScalarKind {
/// Helper function that returns scalar related strings
///
/// <https://docs.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-scalar>
pub(super) fn to_hlsl_str(self, width: crate::Bytes) -> Result<&'static str, Error> {
pub(super) const fn to_hlsl_str(self, width: crate::Bytes) -> Result<&'static str, Error> {
match self {
Self::Sint => Ok("int"),
Self::Uint => Ok("uint"),
Expand All @@ -31,7 +31,7 @@ impl crate::ScalarKind {
}

impl crate::TypeInner {
pub(super) fn is_matrix(&self) -> bool {
pub(super) const fn is_matrix(&self) -> bool {
match *self {
Self::Matrix { .. } => true,
_ => false,
Expand Down Expand Up @@ -113,7 +113,7 @@ impl crate::TypeInner {
}

impl crate::StorageFormat {
pub(super) fn to_hlsl_str(self) -> &'static str {
pub(super) const fn to_hlsl_str(self) -> &'static str {
match self {
Self::R16Float => "float",
Self::R8Unorm => "unorm float",
Expand Down Expand Up @@ -187,7 +187,7 @@ impl crate::BuiltIn {

impl crate::Interpolation {
/// Return the string corresponding to the HLSL interpolation qualifier.
pub(super) fn to_hlsl_str(self) -> Option<&'static str> {
pub(super) const fn to_hlsl_str(self) -> Option<&'static str> {
match self {
// Would be "linear", but it's the default interpolation in SM4 and up
// https://docs.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-struct#interpolation-modifiers-introduced-in-shader-model-4
Expand All @@ -200,7 +200,7 @@ impl crate::Interpolation {

impl crate::Sampling {
/// Return the HLSL auxiliary qualifier for the given sampling value.
pub(super) fn to_hlsl_str(self) -> Option<&'static str> {
pub(super) const fn to_hlsl_str(self) -> Option<&'static str> {
match self {
Self::Center => None,
Self::Centroid => Some("centroid"),
Expand All @@ -211,7 +211,7 @@ impl crate::Sampling {

impl crate::AtomicFunction {
/// Return the HLSL suffix for the `InterlockedXxx` method.
pub(super) fn to_hlsl_suffix(self) -> &'static str {
pub(super) const fn to_hlsl_suffix(self) -> &'static str {
match self {
Self::Add | Self::Subtract => "Add",
Self::And => "And",
Expand Down
6 changes: 3 additions & 3 deletions src/back/hlsl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ pub enum ShaderModel {
}

impl ShaderModel {
pub fn to_str(self) -> &'static str {
pub const fn to_str(self) -> &'static str {
match self {
Self::V5_0 => "5_0",
Self::V5_1 => "5_1",
Expand All @@ -75,7 +75,7 @@ impl ShaderModel {
}

impl crate::ShaderStage {
pub fn to_hlsl_str(self) -> &'static str {
pub const fn to_hlsl_str(self) -> &'static str {
match self {
Self::Vertex => "vs",
Self::Fragment => "ps",
Expand All @@ -85,7 +85,7 @@ impl crate::ShaderStage {
}

impl crate::ImageDimension {
fn to_hlsl_str(self) -> &'static str {
const fn to_hlsl_str(self) -> &'static str {
match self {
Self::D1 => "1D",
Self::D2 => "2D",
Expand Down
2 changes: 1 addition & 1 deletion src/back/hlsl/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ enum InterfaceKey {
}

impl InterfaceKey {
fn new(binding: Option<&crate::Binding>) -> Self {
const fn new(binding: Option<&crate::Binding>) -> Self {
match binding {
Some(&crate::Binding::Location { location, .. }) => Self::Location(location),
Some(&crate::Binding::BuiltIn(built_in)) => Self::BuiltIn(built_in),
Expand Down
16 changes: 8 additions & 8 deletions src/back/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type NeedBakeExpressions = crate::FastHashSet<crate::Handle<crate::Expression>>;
struct Level(usize);

impl Level {
fn next(&self) -> Self {
const fn next(&self) -> Self {
Level(self.0 + 1)
}
}
Expand Down Expand Up @@ -61,7 +61,7 @@ struct FunctionCtx<'a> {

impl<'a> FunctionCtx<'_> {
/// Helper method that generates a [`NameKey`](crate::proc::NameKey) for a local in the current function
fn name_key(&self, local: crate::Handle<crate::LocalVariable>) -> crate::proc::NameKey {
const fn name_key(&self, local: crate::Handle<crate::LocalVariable>) -> crate::proc::NameKey {
match self.ty {
FunctionType::Function(handle) => crate::proc::NameKey::FunctionLocal(handle, local),
FunctionType::EntryPoint(idx) => crate::proc::NameKey::EntryPointLocal(idx, local),
Expand All @@ -72,7 +72,7 @@ impl<'a> FunctionCtx<'_> {
///
/// # Panics
/// - If the function arguments are less or equal to `arg`
fn argument_key(&self, arg: u32) -> crate::proc::NameKey {
const fn argument_key(&self, arg: u32) -> crate::proc::NameKey {
match self.ty {
FunctionType::Function(handle) => crate::proc::NameKey::FunctionArgument(handle, arg),
FunctionType::EntryPoint(ep_index) => {
Expand Down Expand Up @@ -128,7 +128,7 @@ impl crate::Expression {
/// See the [module-level documentation][emit] for details.
///
/// [emit]: index.html#expression-evaluation-time
fn bake_ref_count(&self) -> usize {
const fn bake_ref_count(&self) -> usize {
match *self {
// accesses are never cached, only loads are
crate::Expression::Access { .. } | crate::Expression::AccessIndex { .. } => !0,
Expand All @@ -149,7 +149,7 @@ impl crate::Expression {
/// Helper function that returns the string corresponding to the [`BinaryOperator`](crate::BinaryOperator)
/// # Notes
/// Used by `glsl-out`, `msl-out`, `wgsl-out`, `hlsl-out`.
fn binary_operation_str(op: crate::BinaryOperator) -> &'static str {
const fn binary_operation_str(op: crate::BinaryOperator) -> &'static str {
use crate::BinaryOperator as Bo;
match op {
Bo::Add => "+",
Expand All @@ -176,7 +176,7 @@ fn binary_operation_str(op: crate::BinaryOperator) -> &'static str {
/// Helper function that returns the string corresponding to the [`VectorSize`](crate::VectorSize)
/// # Notes
/// Used by `msl-out`, `wgsl-out`, `hlsl-out`.
fn vector_size_str(size: crate::VectorSize) -> &'static str {
const fn vector_size_str(size: crate::VectorSize) -> &'static str {
match size {
crate::VectorSize::Bi => "2",
crate::VectorSize::Tri => "3",
Expand All @@ -185,7 +185,7 @@ fn vector_size_str(size: crate::VectorSize) -> &'static str {
}

impl crate::TypeInner {
fn is_handle(&self) -> bool {
const fn is_handle(&self) -> bool {
match *self {
crate::TypeInner::Image { .. } | crate::TypeInner::Sampler { .. } => true,
_ => false,
Expand All @@ -197,7 +197,7 @@ impl crate::Statement {
/// Returns true if the statement directly terminates the current block.
///
/// Used to decide whether case blocks require a explicit `break`.
pub fn is_terminator(&self) -> bool {
pub const fn is_terminator(&self) -> bool {
match *self {
crate::Statement::Break
| crate::Statement::Continue
Expand Down
4 changes: 2 additions & 2 deletions src/back/msl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ impl Options {
}
}

fn resolve_push_constants(
const fn resolve_push_constants(
&self,
stage: crate::ShaderStage,
) -> Result<ResolvedBinding, EntryPointError> {
Expand Down Expand Up @@ -428,7 +428,7 @@ impl ResolvedBinding {
}

impl ResolvedInterpolation {
fn from_binding(interpolation: crate::Interpolation, sampling: crate::Sampling) -> Self {
const fn from_binding(interpolation: crate::Interpolation, sampling: crate::Sampling) -> Self {
use crate::Interpolation as I;
use crate::Sampling as S;

Expand Down
Loading

0 comments on commit 7ce98dc

Please sign in to comment.