Skip to content

Rust-ify and refactor#1

Open
marcj303 wants to merge 9 commits intoJack-Sim:mainfrom
marcj303:main
Open

Rust-ify and refactor#1
marcj303 wants to merge 9 commits intoJack-Sim:mainfrom
marcj303:main

Conversation

@marcj303
Copy link

Thanks for the great article on medium. I'm learning Rust and this was a great little exercise. This pull request has 4 commits. It fixes issues noted by clippy and refactors some code to simplify and de-duplicate the logic. Please feel free to review and comment on the merit of each commit. I am happy to split this up if you would like separate pull-requests.

Fix the following clippy recomendations:

the loop variable `i` is only used to index `p`
`#[warn(clippy::needless_range_loop)]` on by default
for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_range_loopclippyneedless_range_loop
main.rs(31, 17): consider using an iterator: `<item>`, `&mut p`

manual implementation of an assign operation
for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#assign_op_patternclippyassign_op_pattern
main.rs(36, 9): replace it with: `counter += 1`

unneeded `return` statement
`#[warn(clippy::needless_return)]` on by default
for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_returnclippyneedless_return
particle.rs(17, 9): remove `return`: `Particle {
redundant field names in struct initialization

`#[warn(clippy::redundant_field_names)]` on by default
for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_field_namesclippyredundant_field_names
particle.rs(18, 13): replace it with: `pos`

manual implementation of an assign operation
`#[warn(clippy::assign_op_pattern)]` on by default
for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#assign_op_patternclippyassign_op_pattern
particle.rs(32, 9): replace it with: `self.pos[0] += self.vel[0]`
Signed-off-by: Marc Jones <marcj303@gmail.com>

fix clippy

Signed-off-by: Marc Jones <marcj303@gmail.com>
Similar to the previous clippy recomendation, use Rust
iterator instead of a for loop to remove particles that have
reached the end of lifetime.

Note that the article mentions that the for loop goes backwards,
but that isn't neccesary for two reasons. [1]

1. The partical vector is a FIFO, not a stack. The first particals
are the oldest, so it should scan front to back.

2. The order of scanning doesn't matter since it is all done before
the screen is redrawn. Since we don't really care about what order,
the particals are removed from the vector,  we can use vector iterator
and simplify the code to retain all particals that are not finished.

[1] https://medium.com/codex/nature-of-rust-particles-40cec0a8c25e

Signed-off-by: Marc Jones <marcj303@gmail.com>
Move adding particals to the vector into the top of the
window.next animation loop. We only need to add particals
in one place.

Signed-off-by: Marc Jones <marcj303@gmail.com>
Use the constants to set the initial partical location
to the center of the width and height.

Signed-off-by: Marc Jones <marcj303@gmail.com>
Port to the speedy2d crate.

Signed-off-by: Marc Jones <marcj303@gmail.com>
We don't need to clone if we borrow self in the finalize
function.

Signed-off-by: Marc Jones <marcj303@gmail.com>
Add Keyboard r,g,b feature to change the colors of the particles.
Keys other than r,g,b turn the particles black.

Add Mouse click to move the particles starting point.

Signed-off-by: Marc Jones <marcj303@gmail.com>
Signed-off-by: Marc Jones <marcj303@gmail.com>
The start_pos dimensions were switched. Set x to WIDTH and y to HEIGHT.

Signed-off-by: Marc Jones <marcj303@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant