Skip to content

Commit 6ab3f13

Browse files
committed
Refactor slicing implementation macros
1 parent 4405d36 commit 6ab3f13

File tree

1 file changed

+79
-161
lines changed

1 file changed

+79
-161
lines changed

src/slice.rs

Lines changed: 79 additions & 161 deletions
Original file line numberDiff line numberDiff line change
@@ -67,82 +67,6 @@ impl Slice {
6767
}
6868
}
6969

70-
macro_rules! impl_slice_from_index_type {
71-
($index:ty) => {
72-
impl From<Range<$index>> for Slice {
73-
#[inline]
74-
fn from(r: Range<$index>) -> Slice {
75-
Slice {
76-
start: r.start as isize,
77-
end: Some(r.end as isize),
78-
step: 1,
79-
}
80-
}
81-
}
82-
83-
impl From<RangeInclusive<$index>> for Slice {
84-
#[inline]
85-
fn from(r: RangeInclusive<$index>) -> Slice {
86-
let end = *r.end() as isize;
87-
Slice {
88-
start: *r.start() as isize,
89-
end: if end == -1 { None } else { Some(end + 1) },
90-
step: 1,
91-
}
92-
}
93-
}
94-
95-
impl From<RangeFrom<$index>> for Slice {
96-
#[inline]
97-
fn from(r: RangeFrom<$index>) -> Slice {
98-
Slice {
99-
start: r.start as isize,
100-
end: None,
101-
step: 1,
102-
}
103-
}
104-
}
105-
106-
impl From<RangeTo<$index>> for Slice {
107-
#[inline]
108-
fn from(r: RangeTo<$index>) -> Slice {
109-
Slice {
110-
start: 0,
111-
end: Some(r.end as isize),
112-
step: 1,
113-
}
114-
}
115-
}
116-
117-
impl From<RangeToInclusive<$index>> for Slice {
118-
#[inline]
119-
fn from(r: RangeToInclusive<$index>) -> Slice {
120-
let end = r.end as isize;
121-
Slice {
122-
start: 0,
123-
end: if end == -1 { None } else { Some(end + 1) },
124-
step: 1,
125-
}
126-
}
127-
}
128-
};
129-
}
130-
131-
impl_slice_from_index_type!(isize);
132-
impl_slice_from_index_type!(usize);
133-
impl_slice_from_index_type!(i32);
134-
135-
impl From<RangeFull> for Slice {
136-
#[inline]
137-
fn from(_: RangeFull) -> Slice {
138-
Slice {
139-
start: 0,
140-
end: None,
141-
step: 1,
142-
}
143-
}
144-
}
145-
14670
/// A slice (range with step) or an index.
14771
///
14872
/// See also the [`s![]`](macro.s!.html) macro for a convenient way to create a
@@ -244,76 +168,58 @@ impl fmt::Display for SliceOrIndex {
244168
}
245169
}
246170

