1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
use core::convert::TryInto;
use core::hint::unreachable_unchecked;
use core::num::TryFromIntError;
/// Defines helper types for more integer indices.
///
/// There are helpers for adapting indices to implement the standard `ops::Index`/`ops::IndexMut`
/// or the crate-wide [`IntSliceIndex`] respectively.
///
/// * [`TryIndex`] uses `TryInto<usize>` to convert an type into an index, slightly making them
///   more convenient to use where the error conditions have been checked through external means or
///   where such panicking is permissible.
/// * [`Int`] wraps an implementor of [`IntSliceIndex`] to turn it into an implementor of
///   `ops::Index` and `ops::IndexMut` as well.
///
/// [`Int`]: struct.Int.html
/// [`IntSliceIndex`]: ../trait.IntSliceIndex.html
/// [`TryIndex`]: struct.TryIndex.html
use core::ops::{Range, RangeFrom, RangeTo};
use core::slice::SliceIndex;

use self::sealed::{IndexSealed, IntoIntIndex};
use super::IntSliceIndex;

/// Sealed traits for making `Int` work as an index, without exposing too much.
///
/// ## Navigating the jungle of traits
/// The main point here is to properly seal the traits. Parts of this are meant to be adopted by
/// `core` at some point, this prevents some unwanted usage. Also note that user defined types
/// convertible with `TryFromIntError` _should_ require slightly more ceremony.
///
/// So the `IntSliceIndex` is a parallel of `core::slice::SliceIndex` and inherited from the
/// exposed `crate::IntSliceIndex`. It also contains the interface which we use internally. We
/// can't define unstable methods, and methods would be inherited despite the hidden sealed trait.
///
/// ```
/// mod sealed {
///     pub trait Seal {
///         fn can_be_observed(&self);
///     }
/// }
///
/// trait Public: sealed::Seal {}
///
/// fn with_public<T: Public>(t: &T) {
///     t.can_be_observed();
/// }
/// ```
///
/// To work around this issue, we use two sealed traits with the same symbols. As neither can be
/// named the necessary disambiguation can not be performed in a downstream crate.
pub(crate) mod sealed {
    use core::num::TryFromIntError;

    /// A trait abstracting slice independent index behaviour, `ops::Index` can use on this.
    ///
    /// This is for two reasons. The panic message is improved by mentioning the original inputs.
    /// But this requires the additional bounds of `Copy`, which is not available for `Range` due
    /// to historical issues. By not exposing this we can always relax this later when, and if,
    /// specialization becomes available to stable Rust.
    pub trait IndexSealed {
        /// Punts the `Copy` bound to the implementor.
        fn copy(&self) -> Self;
        #[cold]
        fn panic_msg(limit: usize, idx: Self) -> !;
    }

    /// Provide one canonical conversion to an index.
    ///
    /// We use this converter to provide the methods of `IntSliceIndex` in the macro expanded
    /// implementation.
    pub trait IntoIntIndex {
        type IntoIndex;
        fn index(self) -> Result<Self::IntoIndex, TryFromIntError>;
    }

    /// Defines actual indexing on a potentially unsized type.
    ///
    /// This is sealed as well, as it contains the otherwise exposed `Index` item whose bounds we
    /// may later want to adjust.
    pub trait IntSliceIndex<T: ?Sized>: Sized {
        type Output: ?Sized;
        fn get(self, slice: &T) -> Option<&Self::Output>;
        fn get_mut(self, slice: &mut T) -> Option<&mut Self::Output>;
        unsafe fn get_unchecked(self, slice: &T) -> &Self::Output;
        unsafe fn get_unchecked_mut(self, slice: &mut T) -> &mut Self::Output;
        fn index(self, slice: &T) -> &Self::Output;
        fn index_mut(self, slice: &mut T) -> &mut Self::Output;
    }

    /// Stops downstream from using the `IntSliceIndex` methods and associate type by having a
    /// redundant pair of the same definitions. Methods do not have the same result type as this
    /// does not influence type deduction and makes it clear that _we_ should never call them.
    /// Hence, all methods provided here are actually unreachable.
    pub trait SealedSliceIndex<T: ?Sized>: IntSliceIndex<T> {
        type Output: ?Sized;
        fn get(self, _: &T) -> ! {
            unreachable!()
        }
        fn get_mut(self, _: &mut T) -> ! {
            unreachable!()
        }
        unsafe fn get_unchecked(self, _: &T) -> ! {
            unreachable!()
        }
        unsafe fn get_unchecked_mut(self, _: &mut T) -> ! {
            unreachable!()
        }
        fn index(self, _: &T) -> ! {
            unreachable!()
        }
        fn index_mut(self, _: &mut T) -> ! {
            unreachable!()
        }
    }

