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};
pub trait Len {
fn len(&self) -> usize;
fn is_empty(&self) -> bool {
self.len() == 0
}
}
pub trait LenMut: Len {
fn truncate(&mut self, len: usize);
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!(LinkedList, impl<T> Len for LinkedList<T>);
do_len!(OsStr, impl Len for OsStr);
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!(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>);