247-
impl From<Slice> for SliceOrIndex {
248-
#[inline]
249-
fn from(s: Slice) -> SliceOrIndex {
250-
SliceOrIndex::Slice {
251-
start: s.start,
252-
end: s.end,
253-
step: s.step,
254-
}
255-
}
256-
}
257-
258-
macro_rules! impl_sliceorindex_from_index_type {
259-
($index:ty) => {
260-
impl From<$index> for SliceOrIndex {
171+
macro_rules! impl_slice_variant_from_range {
172+
($self:ty, $constructor:path, $index:ty) => {
173+
impl From<Range<$index>> for $self {
261174
#[inline]
262-
fn from(r: $index) -> SliceOrIndex {
263-
SliceOrIndex::Index(r as isize)
264-
}
265-
}
266-
267-
impl From<Range<$index>> for SliceOrIndex {
268-
#[inline]
269-
fn from(r: Range<$index>) -> SliceOrIndex {
270-
SliceOrIndex::Slice {
175+
fn from(r: Range<$index>) -> $self {
176+
$constructor {
271177
start: r.start as isize,
272178
end: Some(r.end as isize),
273179
step: 1,
274180
}
275181
}
276182
}
277183

278-
impl From<RangeInclusive<$index>> for SliceOrIndex {
184+
impl From<RangeInclusive<$index>> for $self {
279185
#[inline]
280-
fn from(r: RangeInclusive<$index>) -> SliceOrIndex {
186+
fn from(r: RangeInclusive<$index>) -> $self {
281187
let end = *r.end() as isize;
282-
SliceOrIndex::Slice {
188+
$constructor {
283189
start: *r.start() as isize,
284190
end: if end == -1 { None } else { Some(end + 1) },
285191
step: 1,
286192
}
287193
}
288194
}
289195

290-
impl From<RangeFrom<$index>> for SliceOrIndex {
196+
impl From<RangeFrom<$index>> for $self {
291197
#[inline]
292-
fn from(r: RangeFrom<$index>) -> SliceOrIndex {
293-
SliceOrIndex::Slice {
198+
fn from(r: RangeFrom<$index>) -> $self {
199+
$constructor {
294200
start: r.start as isize,
295201
end: None,
296202
step: 1,
297203
}
298204
}
299205
}
300206

301-
impl From<RangeTo<$index>> for SliceOrIndex {
207+
impl From<RangeTo<$index>> for $self {
302208
#[inline]
303-
fn from(r: RangeTo<$index>) -> SliceOrIndex {
304-
SliceOrIndex::Slice {
209+
fn from(r: RangeTo<$index>) -> $self {
210+
$constructor {
305211
start: 0,
306212
end: Some(r.end as isize),
307213
step: 1,
308214
}
309215
}
310216
}
311217

312-
impl From<RangeToInclusive<$index>> for SliceOrIndex {
218+
impl From<RangeToInclusive<$index>> for $self {
313219
#[inline]
314-
fn from(r: RangeToInclusive<$index>) -> SliceOrIndex {
220+
fn from(r: RangeToInclusive<$index>) -> $self {
315221
let end = r.end as isize;
316-
SliceOrIndex::Slice {
222+
$constructor {
317223
start: 0,
318224
end: if end == -1 { None } else { Some(end + 1) },
319225
step: 1,
@@ -322,10 +228,23 @@ macro_rules! impl_sliceorindex_from_index_type {
322228
}
323229
};
324230
}
231+
impl_slice_variant_from_range!(Slice, Slice, isize);
232+
impl_slice_variant_from_range!(Slice, Slice, usize);
233+
impl_slice_variant_from_range!(Slice, Slice, i32);
234+
impl_slice_variant_from_range!(SliceOrIndex, SliceOrIndex::Slice, isize);
235+
impl_slice_variant_from_range!(SliceOrIndex, SliceOrIndex::Slice, usize);
236+
impl_slice_variant_from_range!(SliceOrIndex, SliceOrIndex::Slice, i32);
325237

326-
impl_sliceorindex_from_index_type!(isize);
327-
impl_sliceorindex_from_index_type!(usize);
328-
impl_sliceorindex_from_index_type!(i32);
238+
impl From<RangeFull> for Slice {
239+
#[inline]
240+
fn from(_: RangeFull) -> Slice {
241+
Slice {
242+
start: 0,
243+
end: None,
244+
step: 1,
245+
}
246+
}
247+
}
329248

330249
impl From<RangeFull> for SliceOrIndex {
331250
#[inline]
@@ -338,6 +257,31 @@ impl From<RangeFull> for SliceOrIndex {
338257
}
339258
}
340259

260+
impl From<Slice> for SliceOrIndex {
261+
#[inline]
262+
fn from(s: Slice) -> SliceOrIndex {
263+
SliceOrIndex::Slice {
264+
start: s.start,
265+
end: s.end,
266+
step: s.step,
267+
}
268+
}
269+
}
270+
271+
macro_rules! impl_sliceorindex_from_index {
272+
($index:ty) => {
273+
impl From<$index> for SliceOrIndex {
274+
#[inline]
275+
fn from(r: $index) -> SliceOrIndex {
276+
SliceOrIndex::Index(r as isize)
277+
}
278+
}
279+
};
280+
}
281+
impl_sliceorindex_from_index!(isize);
282+
impl_sliceorindex_from_index!(usize);
283+
impl_sliceorindex_from_index!(i32);
284+
341285
/// Represents all of the necessary information to perform a slice.
342286
///
343287
/// The type `T` is typically `[SliceOrIndex; n]`, `[SliceOrIndex]`, or
@@ -476,61 +420,35 @@ pub trait SliceNextDim<D1, D2> {
476420
fn next_dim(&self, PhantomData<D1>) -> PhantomData<D2>;
477421
}
478422

479-
impl<D1: Dimension> SliceNextDim<D1, D1::Larger> for Slice {
480-
fn next_dim(&self, _: PhantomData<D1>) -> PhantomData<D1::Larger> {
481-
PhantomData
482-
}
483-
}
484-
485-
macro_rules! impl_slicenextdim_for_index_type {
486-
($index:ty) => {
487-
impl<D1: Dimension> SliceNextDim<D1, D1> for $index {
423+
macro_rules! impl_slicenextdim_equal {
424+
($self:ty) => {
425+
impl<D1: Dimension> SliceNextDim<D1, D1> for $self {
488426
fn next_dim(&self, _: PhantomData<D1>) -> PhantomData<D1> {
489427
PhantomData
490428
}
491429
}
492430
}
493431
}
494-
495-
impl_slicenextdim_for_index_type!(isize);
496-
impl_slicenextdim_for_index_type!(usize);
497-
impl_slicenextdim_for_index_type!(i32);
498-
499-
impl<D1: Dimension, T> SliceNextDim<D1, D1::Larger> for Range<T> {
500-
fn next_dim(&self, _: PhantomData<D1>) -> PhantomData<D1::Larger> {
501-
PhantomData
502-
}
503-
}
504-
505-
impl<D1: Dimension, T> SliceNextDim<D1, D1::Larger> for RangeInclusive<T> {
506-
fn next_dim(&self, _: PhantomData<D1>) -> PhantomData<D1::Larger> {
507-
PhantomData
508-
}
509-
}
510-
511-
impl<D1: Dimension, T> SliceNextDim<D1, D1::Larger> for RangeFrom<T> {
512-
fn next_dim(&self, _: PhantomData<D1>) -> PhantomData<D1::Larger> {
513-
PhantomData
514-
}
515-
}
516-
517-
impl<D1: Dimension, T> SliceNextDim<D1, D1::Larger> for RangeTo<T> {
518-
fn next_dim(&self, _: PhantomData<D1>) -> PhantomData<D1::Larger> {
519-
PhantomData
520-
}
521-
}
522-
523-
impl<D1: Dimension, T> SliceNextDim<D1, D1::Larger> for RangeToInclusive<T> {
524-
fn next_dim(&self, _: PhantomData<D1>) -> PhantomData<D1::Larger> {
525-
PhantomData
526-
}
527-
}
528-
529-
impl<D1: Dimension> SliceNextDim<D1, D1::Larger> for RangeFull {
530-
fn next_dim(&self, _: PhantomData<D1>) -> PhantomData<D1::Larger> {
531-
PhantomData
432+
impl_slicenextdim_equal!(isize);
433+
impl_slicenextdim_equal!(usize);
434+
impl_slicenextdim_equal!(i32);
435+
436+
macro_rules! impl_slicenextdim_larger {
437+
(($($generics:tt)*), $self:ty) => {
438+
impl<D1: Dimension, $($generics),*> SliceNextDim<D1, D1::Larger> for $self {
439+
fn next_dim(&self, _: PhantomData<D1>) -> PhantomData<D1::Larger> {
440+
PhantomData
441+
}
442+
}
532443
}
533444
}
445+
impl_slicenextdim_larger!((T), Range<T>);
446+
impl_slicenextdim_larger!((T), RangeInclusive<T>);
447+
impl_slicenextdim_larger!((T), RangeFrom<T>);
448+
impl_slicenextdim_larger!((T), RangeTo<T>);
449+
impl_slicenextdim_larger!((T), RangeToInclusive<T>);
450+
impl_slicenextdim_larger!((), RangeFull);
451+
impl_slicenextdim_larger!((), Slice);
534452

535453
/// Slice argument constructor.
536454
///

0 commit comments

Comments
 (0)