Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upAdd a trait for checked indexing #2
Conversation
This comment has been minimized.
This comment has been minimized.
|
I would like to bikeshed the name a bit, |
This comment has been minimized.
This comment has been minimized.
|
The usual name is get and get_mut, like slices do it. This was mentioned on irc today by @Kimundi (not w.r.t arrayvec). |
This comment has been minimized.
This comment has been minimized.
|
These names are already taken though. Maybe |
This comment has been minimized.
This comment has been minimized.
|
Maybe checked index is good then. If we extend this to &str by the way, the has_index check needs to include character boundary checks (is_acceptable_index in odds). I don't think has_index method should have any panicking case? |
This comment has been minimized.
This comment has been minimized.
|
I have a small error there, it will currently panic if you use It would need to include boundary checks for |
This comment has been minimized.
This comment has been minimized.
|
Fixed the mistake. Thanks for your fast response times by the way, it's wonderful! |
bluss
self-assigned this
Sep 12, 2015
This comment has been minimized.
This comment has been minimized.
|
Good catch. I know I said I would easily accept this functionality, just imagined it as a simple function or so. I don't know. I think there should be no possiblity of panic -- I use checked versions in places where you want to avoid panics, a possible panic in the optimizer's analysis may throw it off and miss some other optimizations. |
This comment has been minimized.
This comment has been minimized.
llogiq
commented
Sep 21, 2015
|
Why not |
This comment has been minimized.
This comment has been minimized.
|
It's going to cover both indexing and slicing, just like the Index trait. |
This comment has been minimized.
This comment has been minimized.
|
Maybe we can use just something like this? I'd like to not reuse the trait CheckedIndex<Idx> {
type Output;
fn checked_index(&self, i: Idx) -> Option<&Self::Output>;
} |
This comment has been minimized.
This comment has been minimized.
|
Optionally: Even add |
This comment has been minimized.
This comment has been minimized.
|
The optimizer has no problem with seeing the double bounds check, it will eliminate the panic anyway – I don't think not re-using |
This comment has been minimized.
This comment has been minimized.
|
That's a good point, it's true most of the time. |
This comment has been minimized.
This comment has been minimized.
|
I'm inclined to close this, since I don't agree with the approach. The crate space is free — it doesn't have to be in |
This comment has been minimized.
This comment has been minimized.
|
That's fine with me! |
tbu- commentedSep 11, 2015
No description provided.