len-trait 0.2.5

Len trait for collectons.
#[cfg(feature = "no-std")]
use core::hash::Hash;

#[cfg(not(feature = "no-std"))]
use std::hash::Hash;

macro_rules! do_impl {
    (Len for $name:ident; $($gen:tt)*) => {
        do_impl!(Len for $name, $name; $($gen)*);
    };
    (Len for $name:ident, $impl_for:ty; $($gen:tt)*) => {
        impl<$($gen)*> $crate::Len for $impl_for {
            fn len(&self) -> usize {
                $name::len(self)
            }
            fn is_empty(&self) -> bool {
                $name::is_empty(self)
            }
        }
    };
    (LenZero for $name:ident; $($gen:tt)*) => {
        do_impl!(LenZero for $name, $name; $($gen)*);
    };
    (LenZero for $name:ident, $impl_for:ty; $($gen:tt)*) => {
        do_impl!(Len for $name, $impl_for; $($gen)*);
        impl<$($gen)*> $crate::LenZero for $impl_for {
            fn clear(&mut self) {
                $name::clear(self)
            }
        }
    };
    (LenMut for $name:ident; $($gen:tt)*) => {
        do_impl!(LenMut for $name, $name; $($gen)*);
    };
    (LenMut for $name:ident, $impl_for:ty; $($gen:tt)*) => {
        do_impl!(LenZero for $name, $impl_for; $($gen)*);
        impl<$($gen)*> $crate::LenMut for $impl_for {
            fn truncate(&mut self, len: usize) {
                $name::truncate(self, len)
            }
        }
    };
    (Capacity for $name:ident; $($gen:tt)*) => {
        do_impl!(Capacity for $name, $name; $($gen)*);
    };
    (Capacity for $name:ident, $impl_for:ty; $($gen:tt)*) => {
        impl<$($gen)*> $crate::Capacity for $impl_for {
            fn capacity(&self) -> usize {
                $name::capacity(self)
            }
        }
    };
    (DefaultCapacity for $name:ident; $($gen:tt)*) => {
        do_impl!(DefaultCapacity for $name, $name; $($gen)*);
    };
    (DefaultCapacity for $name:ident, $impl_for:ty; $($gen:tt)*) => {
        do_impl!(Capacity for $name, $impl_for; $($gen)*);
        impl<$($gen)*> $crate::DefaultCapacity for $impl_for {
            fn default_capacity(capacity: usize) -> Self {
                $name::with_capacity(capacity)
            }
        }
    };
    (CapacityMut for $name:ident; $($gen:tt)*) => {
        do_impl!(CapacityMut for $name, $name; $($gen)*);
    };
    (CapacityMut for $name:ident, $impl_for:ty; $($gen:tt)*) => {
        do_impl!(DefaultCapacity for $name, $impl_for; $($gen)*);
        impl<$($gen)*> $crate::CapacityMut for $impl_for {
            fn reserve(&mut self, additional: usize) {
                $name::reserve(self, additional)
            }
            fn reserve_exact(&mut self, additional: usize) {
                $name::reserve_exact(self, additional)
            }
            fn shrink_to_fit(&mut self) {
                $name::shrink_to_fit(self)
            }
        }
    };
    (SplitAt for $name:ident; $($gen:tt)*) => {
        do_impl!(SplitAt for $name, $name; $($gen)*);
    };
    (SplitAt for $name:ident, $impl_for:ty; $($gen:tt)*) => {
        impl<$($gen)*> $crate::SplitAt for $impl_for {
            fn split_at(&self, index: usize) -> (&Self, &Self) {
                $name::split_at(self, index)
            }

            fn split_at_mut(&mut self, index: usize) -> (&mut Self, &mut Self) {
                $name::split_at_mut (self, index)
            }
        }
    };
    (SplitOff for $name:ident; $($gen:tt)*) => {
        do_impl!(SplitOff for $name, $name; $($gen)*);
    };
    (SplitOff for $name:ident, $impl_for:ty; $($gen:tt)*) => {
        impl<$($gen)*> $crate::SplitOff for $impl_for {
            fn split_off(&mut self, index: usize) -> Self {
                $name::split_off(self, index)
            }
        }
    };
}

