Skip to content

Commit

Permalink
Cleaning up a few things
Browse files Browse the repository at this point in the history
  • Loading branch information
mattkleiny committed Jan 22, 2024
1 parent ffb8e6a commit f165143
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 108 deletions.
16 changes: 4 additions & 12 deletions common/src/collections/grid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ pub struct Grid<T> {
impl<T> Grid<T> {
/// 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],
Expand All @@ -19,9 +17,7 @@ impl<T> Grid<T> {

/// 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(),
Expand Down Expand Up @@ -122,17 +118,13 @@ impl<T> Grid<T> {

/// 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());
}

Expand Down
8 changes: 2 additions & 6 deletions common/src/collections/multimap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@ impl<K: Eq + Hash, V> MultiMap<K, V> {

/// 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)
}

Expand All @@ -66,9 +64,7 @@ impl<K: Eq + Hash, V> MultiMap<K, V> {

/// 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);

Expand Down
8 changes: 2 additions & 6 deletions common/src/collections/quadtree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,7 @@ impl<T> QuadTree<T> {
/// 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)
}

Expand All @@ -153,9 +151,7 @@ impl<T> QuadTree<T> {
/// 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<T: Clone>(node: &mut QuadTreeNode<T>, value: T, bounds: Rectangle, depth: usize) -> bool {
if depth > 100 {
panic!("exceeded maximum recursion depth");
Expand Down
4 changes: 1 addition & 3 deletions common/src/collections/ringbuffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ pub struct RingBuffer<T> {
impl<T> RingBuffer<T> {
/// 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],
Expand Down
4 changes: 1 addition & 3 deletions modules/graphics/src/materials.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<TextureSampler>)
where
K: Into<ShaderUniformKey<&'a Texture>>,
{
where K: Into<ShaderUniformKey<&'a Texture>> {
self.uniforms.set_texture(key, texture, sampler);
}

Expand Down
7 changes: 2 additions & 5 deletions modules/graphics/src/shaders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<U> From<&[U]> for ShaderUniform
where
for<'a> &'a U: Into<ShaderUniform>,
where for<'a> &'a U: Into<ShaderUniform>
{
fn from(value: &[U]) -> Self {
Self::Array(value.iter().map(|v| v.into()).collect::<Vec<ShaderUniform>>())
Expand Down Expand Up @@ -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<TextureSampler>)
where
K: Into<ShaderUniformKey<&'a Texture>>,
{
where K: Into<ShaderUniformKey<&'a Texture>> {
let slot = self.allocate_texture_slot(texture);
let uniform = ShaderUniform::Texture(texture.id(), slot, sampler);

Expand Down
90 changes: 39 additions & 51 deletions modules/graphics/src/shaders/lang/shady.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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]
Expand Down Expand Up @@ -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]
Expand All @@ -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]
Expand Down
40 changes: 18 additions & 22 deletions modules/graphics/src/skinning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit f165143

Please sign in to comment.