1 // Copyright 2014-2015 The Rust Project Developers. See the COPYRIGHT 2 // file at the top-level directory of this distribution and at 3 // http://rust-lang.org/COPYRIGHT. 4 // 5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or 6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license 7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your 8 // option. This file may not be copied, modified, or distributed 9 // except according to those terms. 10 11 //! This module contains shims around the stdlib HashMap 12 //! that add fallible methods 13 //! 14 //! These methods are a lie. They are not actually fallible. This is just to make 15 //! it smooth to switch between hashmap impls in a codebase. 16 17 use std::collections::HashMap as StdMap; 18 use std::collections::HashSet as StdSet; 19 use std::fmt; 20 use std::hash::{BuildHasher, Hash}; 21 use std::ops::{Deref, DerefMut}; 22 23 pub use std::collections::hash_map::{Entry, RandomState, Iter as MapIter, IterMut as MapIterMut}; 24 pub use std::collections::hash_set::{Iter as SetIter, IntoIter as SetIntoIter}; 25 26 #[derive(Clone)] 27 pub struct HashMap<K, V, S = RandomState>(StdMap<K, V, S>); 28 29 30 use FailedAllocationError; 31 32 impl<K, V, S> Deref for HashMap<K, V, S> { 33 type Target = StdMap<K, V, S>; deref(&self) -> &Self::Target34 fn deref(&self) -> &Self::Target { 35 &self.0 36 } 37 } 38 39 impl<K, V, S> DerefMut for HashMap<K, V, S> { deref_mut(&mut self) -> &mut Self::Target40 fn deref_mut(&mut self) -> &mut Self::Target { 41 &mut self.0 42 } 43 } 44 45 impl<K, V, S> HashMap<K, V, S> 46 where K: Eq + Hash, 47 S: BuildHasher 48 { 49 #[inline] try_with_hasher(hash_builder: S) -> Result<HashMap<K, V, S>, FailedAllocationError>50 pub fn try_with_hasher(hash_builder: S) -> Result<HashMap<K, V, S>, FailedAllocationError> { 51 Ok(HashMap(StdMap::with_hasher(hash_builder))) 52 } 53 54 #[inline] try_with_capacity_and_hasher(capacity: usize, hash_builder: S) -> Result<HashMap<K, V, S>, FailedAllocationError>55 pub fn try_with_capacity_and_hasher(capacity: usize, 56 hash_builder: S) 57 -> Result<HashMap<K, V, S>, FailedAllocationError> { 58 Ok(HashMap(StdMap::with_capacity_and_hasher(capacity, hash_builder))) 59 } 60 with_capacity_and_hasher(capacity: usize, hash_builder: S) -> HashMap<K, V, S>61 pub fn with_capacity_and_hasher(capacity: usize, hash_builder: S) -> HashMap<K, V, S> { 62 HashMap(StdMap::with_capacity_and_hasher(capacity, hash_builder)) 63 } 64 65 66 #[inline] try_reserve(&mut self, additional: usize) -> Result<(), FailedAllocationError>67 pub fn try_reserve(&mut self, additional: usize) -> Result<(), FailedAllocationError> { 68 Ok(self.reserve(additional)) 69 } 70 try_shrink_to_fit(&mut self) -> Result<(), FailedAllocationError>71 pub fn try_shrink_to_fit(&mut self) -> Result<(), FailedAllocationError> { 72 Ok(self.shrink_to_fit()) 73 } 74 try_entry(&mut self, key: K) -> Result<Entry<K, V>, FailedAllocationError>75 pub fn try_entry(&mut self, key: K) -> Result<Entry<K, V>, FailedAllocationError> { 76 Ok(self.entry(key)) 77 } 78 79 #[inline] try_insert(&mut self, k: K, v: V) -> Result<Option<V>, FailedAllocationError>80 pub fn try_insert(&mut self, k: K, v: V) -> Result<Option<V>, FailedAllocationError> { 81 Ok(self.insert(k, v)) 82 } 83 } 84 85 #[derive(Clone)] 86 pub struct HashSet<T, S = RandomState>(StdSet<T, S>); 87 88 89 impl<T, S> Deref for HashSet<T, S> { 90 type Target = StdSet<T, S>; deref(&self) -> &Self::Target91 fn deref(&self) -> &Self::Target { 92 &self.0 93 } 94 } 95 96 impl<T, S> DerefMut for HashSet<T, S> { deref_mut(&mut self) -> &mut Self::Target97 fn deref_mut(&mut self) -> &mut Self::Target { 98 &mut self.0 99 } 100 } 101 102 impl<T: Hash + Eq> HashSet<T, RandomState> { 103 #[inline] new() -> HashSet<T, RandomState>104 pub fn new() -> HashSet<T, RandomState> { 105 HashSet(StdSet::new()) 106 } 107 108 #[inline] with_capacity(capacity: usize) -> HashSet<T, RandomState>109 pub fn with_capacity(capacity: usize) -> HashSet<T, RandomState> { 110 HashSet(StdSet::with_capacity(capacity)) 111 } 112 } 113 114 115 impl<T, S> HashSet<T, S> 116 where T: Eq + Hash, 117 S: BuildHasher 118 { 119 #[inline] with_hasher(hasher: S) -> HashSet<T, S>120 pub fn with_hasher(hasher: S) -> HashSet<T, S> { 121 HashSet(StdSet::with_hasher(hasher)) 122 } 123 124 125 #[inline] with_capacity_and_hasher(capacity: usize, hasher: S) -> HashSet<T, S>126 pub fn with_capacity_and_hasher(capacity: usize, hasher: S) -> HashSet<T, S> { 127 HashSet(StdSet::with_capacity_and_hasher(capacity, hasher)) 128 } 129 130 #[inline] try_reserve(&mut self, additional: usize) -> Result<(), FailedAllocationError>131 pub fn try_reserve(&mut self, additional: usize) -> Result<(), FailedAllocationError> { 132 Ok(self.reserve(additional)) 133 } 134 135 #[inline] try_shrink_to_fit(&mut self) -> Result<(), FailedAllocationError>136 pub fn try_shrink_to_fit(&mut self) -> Result<(), FailedAllocationError> { 137 Ok(self.shrink_to_fit()) 138 } 139 140 #[inline] try_insert(&mut self, value: T) -> Result<bool, FailedAllocationError>141 pub fn try_insert(&mut self, value: T) -> Result<bool, FailedAllocationError> { 142 Ok(self.insert(value)) 143 } 144 } 145 146 // Pass through trait impls 147 // We can't derive these since the bounds are not obvious to the derive macro 148 149 impl<K: Hash + Eq, V, S: BuildHasher + Default> Default for HashMap<K, V, S> { default() -> Self150 fn default() -> Self { 151 HashMap(Default::default()) 152 } 153 } 154 155 impl<K, V, S> fmt::Debug for HashMap<K, V, S> 156 where K: Eq + Hash + fmt::Debug, 157 V: fmt::Debug, 158 S: BuildHasher { fmt(&self, f: &mut fmt::Formatter) -> fmt::Result159 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 160 self.0.fmt(f) 161 } 162 } 163 164 impl<K, V, S> PartialEq for HashMap<K, V, S> 165 where K: Eq + Hash, 166 V: PartialEq, 167 S: BuildHasher 168 { eq(&self, other: &HashMap<K, V, S>) -> bool169 fn eq(&self, other: &HashMap<K, V, S>) -> bool { 170 self.0.eq(&other.0) 171 } 172 } 173 174 impl<K, V, S> Eq for HashMap<K, V, S> 175 where K: Eq + Hash, 176 V: Eq, 177 S: BuildHasher 178 { 179 } 180 181 impl<'a, K, V, S> IntoIterator for &'a HashMap<K, V, S> 182 where K: Eq + Hash, 183 S: BuildHasher 184 { 185 type Item = (&'a K, &'a V); 186 type IntoIter = MapIter<'a, K, V>; 187 into_iter(self) -> MapIter<'a, K, V>188 fn into_iter(self) -> MapIter<'a, K, V> { 189 self.0.iter() 190 } 191 } 192 193 impl<'a, K, V, S> IntoIterator for &'a mut HashMap<K, V, S> 194 where K: Eq + Hash, 195 S: BuildHasher 196 { 197 type Item = (&'a K, &'a mut V); 198 type IntoIter = MapIterMut<'a, K, V>; 199 into_iter(self) -> MapIterMut<'a, K, V>200 fn into_iter(self) -> MapIterMut<'a, K, V> { 201 self.0.iter_mut() 202 } 203 } 204 205 impl<T: Eq + Hash, S: BuildHasher + Default> Default for HashSet<T, S> { default() -> Self206 fn default() -> Self { 207 HashSet(Default::default()) 208 } 209 } 210 211 impl<T, S> fmt::Debug for HashSet<T, S> 212 where T: Eq + Hash + fmt::Debug, 213 S: BuildHasher 214 { fmt(&self, f: &mut fmt::Formatter) -> fmt::Result215 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 216 self.0.fmt(f) 217 } 218 } 219 220 impl<T, S> PartialEq for HashSet<T, S> 221 where T: Eq + Hash, 222 S: BuildHasher 223 { eq(&self, other: &HashSet<T, S>) -> bool224 fn eq(&self, other: &HashSet<T, S>) -> bool { 225 self.0.eq(&other.0) 226 } 227 } 228 229 impl<T, S> Eq for HashSet<T, S> 230 where T: Eq + Hash, 231 S: BuildHasher 232 { 233 } 234 235 impl<'a, T, S> IntoIterator for &'a HashSet<T, S> 236 where T: Eq + Hash, 237 S: BuildHasher 238 { 239 type Item = &'a T; 240 type IntoIter = SetIter<'a, T>; 241 into_iter(self) -> SetIter<'a, T>242 fn into_iter(self) -> SetIter<'a, T> { 243 self.0.iter() 244 } 245 } 246 247 impl<T, S> IntoIterator for HashSet<T, S> 248 where T: Eq + Hash, 249 S: BuildHasher 250 { 251 type Item = T; 252 type IntoIter = SetIntoIter<T>; 253 254 into_iter(self) -> SetIntoIter<T>255 fn into_iter(self) -> SetIntoIter<T> { 256 self.0.into_iter() 257 } 258 } 259 260 261