mod core {
    type Slice<T> = [T];

    do_impl!(Len for Slice, Slice<T>; T);
    do_impl!(SplitAt for Slice, Slice<T>; T);

    do_impl!(Len for str; );
    do_impl!(SplitAt for str; );
}

#[cfg(not(feature = "no-std"))]
mod std {
    use std::ffi;
    use super::super::Len;

    impl Len for ffi::CStr {
        fn len(&self) -> usize {
            self.to_bytes().len()
        }
        fn is_empty(&self) -> bool {
            self.to_bytes().is_empty()
        }
    }

    impl Len for ffi::CString {
        fn len(&self) -> usize {
            self.as_bytes().len()
        }
        fn is_empty(&self) -> bool {
            self.as_bytes().is_empty()
        }
    }
    // TODO: LenZero for CString
    // TODO: LenMut for CString

    use self::ffi::OsStr;
    do_impl!(Len for OsStr; );

    use self::ffi::OsString;
    impl Len for OsString {
        fn len(&self) -> usize {
            OsStr::len(self)
        }
        fn is_empty(&self) -> bool {
            OsStr::is_empty(self)
        }
    }
    // TODO: LenZero for OsString
    // TODO: LenMut for OsString
    do_impl!(DefaultCapacity for OsString; );
    // TODO: CapacityMut for OsString
}

#[cfg(feature = "collections")]
mod collections {
    #[cfg(feature = "no-std")]
    use collections;
    #[cfg(not(feature = "no-std"))]
    use std::collections;

    use super::super::CapacityMut;

    use self::collections::BTreeMap;
    do_impl!(LenZero for BTreeMap, BTreeMap<K, V>; K: Ord, V);

    use self::collections::BTreeSet;
    do_impl!(LenZero for BTreeSet, BTreeSet<T>; T: Ord);

    use self::collections::BinaryHeap;
    do_impl!(LenZero for BinaryHeap, BinaryHeap<T>; T: Ord);
    do_impl!(CapacityMut for BinaryHeap, BinaryHeap<T>; T: Ord);

    use self::collections::HashMap;
    do_impl!(LenZero for HashMap, HashMap<K, V>; K: Eq + super::Hash, V);
    do_impl!(DefaultCapacity for HashMap, HashMap<K, V>; K: Eq + super::Hash, V);
    impl<K: Eq + super::Hash, V> CapacityMut for HashMap<K, V> {
        fn reserve(&mut self, additional: usize) {
            HashMap::reserve(self, additional)
        }
        fn reserve_exact(&mut self, additional: usize) {
            HashMap::reserve(self, additional)
        }
        fn shrink_to_fit(&mut self) {
            HashMap::shrink_to_fit(self)
        }
    }

    use self::collections::HashSet;
    do_impl!(LenZero for HashSet, HashSet<T>; T: Eq + super::Hash);
    do_impl!(DefaultCapacity for HashSet, HashSet<T>; T: Eq + super::Hash);
    impl<T: Eq + super::Hash> CapacityMut for HashSet<T> {
        fn reserve(&mut self, additional: usize) {
            HashSet::reserve(self, additional)
        }
        fn reserve_exact(&mut self, additional: usize) {
            HashSet::reserve(self, additional)
        }
        fn shrink_to_fit(&mut self) {
            HashSet::shrink_to_fit(self)
        }
    }

    use self::collections::LinkedList;
    do_impl!(LenZero for LinkedList, LinkedList<T>; T);

    #[cfg(feature = "no-std")]
    use self::collections::String;
    do_impl!(LenMut for String; );
    do_impl!(CapacityMut for String; );
    do_impl!(SplitOff for String; );

    #[cfg(feature = "no-std")]
    use self::collections::Vec;
    do_impl!(LenMut for Vec, Vec<T>; T);
    do_impl!(CapacityMut for Vec, Vec<T>; T);
    do_impl!(SplitOff for Vec, Vec<T>; T);

