From f1651433295d1c22086a551343183c69484122c6 Mon Sep 17 00:00:00 2001 From: Matt Kleinschafer Date: Tue, 23 Jan 2024 10:55:05 +1100 Subject: [PATCH] Cleaning up a few things --- common/src/collections/grid.rs | 16 +--- common/src/collections/multimap.rs | 8 +- common/src/collections/quadtree.rs | 8 +- common/src/collections/ringbuffer.rs | 4 +- modules/graphics/src/materials.rs | 4 +- modules/graphics/src/shaders.rs | 7 +- modules/graphics/src/shaders/lang/shady.rs | 90 ++++++++++------------ modules/graphics/src/skinning.rs | 40 +++++----- rustfmt.toml | 3 + 9 files changed, 72 insertions(+), 108 deletions(-) diff --git a/common/src/collections/grid.rs b/common/src/collections/grid.rs index 6a0d3af4..e8bd14d7 100644 --- a/common/src/collections/grid.rs +++ b/common/src/collections/grid.rs @@ -8,9 +8,7 @@ pub struct Grid { impl Grid { /// Creates a new grid with the given dimensions. pub fn new(width: usize, height: usize) -> Self - where - T: Clone + Default, - { + where T: Clone + Default { Self { stride: width, items: vec![T::default(); width * height], @@ -19,9 +17,7 @@ impl Grid { /// Converts the given slice into a grid. pub fn from_slice(stride: usize, slice: &[T]) -> Self - where - T: Clone, - { + where T: Clone { Self { stride, items: slice.to_vec(), @@ -122,17 +118,13 @@ impl Grid { /// Fills the grid with the given value. pub fn fill(&mut self, value: T) - where - T: Clone, - { + where T: Clone { self.items.fill(value); } /// Clears the grid. pub fn clear(&mut self) - where - T: Clone + Default, - { + where T: Clone + Default { self.fill(T::default()); } diff --git a/common/src/collections/multimap.rs b/common/src/collections/multimap.rs index 83b6bb4f..edfa8521 100644 --- a/common/src/collections/multimap.rs +++ b/common/src/collections/multimap.rs @@ -40,9 +40,7 @@ impl MultiMap { /// Determines if the given key-value pair is contained in the map. pub fn contains_value(&self, key: &K, value: &V) -> bool - where - V: PartialEq, - { + where V: PartialEq { self.entries.get(key).map(|vec| vec.contains(value)).unwrap_or(false) } @@ -66,9 +64,7 @@ impl MultiMap { /// Removes the given key-value pair from the map. pub fn remove(&mut self, key: &K, value: V) - where - V: PartialEq, - { + where V: PartialEq { if let Some(entries) = self.entries.get_mut(key) { entries.retain(|v| v != &value); diff --git a/common/src/collections/quadtree.rs b/common/src/collections/quadtree.rs index ec600002..45882740 100644 --- a/common/src/collections/quadtree.rs +++ b/common/src/collections/quadtree.rs @@ -133,9 +133,7 @@ impl QuadTree { /// This involves iterating over all the nodes in the [`QuadTree`], so it is /// not recommended for use in performance-critical code. pub fn contains(&self, value: T) -> bool - where - T: PartialEq, - { + where T: PartialEq { self.iter().any(|(v, _)| v == &value) } @@ -153,9 +151,7 @@ impl QuadTree { /// If you instead wish to move an item within the tree, use the [`update`] /// method instead. pub fn insert(&mut self, value: T, bounds: Rectangle) - where - T: Clone, - { + where T: Clone { fn insert_recursive(node: &mut QuadTreeNode, value: T, bounds: Rectangle, depth: usize) -> bool { if depth > 100 { panic!("exceeded maximum recursion depth"); diff --git a/common/src/collections/ringbuffer.rs b/common/src/collections/ringbuffer.rs index 5c8f9314..69912ae1 100644 --- a/common/src/collections/ringbuffer.rs +++ b/common/src/collections/ringbuffer.rs @@ -11,9 +11,7 @@ pub struct RingBuffer { impl RingBuffer { /// Creates a new ring buffer with the given capacity. pub fn new(capacity: usize) -> Self - where - T: Clone, - { + where T: Clone { Self { cursor: 0, elements: vec![None; capacity], diff --git a/modules/graphics/src/materials.rs b/modules/graphics/src/materials.rs index 6e926b46..b8b306a6 100644 --- a/modules/graphics/src/materials.rs +++ b/modules/graphics/src/materials.rs @@ -121,9 +121,7 @@ impl Material { /// Sets the given [`UniformKey`] with a single texture. pub fn set_texture<'a, K>(&'a mut self, key: K, texture: &Texture, sampler: Option) - where - K: Into>, - { + where K: Into> { self.uniforms.set_texture(key, texture, sampler); } diff --git a/modules/graphics/src/shaders.rs b/modules/graphics/src/shaders.rs index 315e83d5..88577827 100644 --- a/modules/graphics/src/shaders.rs +++ b/modules/graphics/src/shaders.rs @@ -270,8 +270,7 @@ pub enum ShaderUniform { /// Allow for the conversion of a slice of values into a shader uniform array, /// provided all of the values can be individually converted into a uniform. impl From<&[U]> for ShaderUniform -where - for<'a> &'a U: Into, +where for<'a> &'a U: Into { fn from(value: &[U]) -> Self { Self::Array(value.iter().map(|v| v.into()).collect::>()) @@ -346,9 +345,7 @@ impl ShaderUniformSet { /// Sets the given [`UniformKey`] as a uniform with a single texture in the /// set. pub fn set_texture<'a, K>(&'a mut self, key: K, texture: &Texture, sampler: Option) - where - K: Into>, - { + where K: Into> { let slot = self.allocate_texture_slot(texture); let uniform = ShaderUniform::Texture(texture.id(), slot, sampler); diff --git a/modules/graphics/src/shaders/lang/shady.rs b/modules/graphics/src/shaders/lang/shady.rs index b8b07fe5..d43a0658 100644 --- a/modules/graphics/src/shaders/lang/shady.rs +++ b/modules/graphics/src/shaders/lang/shady.rs @@ -818,18 +818,15 @@ mod parser { let stream = TokenStream::parse(code).unwrap(); - assert_eq!( - stream.tokens, - vec![ - Token::Integer(1), - Token::BinaryOperator(BinaryOperator::Add), - Token::Integer(2), - Token::BinaryOperator(BinaryOperator::Multiply), - Token::Float(std::f32::consts::PI), - Token::BinaryOperator(BinaryOperator::Subtract), - Token::Integer(4), - ] - ); + assert_eq!(stream.tokens, vec![ + Token::Integer(1), + Token::BinaryOperator(BinaryOperator::Add), + Token::Integer(2), + Token::BinaryOperator(BinaryOperator::Multiply), + Token::Float(std::f32::consts::PI), + Token::BinaryOperator(BinaryOperator::Subtract), + Token::Integer(4), + ]); } #[test] @@ -838,18 +835,15 @@ mod parser { let stream = TokenStream::parse(code).unwrap(); - assert_eq!( - stream.tokens, - vec![ - Token::Keyword("let".to_string()), - Token::Identifier("x".to_string()), - Token::BinaryOperator(BinaryOperator::Equal), - Token::Integer(1), - Token::BinaryOperator(BinaryOperator::Add), - Token::Integer(2), - Token::Semicolon, - ] - ); + assert_eq!(stream.tokens, vec![ + Token::Keyword("let".to_string()), + Token::Identifier("x".to_string()), + Token::BinaryOperator(BinaryOperator::Equal), + Token::Integer(1), + Token::BinaryOperator(BinaryOperator::Add), + Token::Integer(2), + Token::Semicolon, + ]); } #[test] @@ -930,18 +924,15 @@ mod parser { let kernel = Kernel::parse(code).unwrap(); - assert_eq!( - kernel, - Kernel { - kind: KernelKind::Fragment, - name: "fragment".to_string(), - statements: vec![Statement::Return(Expression::Binary( - Box::new(Expression::Literal(Literal::Integer(1))), - BinaryOperator::Add, - Box::new(Expression::Literal(Literal::Integer(2))), - ))], - } - ); + assert_eq!(kernel, Kernel { + kind: KernelKind::Fragment, + name: "fragment".to_string(), + statements: vec![Statement::Return(Expression::Binary( + Box::new(Expression::Literal(Literal::Integer(1))), + BinaryOperator::Add, + Box::new(Expression::Literal(Literal::Integer(2))), + ))], + }); } #[test] @@ -956,21 +947,18 @@ mod parser { let module = Module::parse(code).unwrap(); - assert_eq!( - module, - Module { - kind: ModuleKind::Canvas, - kernels: vec![Kernel { - kind: KernelKind::Fragment, - name: "fragment".to_string(), - statements: vec![Statement::Return(Expression::Binary( - Box::new(Expression::Literal(Literal::Integer(1))), - BinaryOperator::Add, - Box::new(Expression::Literal(Literal::Integer(2))), - ))], - }], - } - ); + assert_eq!(module, Module { + kind: ModuleKind::Canvas, + kernels: vec![Kernel { + kind: KernelKind::Fragment, + name: "fragment".to_string(), + statements: vec![Statement::Return(Expression::Binary( + Box::new(Expression::Literal(Literal::Integer(1))), + BinaryOperator::Add, + Box::new(Expression::Literal(Literal::Integer(2))), + ))], + }], + }); } #[test] diff --git a/modules/graphics/src/skinning.rs b/modules/graphics/src/skinning.rs index eda1cc83..8baa6846 100644 --- a/modules/graphics/src/skinning.rs +++ b/modules/graphics/src/skinning.rs @@ -350,28 +350,24 @@ mod tests { #[test] fn animation_should_insert_keyframes_in_chronological_order() { - let animation = Animation::from_keyframes( - "Idle".to_string(), - TimeSpan::from_seconds(3.), - vec![ - Keyframe { - normalised_time: 1., - transform: Mat4::IDENTITY, - }, - Keyframe { - normalised_time: 0.7, - transform: Mat4::IDENTITY, - }, - Keyframe { - normalised_time: 0.6, - transform: Mat4::IDENTITY, - }, - Keyframe { - normalised_time: 0., - transform: Mat4::IDENTITY, - }, - ], - ); + let animation = Animation::from_keyframes("Idle".to_string(), TimeSpan::from_seconds(3.), vec![ + Keyframe { + normalised_time: 1., + transform: Mat4::IDENTITY, + }, + Keyframe { + normalised_time: 0.7, + transform: Mat4::IDENTITY, + }, + Keyframe { + normalised_time: 0.6, + transform: Mat4::IDENTITY, + }, + Keyframe { + normalised_time: 0., + transform: Mat4::IDENTITY, + }, + ]); assert_eq!(animation.keyframes[0].normalised_time, 0.); assert_eq!(animation.keyframes[1].normalised_time, 0.6); diff --git a/rustfmt.toml b/rustfmt.toml index 685d6db3..438149e2 100644 --- a/rustfmt.toml +++ b/rustfmt.toml @@ -4,3 +4,6 @@ max_width = 120 wrap_comments = true group_imports = "StdExternalCrate" imports_granularity = "Crate" +where_single_line = true +normalize_comments = true +overflow_delimited_expr = true