1use std::collections::{HashMap,HashSet};
48use std::iter::FromIterator;
49use std::cmp::Eq;
50use std::hash::Hash;
51use std::result::Result;
52
53pub trait ToVec<T> {
55 fn to_vec(self) -> Vec<T>;
58}
59
60pub trait ToVecResult<T,E> {
62 fn to_vec_result(self) -> Result<Vec<T>,E>;
65}
66
67pub trait ToMap<K,V> {
69 fn to_map(self) -> HashMap<K,V>;
71}
72
73pub trait ToSet<K> {
75 fn to_set(self) -> HashSet<K>;
77}
78
79impl <T,I> ToVec<T> for I
80where I: Iterator<Item=T> {
81 fn to_vec(self) -> Vec<T> {
82 FromIterator::from_iter(self)
83 }
84}
85
86impl <T,E,I> ToVecResult<T,E> for I
87where I: Iterator<Item=Result<T,E>> {
88 fn to_vec_result(self) -> Result<Vec<T>,E> {
89 FromIterator::from_iter(self)
90 }
91}
92
93impl <'a, K,V,I> ToMap<K,V> for I
94where K: Eq + Hash + Clone +'a, V: Clone +'a, I: Iterator<Item=&'a (K,V)> {
95 fn to_map(self) -> HashMap<K,V> {
96 FromIterator::from_iter(self.cloned())
97 }
98}
99
100
101impl <'a, K,I> ToSet<K> for I
102where K: Eq + Hash + Clone + 'a, I: Iterator<Item=&'a K> {
103 fn to_set(self) -> HashSet<K> {
104 FromIterator::from_iter(self.cloned())
105 }
106}
107
108
109#[cfg(test)]
110mod tests {
111 use super::*;
112
113 #[test]
114 fn test_to_vec() {
115 let v = "one two three".split_whitespace().to_vec();
116 assert_eq!(v,&["one","two","three"]);
117 }
118
119 #[test]
120 fn test_to_vec_result() {
121 let numbers = "23E 5F5 FF00".split_whitespace()
122 .map(|s| u32::from_str_radix(s,16)).to_vec_result().unwrap();
123
124 assert_eq!(numbers,&[0x23E, 0x5F5, 0xFF00]);
125 }
126
127 #[test]
128 fn test_to_set() {
129 let set1 = [10,5,2,5,10].iter().to_set();
130 let set2 = [2,5,10].iter().to_set();
131
132 assert_eq!(set1,set2);
133
134 let set3 = set1.intersection(&set2).to_set();
135 assert_eq!(set3,set1);
136
137 let colours = ["green","orange","blue"].iter().to_set();
138 let fruit = ["apple","banana","orange"].iter().to_set();
139 let common = colours.intersection(&fruit).to_set();
140 assert_eq!(common, ["orange"].iter().to_set());
141 }
142
143 const VALUES: &[(&str,i32)] = &[("hello",10),("dolly",20)];
144
145 #[test]
146 fn test_to_map() {
147
148 let map = VALUES.iter().to_map();
149
150 assert_eq!(map.get("hello"),Some(&10));
151 assert_eq!(map.get("dolly"),Some(&20));
152
153 }
154
155
156}