len-trait 0.4.0

Len trait for collectons.
Documentation
//! 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);
}