    use self::collections::VecDeque;
    do_impl!(LenMut for VecDeque, VecDeque<T>; T);
    do_impl!(CapacityMut for VecDeque, VecDeque<T>; T);
    do_impl!(SplitOff for VecDeque, VecDeque<T>; T);
}

#[cfg(feature = "bit-set")]
mod bit_set {
    use bit_set::BitSet;
    use super::super::CapacityMut;

    do_impl!(LenZero for BitSet; );
    do_impl!(DefaultCapacity for BitSet; );
    impl CapacityMut for BitSet {
        fn reserve(&mut self, additional: usize) {
            BitSet::reserve_len(self, additional)
        }
        fn reserve_exact(&mut self, additional: usize) {
            BitSet::reserve_len_exact(self, additional)
        }
        fn shrink_to_fit(&mut self) {
            BitSet::shrink_to_fit(self)
        }
    }
}

#[cfg(feature = "bit-vec")]
mod bit_vec {
    use bit_vec::BitVec;
    use super::super::CapacityMut;

    do_impl!(LenMut for BitVec; );
    do_impl!(DefaultCapacity for BitVec; );
    impl CapacityMut for BitVec {
        fn reserve(&mut self, additional: usize) {
            BitVec::reserve(self, additional)
        }
        fn reserve_exact(&mut self, additional: usize) {
            BitVec::reserve_exact(self, additional)
        }
        fn shrink_to_fit(&mut self) {}
    }
}

#[cfg(feature = "blist")]
mod blist {
    use blist::BList;
    do_impl!(LenZero for BList, BList<T>; T);
}

#[cfg(feature = "enum-set")]
mod enum_set {
    use enum_set::{CLike, EnumSet};
    do_impl!(LenZero for EnumSet, EnumSet<T>; T: CLike);
}

#[cfg(feature = "interval-heap")]
mod interval_heap {
    use interval_heap::IntervalHeap;
    do_impl!(LenZero for IntervalHeap, IntervalHeap<T>; T: Ord);
    do_impl!(CapacityMut for IntervalHeap, IntervalHeap<T>; T: Ord);
}

#[cfg(feature = "linear-map")]
mod linear_map {
    use linear_map::LinearMap;
    do_impl!(LenZero for LinearMap, LinearMap<K, V>; K: Eq, V);
    do_impl!(CapacityMut for LinearMap, LinearMap<K, V>; K: Eq, V);
}

#[cfg(feature = "linked-hash-map")]
mod linked_hash_map {
    use linked_hash_map::LinkedHashMap;
    use super::super::CapacityMut;

    do_impl!(LenZero for LinkedHashMap, LinkedHashMap<K, V>; K: Eq + super::Hash, V);
    do_impl!(DefaultCapacity for LinkedHashMap, LinkedHashMap<K, V>; K: Eq + super::Hash, V);
    impl<K: Eq + super::Hash, V> CapacityMut for LinkedHashMap<K, V> {
        fn reserve(&mut self, additional: usize) {
            LinkedHashMap::reserve(self, additional)
        }
        fn reserve_exact(&mut self, additional: usize) {
            LinkedHashMap::reserve(self, additional)
        }
        fn shrink_to_fit(&mut self) {
            LinkedHashMap::shrink_to_fit(self)
        }
    }
}

#[cfg(feature = "lru-cache")]
mod lru_cache {
    use lru_cache::LruCache;
    do_impl!(LenZero for LruCache, LruCache<K, V>; K: Eq + super::Hash, V);
}

#[cfg(feature = "vec_map")]
mod vec_map {
    use vec_map::VecMap;
    use super::super::CapacityMut;

    do_impl!(LenZero for VecMap, VecMap<T>; T);
    do_impl!(DefaultCapacity for VecMap, VecMap<T>; T);
    impl<T> CapacityMut for VecMap<T> {
        fn reserve(&mut self, additional: usize) {
            VecMap::reserve_len(self, additional)
        }
        fn reserve_exact(&mut self, additional: usize) {
            VecMap::reserve_len_exact(self, additional)
        }
        fn shrink_to_fit(&mut self) {}
    }
}