    impl<U: ?Sized, T: IntSliceIndex<U>> SealedSliceIndex<U> for T {
        type Output = <Self as IntSliceIndex<U>>::Output;
    }
}

/// An indexing adaptor for `TryInto`.
///
/// This transparent wrapper allows any type to function as an index as long as it is fallibly
/// convertible to a `usize`. Contrary to the simple integer types, the implementation of
/// `get_unchecked` methods will _not_ unsafely assume that the conversion itself can't fail, only
/// that the resulting index is in-bounds.
///
/// Separating this from the main `IndexType` solves a coherence problem that would occurs when
/// instantiating it with ranges: The standard library is permitted to add new impls of
/// `TryInto<usize>`, for example even for `Range<usize>`. Hence, these two impls would overlap
/// but we would like the first to have another return type than the second. The indirection
/// over this type means that our impls are only generic for `TryIndex<T>` instead and do not
/// overlap.
#[repr(transparent)]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct TryIndex<T>(pub T);

impl<T> TryIndex<T>
where
    T: TryInto<usize>,
    T::Error: Into<TryFromIntError>,
{
    fn into_int_index(self) -> usize {
        match self.0.try_into() {
            Ok(idx) => idx,
            Err(error) => panic!("Invalid index, {}", error.into()),
        }
    }
}

impl<T> IntoIntIndex for TryIndex<T>
where
    T: TryInto<usize>,
    T::Error: Into<TryFromIntError>,
{
    type IntoIndex = usize;
    fn index(self) -> Result<usize, TryFromIntError> {
        self.0.try_into().map_err(Into::into)
    }
}

impl<T, U> sealed::IntSliceIndex<[U]> for TryIndex<T>
where
    T: TryInto<usize>,
    T::Error: Into<TryFromIntError>,
{
    type Output = U;
    fn get(self, slice: &[U]) -> Option<&Self::Output> {
        match IntoIntIndex::index(self) {
            Ok(idx) => slice.get(idx),
            Err(_) => None,
        }
    }
    fn get_mut(self, slice: &mut [U]) -> Option<&mut Self::Output> {
        match IntoIntIndex::index(self) {
            Ok(idx) => slice.get_mut(idx),
            Err(_) => None,
        }
    }
    unsafe fn get_unchecked(self, slice: &[U]) -> &Self::Output {
        // Explicitly do __NOT__ make the conversion itself unchecked.
        slice.get_unchecked(self.into_int_index())
    }
    unsafe fn get_unchecked_mut(self, slice: &mut [U]) -> &mut Self::Output {
        // Explicitly do __NOT__ make the conversion itself unchecked.
        slice.get_unchecked_mut(self.into_int_index())
    }
    fn index(self, slice: &[U]) -> &Self::Output {
        &slice[self.into_int_index()]
    }
    fn index_mut(self, slice: &mut [U]) -> &mut Self::Output {
        &mut slice[self.into_int_index()]
    }
}

impl<T, U> IntSliceIndex<[U]> for TryIndex<T>
where
    T: TryInto<usize>,
    T::Error: Into<TryFromIntError>,
{
}

impl<T, U> core::ops::Index<TryIndex<T>> for [U]
where
    T: TryInto<usize> + IndexSealed,
    T::Error: Into<TryFromIntError>,
{
    type Output = U;
    fn index(&self, idx: TryIndex<T>) -> &U {
        sealed::IntSliceIndex::index(idx, self)
    }
}

impl<T, U> core::ops::IndexMut<TryIndex<T>> for [U]
where
    T: TryInto<usize> + IndexSealed,
    T::Error: Into<TryFromIntError>,
{
    fn index_mut(&mut self, idx: TryIndex<T>) -> &mut Self::Output {
        sealed::IntSliceIndex::index_mut(idx, self)
    }
}

