-
Notifications
You must be signed in to change notification settings - Fork 329
Add support for slicing with inclusive ranges #467
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
Add support for slicing with inclusive ranges #467
Conversation
Rust 1.26 added support for inclusive ranges, which can be created with syntax `start..=end` or `..=end`. This commit bumps the required Rust version to 1.27 because the `.start()` and `.end()` methods of `RangeInclusive` were added only in 1.27.
f90f843
to
4405d36
Compare
I just pushed a new version because it turns out that the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some questions about macros, but apart from that, lgtm.
src/slice.rs
Outdated
@@ -453,6 +502,12 @@ impl<D1: Dimension, T> SliceNextDim<D1, D1::Larger> for Range<T> { | |||
} | |||
} | |||
|
|||
impl<D1: Dimension, T> SliceNextDim<D1, D1::Larger> for RangeInclusive<T> { | |||
fn next_dim(&self, _: PhantomData<D1>) -> PhantomData<D1::Larger> { | |||
PhantomData |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another macro question :) From line 499 to 527, would it be possible to create a macro for that? next_dim
is implemented for all types of Range
except RangeFull
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it is a lot more concise with a macro. Thanks!
step: 1, | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Line 70 to 129 and 265 to 324. do you think it would a good place for a macro? They are already macros, but it could be a merge of impl_slice_from_index_type
and impl_sliceorindex_from_index_type
. The only change in all those lines is Slice
and SliceOrIndex
.
It's a real question btw. Maybe too much macro make the code unreadable or un-fun or wathever. Or maybe the special case From<$index>
make this more complex?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've merged the macros as you've suggested, which substantially reduced the duplication. The special case is not an issue; I've kept a separate macro for it.
Thanks for pointing this out!
My usual attention for inclusive ranges would be to overflow issues, and I haven't really looked closely enough at that here |
A few notes:
|
Rust 1.26 added support for inclusive ranges, which can be created with syntax
start..=end
or..=end
.The behavior of this implementation is a little weird in the rare case that
range.end == ::std::isize::MAX
due to overflow, but without changing the representation ofSlice
andSliceOrIndex
, I don't see a good way to handle that case cleanly. This shouldn't be a significant issue in practice because:range.end == ::std::isize::MAX
.slice.end = 0
, which will result in a panic when slicing unlessslice.start == 0
, which will result in an empty slice. The panic is reasonable behavior. The empty slice behavior in theslice.start == 0
case isn't ideal, but I wouldn't expect it to result in any significant problems.