-
Notifications
You must be signed in to change notification settings - Fork 28
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
Improve get
and get_mut
method signature, Add From implementations for Vec<Vec<T>> and (Vec<T>, usize)
#41
Conversation
… that guarantees conversion into a usize works This way get_unchecked and get_unchecked_mut are a little more versatile, but still won't randomly panic on you.
…tems from a 2d and (1d, size) types
get
and get_mut
get
and get_mut
method signature, Add From implementations for Vec<Vec<T>> and (Vec<T>, usize)
Looks good to me and very useful. Had some mergers before this one which now causes merge conflicts. Could you please rebase to main? Then I will merge this as well. |
I don't know whether this is intentional but I want to note that having a single generic type for both arguments (e.g. |
Good call! That was not intentional. I'll implement your suggestion when I rebase! |
@becheran, Should be ready to go! I realized I implemented a merge commit instead of a rebase (force of habit, sorry!). I can undo those commits if you feel strongly about rebasing. |
While using this library, I found it difficult to get neighbors because of the possibility of overflows when going negative. For instance, to get the neighbor that is up and to the left of the current cell, I would have to something like this:
This was a lot of code. Ideally, I could just do this:
Unfortunately,
.get
and.get_mut
do not accept anything butusize
, so this would cause an overflow warning.I started adding this to my rust projects:
This trait and implementation allowed me to add new methods called
get_checked
andget_checked_mut
to do what I wanted: pass in an isize or any other thing that could be converted into a number. If it was less than 0, it would return None, which is perfect: there wouldn't be a value there anyway!After implementing this a couple times, I figured that it was useful enough to contribute to this library, hence this PR. (I also cleaned up some of the clippy linter warnings I was getting! And added implentation for From which makes more sense than adding a
from_vec
method, making it possible to potentially remove that function one day)