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
//! Traits involving collections which index over a particular type.
#[cfg(feature = "std")]
use std::ops;
#[cfg(not(feature = "std"))]
use core::ops;

use self::ops::{Index, IndexMut, Range, RangeFrom, RangeFull, RangeTo};
#[cfg(feature = "nightly")]
use self::ops::{RangeInclusive, RangeToInclusive};

/// Shorthand trait for collections which offer consistent, immutable slicing.
#[cfg(feature = "nightly")]
pub trait IndexRanges<Idx>: Index<Range<Idx>, Output = <Self as Index<RangeFull>>::Output>
                            + Index<RangeTo<Idx>, Output = <Self as Index<RangeFull>>::Output>
                            + Index<RangeFrom<Idx>, Output = <Self as Index<RangeFull>>::Output>
                            + Index<RangeInclusive<Idx>, Output = <Self as Index<RangeFull>>::Output>
                            + Index<RangeToInclusive<Idx>, Output = <Self as Index<RangeFull>>::Output>
                            + Index<RangeFull>
{}
#[cfg(feature = "nightly")]
impl<T: ?Sized, Idx> IndexRanges<Idx> for T
    where T: Index<Range<Idx>, Output = <Self as Index<RangeFull>>::Output>
            + Index<RangeTo<Idx>, Output = <Self as Index<RangeFull>>::Output>
            + Index<RangeFrom<Idx>, Output = <Self as Index<RangeFull>>::Output>
            + Index<RangeInclusive<Idx>, Output = <Self as Index<RangeFull>>::Output>
            + Index<RangeToInclusive<Idx>, Output = <Self as Index<RangeFull>>::Output>
            + Index<RangeFull>
{}

/// Shorthand trait for collections which offer consistent, immutable slicing.
#[cfg(not(feature = "nightly"))]
pub trait IndexRanges<Idx>: Index<Range<Idx>, Output = <Self as Index<RangeFull>>::Output>
                            + Index<RangeTo<Idx>, Output = <Self as Index<RangeFull>>::Output>
                            + Index<RangeFrom<Idx>, Output = <Self as Index<RangeFull>>::Output>
                            + Index<RangeFull>
{}
#[cfg(not(feature = "nightly"))]
impl<T: ?Sized, Idx> IndexRanges<Idx> for T
    where T: Index<Range<Idx>, Output = <Self as Index<RangeFull>>::Output>
            + Index<RangeTo<Idx>, Output = <Self as Index<RangeFull>>::Output>
            + Index<RangeFrom<Idx>, Output = <Self as Index<RangeFull>>::Output>
            + Index<RangeFull>
{}

/// Shorthand trait for collections which offer consistent, mutable slicing.
#[cfg(feature = "nightly")]
pub trait IndexRangesMut<Idx>: IndexMut<Range<Idx>>
                                + IndexMut<RangeTo<Idx>>
                                + IndexMut<RangeFrom<Idx>>
                                + IndexMut<RangeInclusive<Idx>>
                                + IndexMut<RangeToInclusive<Idx>>
                                + IndexMut<RangeFull>
                                + IndexRanges<Idx>
{}
#[cfg(feature = "nightly")]
impl<T: ?Sized, Idx> IndexRangesMut<Idx> for T
    where T: IndexMut<Range<Idx>>
            + IndexMut<RangeTo<Idx>>
            + IndexMut<RangeFrom<Idx>>
            + IndexMut<RangeInclusive<Idx>>
            + IndexMut<RangeToInclusive<Idx>>
            + IndexMut<RangeFull>
            + IndexRanges<Idx>
{}

/// Shorthand trait for collections which offer consistent, mutable slicing.
#[cfg(not(feature = "nightly"))]
pub trait IndexRangesMut<Idx>: IndexMut<Range<Idx>>
                                + IndexMut<RangeTo<Idx>>
                                + IndexMut<RangeFrom<Idx>>
                                + IndexMut<RangeFull>
                                + IndexRanges<Idx>
{}
#[cfg(not(feature = "nightly"))]
impl<T: ?Sized, Idx> IndexRangesMut<Idx> for T
    where T: IndexMut<Range<Idx>>
            + IndexMut<RangeTo<Idx>>
            + IndexMut<RangeFrom<Idx>>
            + IndexMut<RangeFull>
            + IndexRanges<Idx>
{}

/// Trait for collections which can be split into two pieces at a given index.
///
/// Splitting a collection must take a constant amount of time and space.
pub trait SplitAt<Idx>: IndexRanges<Idx> {
    /// Splits the collection into two pieces at the given index.
    ///
    /// # Panics
    ///
    /// Panics if `index` is invalid according to the collection.
    fn split_at(&self, index: Idx) -> (&<Self as Index<RangeFull>>::Output, &<Self as Index<RangeFull>>::Output);
}

/// Trait for collections which can be split into two pieces at a given index.
///
/// Splitting a collection must take a constant amount of time and space.
pub trait SplitAtMut<Idx>: IndexRangesMut<Idx> + SplitAt<Idx> {
    /// Splits the collection into two mutable pieces at the given index.
    ///
    /// # Panics
    ///
    /// Panics if `index` is invalid according to the collection.
    fn split_at_mut(&mut self, index: Idx) -> (&mut <Self as Index<RangeFull>>::Output, &mut <Self as Index<RangeFull>>::Output);
}