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
#![cfg_attr(test, feature(plugin))]
#![cfg_attr(test, plugin(clippy))]

use std::ffi::OsStr;
use std::collections::{BTreeMap, BTreeSet, BinaryHeap, LinkedList, VecDeque};

/// A collection with a length.
pub trait Len {
    /// Returns the length of the collection.
    fn len(&self) -> usize;

    /// Returns whether the collection is empty.
    fn is_empty(&self) -> bool {
        self.len() == 0
    }
}

/// A mutable collection with a length.
pub trait LenMut: Len {
    /// Truncates the collection so that it is no greater than `len` long.
    fn truncate(&mut self, len: usize);

    /// Empties the collection, making its length zero.
    fn clear(&mut self) {
        self.truncate(0)
    }
}

macro_rules! do_len {
    ($name:ident, $($head:tt)*) => {
        $($head)* {
            fn len(&self) -> usize {
                $name::len(self)
            }
            fn is_empty(&self) -> bool {
                $name::is_empty(self)
            }
        }
    };
}

macro_rules! do_len_mut {
    ($name:ident, $($head:tt)*) => {
        $($head)* {
            fn truncate(&mut self, len: usize) {
                $name::truncate(self, len)
            }
            fn clear(&mut self) {
                $name::clear(self)
            }
        }
    };
}

type Slice<T> = [T];

do_len!(BTreeMap, impl<K, V> Len for BTreeMap<K, V>);
do_len!(BTreeSet, impl<T: Ord> Len for BTreeSet<T>);
do_len!(BinaryHeap, impl<T: Ord> Len for BinaryHeap<T>);
//do_len!(CStr, impl Len for CStr);
//do_len!(CString, impl Len for CString);
do_len!(LinkedList, impl<T> Len for LinkedList<T>);
do_len!(OsStr, impl Len for OsStr);
//do_len!(OsString, impl Len for OsString);
do_len!(Slice, impl<T> Len for [T]);
do_len!(String, impl Len for String);
do_len!(Vec, impl<T> Len for Vec<T>);
do_len!(VecDeque, impl<T> Len for VecDeque<T>);
do_len!(str, impl Len for str);
//do_len_mut!(BTreeMap, impl<K: Ord, V> LenMut for BTreeMap<K, V>);
//do_len_mut!(BTreeSet, impl<T: Ord> LenMut for BTreeSet<T>);
//do_len_mut!(BinaryHeap, impl<T: Ord> LenMut for BinaryHeap<T>);
//do_len_mut!(CString, impl LenMut for CString);
//do_len_mut!(LinkedList, impl<T> LenMut for LinkedList<T>);
//do_len_mut!(OsString, impl LenMut for OsString);
do_len_mut!(String, impl LenMut for String);
do_len_mut!(Vec, impl<T> LenMut for Vec<T>);
do_len_mut!(VecDeque, impl<T> LenMut for VecDeque<T>);