/// An adaptor for `ops::Index` that uses this crate's `IntSliceIndex` instead of the standard one.
///
/// This struct can be used to index a slice with an arbitrary integer type, using the standard
/// indexing syntax. It is also constructed by the [`Int`] method exported in the crate root. The
/// indexing operation will first try to convert the number of a `usize` index and then do the
/// usual indexing.
///
/// [`Int`]: ../fn.Int.html
///
/// ```rust
/// use index_ext::int::Int;
/// let val = [0u8; 2][Int(1u32)];
/// ```
///
/// This is a transparent wrapper.
#[repr(transparent)]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Int<T>(pub T);

impl<T, U> core::ops::Index<Int<T>> for [U]
where
    T: IntSliceIndex<[U]> + IndexSealed,
{
    type Output = <T as sealed::IntSliceIndex<[U]>>::Output;

    fn index(&self, idx: Int<T>) -> &Self::Output {
        <T as sealed::IntSliceIndex<[U]>>::index(idx.0, self)
    }
}

impl<T, U> core::ops::IndexMut<Int<T>> for [U]
where
    T: IntSliceIndex<[U]> + IndexSealed,
{
    fn index_mut(&mut self, idx: Int<T>) -> &mut Self::Output {
        <T as sealed::IntSliceIndex<[U]>>::index_mut(idx.0, self)
    }
}

// Core implementations for the basede types. We implement the `IntoIntIndex` trait for generic
// types so we can reuse them in the concrete macro-derived impls of the sealed IntSliceIndex
// trait.

impl<T: TryInto<usize>> sealed::IntoIntIndex for Range<T>
where
    TryFromIntError: From<T::Error>,
{
    type IntoIndex = Range<usize>;
    fn index(self) -> Result<Range<usize>, TryFromIntError> {
        let Range { start, end } = self;
        let start: usize = start.try_into()?;
        let end: usize = end.try_into()?;
        Ok(start..end)
    }
}

impl<T: TryInto<usize>> sealed::IntoIntIndex for RangeTo<T>
where
    TryFromIntError: From<T::Error>,
{
    type IntoIndex = RangeTo<usize>;
    fn index(self) -> Result<RangeTo<usize>, TryFromIntError> {
        let end: usize = self.end.try_into()?;
        Ok(..end)
    }
}

impl<T: TryInto<usize>> sealed::IntoIntIndex for RangeFrom<T>
where
    TryFromIntError: From<T::Error>,
{
    type IntoIndex = RangeFrom<usize>;
    fn index(self) -> Result<RangeFrom<usize>, TryFromIntError> {
        let start: usize = self.start.try_into()?;
        Ok(start..)
    }
}

