Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Index bounds check #98

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Index bounds check #98

wants to merge 1 commit into from

Conversation

smcmurray
Copy link

@smcmurray smcmurray commented Sep 22, 2018

get_index should still vet its arguments.

βœ‹ A similar PR may already be submitted!
Please search πŸ”Ž among the open pull requests before creating one.

βœ‹ If there isn't a similar PR, is there an open issue associated
with what this PR is trying to solve?
If not, let's create one before opening a PR. ✨

Now that you've checked, it's time to create your PR. πŸ“
Thanks for submitting! πŸ™

For more information, see the contributing guide. πŸ‘«

Summary

Explain the motivation for making this change. What existing problem does the pull request solve? πŸ€”

  • πŸ‘― This PR is not a duplicate
  • πŸ“¬ This PR addresses an open issue
  • βœ… This PR has passed CI

get_index should still vet its arguments.
@smcmurray
Copy link
Author

Added issue #99 for this, as directed.

Copy link
Member

@fitzgen fitzgen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for bringing this up @smcmurray

There is no unsafety here (not that you implied it, just noting for posterity), as indexing arrays is bounds checked in Rust.

Given that this is only for catching logic errors, I think it makes most sense to have debug assertions like this:

debug_assert!(row < self.height);
debug_assert!(column < self.width);

Can you send a PR to https://github.com/rustwasm/wasm_game_of_life too?

Thanks again @smcmurray

@SomeoneToIgnore
Copy link
Contributor

I like the way it's implemented in the pull request (using % instead of debug_assert), allowing the javascript code to receive the index for any row and column pair specified without any panics.

Consider an excersize from https://rustwasm.github.io/book/game-of-life/interactivity.html#exercises where a pulsar should be added: if a user clicks close enough to the edge of the universe, the part of the figure to be added will appear on the other side of the universe (since it's periodic).
If we go with the % approach, then implementing this behavior is rather trivial – we simply add some offsets to the clicked point row and column and call get_index for those.
With debug_assert! approach we will be forced to write extra boilerplate to ensure that those offset values are in the universe range instead.

If you're fine with the approach from the PR, I can create a PR into the https://github.com/rustwasm/wasm_game_of_life repo.

@fitzgen
Copy link
Member

fitzgen commented Nov 26, 2018

If we go with the % approach, then implementing this behavior is rather trivial – we simply add some offsets to the clicked point row and column and call get_index for those.

This should be handled further up the stack, before we ever call Universe::get_index.

I can create a PR into the https://github.com/rustwasm/wasm_game_of_life repo.

That would be very appreciated!

@SomeoneToIgnore
Copy link
Contributor

Makes sense, here is the PR: rustwasm/wasm_game_of_life#38

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.

3 participants