macro_rules! slice_index {
($($t:ty),*) => {
    $(slice_index!(@$t);)*
};
(@IntSliceIndex<[U]> for $t:ty: with IntoIntIndex) => {
    impl<U> sealed::IntSliceIndex<[U]> for $t {
        type Output = <<Self as sealed::IntoIntIndex>::IntoIndex as SliceIndex<[U]>>::Output;
        fn get(self, slice: &[U]) -> Option<&Self::Output> {
            match IntoIntIndex::index(self) {
                Ok(idx) => slice.get(idx),
                Err(_) => None,
            }
        }
        fn get_mut(self, slice: &mut [U]) -> Option<&mut Self::Output> {
            match IntoIntIndex::index(self) {
                Ok(idx) => slice.get_mut(idx),
                Err(_) => None,
            }
        }
        unsafe fn get_unchecked(self, slice: &[U]) -> &Self::Output {
            match IntoIntIndex::index(self) {
                Ok(idx) => slice.get_unchecked(idx),
                Err(_) => unreachable_unchecked(),
            }
        }
        unsafe fn get_unchecked_mut(self, slice: &mut [U]) -> &mut Self::Output {
            match IntoIntIndex::index(self) {
                Ok(idx) => slice.get_unchecked_mut(idx),
                Err(_) => unreachable_unchecked(),
            }
        }
        fn index(self, slice: &[U]) -> &Self::Output {
            match sealed::IntSliceIndex::get(IndexSealed::copy(&self), slice) {
                Some(output) => output,
                None => IndexSealed::panic_msg(slice.len(), self),
            }
        }
        fn index_mut(self, slice: &mut [U]) -> &mut Self::Output {
            let len = slice.len();
            match sealed::IntSliceIndex::get_mut(IndexSealed::copy(&self), slice) {
                Some(output) => output,
                None => IndexSealed::panic_msg(len, self),
            }
        }
    }
};
(@IntSliceIndex<str> for $t:ty: with IntoIntIndex) => {
    impl sealed::IntSliceIndex<str> for $t {
        type Output = <<Self as sealed::IntoIntIndex>::IntoIndex as SliceIndex<str>>::Output;
        fn get(self, slice: &str) -> Option<&Self::Output> {
            match IntoIntIndex::index(self) {
                Ok(idx) => slice.get(idx),
                Err(_) => None,
            }
        }
        fn get_mut(self, slice: &mut str) -> Option<&mut Self::Output> {
            match IntoIntIndex::index(self) {
                Ok(idx) => slice.get_mut(idx),
                Err(_) => None,
            }
        }
        unsafe fn get_unchecked(self, slice: &str) -> &Self::Output {
            match IntoIntIndex::index(self) {
                Ok(idx) => slice.get_unchecked(idx),
                Err(_) => unreachable_unchecked(),
            }
        }
        unsafe fn get_unchecked_mut(self, slice: &mut str) -> &mut Self::Output {
            match IntoIntIndex::index(self) {
                Ok(idx) => slice.get_unchecked_mut(idx),
                Err(_) => unreachable_unchecked(),
            }
        }
        fn index(self, slice: &str) -> &Self::Output {
            match sealed::IntSliceIndex::get(IndexSealed::copy(&self), slice) {
                Some(output) => output,
                None => IndexSealed::panic_msg(slice.len(), self),
            }
        }
        fn index_mut(self, slice: &mut str) -> &mut Self::Output {
            let len = slice.len();
            match sealed::IntSliceIndex::get_mut(IndexSealed::copy(&self), slice) {
                Some(output) => output,
                None => IndexSealed::panic_msg(len, self),
            }
        }
    }
};
(@$t:ty) => {
    impl sealed::IntoIntIndex for $t {
        type IntoIndex = usize;
        fn index(self) -> Result<usize, TryFromIntError> {
            Ok(self.try_into()?)
        }
    }

    impl sealed::IndexSealed for $t {
        #[inline(always)]
        fn copy(&self) -> Self { *self }
        #[cold]
        fn panic_msg(len: usize, index: Self) -> ! {
            panic!("index {} out of range for slice of length {}", index, len)
        }
    }

    impl sealed::IndexSealed for Range<$t> {
        #[inline(always)]
        fn copy(&self) -> Self { Range { .. *self } }
        #[cold]
        fn panic_msg(len: usize, index: Self) -> ! {
            panic!("index {} out of range for slice of length {}", index.end, len)
        }
    }

    impl sealed::IndexSealed for RangeFrom<$t> {
        #[inline(always)]
        fn copy(&self) -> Self { RangeFrom { .. *self } }
        #[cold]
        fn panic_msg(len: usize, index: Self) -> ! {
            panic!("index {} out of range for slice of length {}", index.start, len)
        }
    }

    impl sealed::IndexSealed for RangeTo<$t> {
        #[inline(always)]
        fn copy(&self) -> Self { RangeTo { .. *self } }
        #[cold]
        fn panic_msg(len: usize, index: Self) -> ! {
            panic!("index {} out of range for slice of length {}", index.end, len)
        }
    }

    slice_index!(@IntSliceIndex<[U]> for $t: with IntoIntIndex);
    slice_index!(@IntSliceIndex<[U]> for Range<$t>: with IntoIntIndex);
    slice_index!(@IntSliceIndex<[U]> for RangeTo<$t>: with IntoIntIndex);
    slice_index!(@IntSliceIndex<[U]> for RangeFrom<$t>: with IntoIntIndex);
    slice_index!(@IntSliceIndex<str> for Range<$t>: with IntoIntIndex);
    slice_index!(@IntSliceIndex<str> for RangeTo<$t>: with IntoIntIndex);
    slice_index!(@IntSliceIndex<str> for RangeFrom<$t>: with IntoIntIndex);

    impl<U> IntSliceIndex<[U]> for $t {}
    impl<U> IntSliceIndex<[U]> for Range<$t> {}
    impl<U> IntSliceIndex<[U]> for RangeTo<$t> {}
    impl<U> IntSliceIndex<[U]> for RangeFrom<$t> {}

    impl IntSliceIndex<str> for Range<$t> {}
    impl IntSliceIndex<str> for RangeTo<$t> {}
    impl IntSliceIndex<str> for RangeFrom<$t> {}
} }

slice_index!(u8, i8, u16, i16, u32, i32, u64, i64, u128, i128, usize, isize);