// Copyright 2014-2015 The Rust Project Developers. See the COPYRIGHT // file at the top-level directory of this distribution and at // http://rust-lang.org/COPYRIGHT. // // Licensed under the Apache License, Version 2.0 or the MIT license // , at your // option. This file may not be copied, modified, or distributed // except according to those terms. //! This module contains shims around the stdlib HashMap //! that add fallible methods //! //! These methods are a lie. They are not actually fallible. This is just to make //! it smooth to switch between hashmap impls in a codebase. use std::collections::HashMap as StdMap; use std::collections::HashSet as StdSet; use std::fmt; use std::hash::{BuildHasher, Hash}; use std::ops::{Deref, DerefMut}; pub use std::collections::hash_map::{Entry, Iter as MapIter, IterMut as MapIterMut, RandomState}; pub use std::collections::hash_set::{IntoIter as SetIntoIter, Iter as SetIter}; #[derive(Clone)] pub struct HashMap(StdMap); use crate::FailedAllocationError; impl Deref for HashMap { type Target = StdMap; fn deref(&self) -> &Self::Target { &self.0 } } impl DerefMut for HashMap { fn deref_mut(&mut self) -> &mut Self::Target { &mut self.0 } } impl HashMap where K: Eq + Hash, S: BuildHasher, { #[inline] pub fn try_with_hasher(hash_builder: S) -> Result, FailedAllocationError> { Ok(HashMap(StdMap::with_hasher(hash_builder))) } #[inline] pub fn try_with_capacity_and_hasher( capacity: usize, hash_builder: S, ) -> Result, FailedAllocationError> { Ok(HashMap(StdMap::with_capacity_and_hasher( capacity, hash_builder, ))) } pub fn with_capacity_and_hasher(capacity: usize, hash_builder: S) -> HashMap { HashMap(StdMap::with_capacity_and_hasher(capacity, hash_builder)) } #[inline] pub fn try_reserve(&mut self, additional: usize) -> Result<(), FailedAllocationError> { Ok(self.reserve(additional)) } pub fn try_shrink_to_fit(&mut self) -> Result<(), FailedAllocationError> { Ok(self.shrink_to_fit()) } pub fn try_entry(&mut self, key: K) -> Result, FailedAllocationError> { Ok(self.entry(key)) } #[inline] pub fn try_insert(&mut self, k: K, v: V) -> Result, FailedAllocationError> { Ok(self.insert(k, v)) } } #[derive(Clone)] pub struct HashSet(StdSet); impl Deref for HashSet { type Target = StdSet; fn deref(&self) -> &Self::Target { &self.0 } } impl DerefMut for HashSet { fn deref_mut(&mut self) -> &mut Self::Target { &mut self.0 } } impl HashSet { #[inline] pub fn new() -> HashSet { HashSet(StdSet::new()) } #[inline] pub fn with_capacity(capacity: usize) -> HashSet { HashSet(StdSet::with_capacity(capacity)) } } impl HashSet where T: Eq + Hash, S: BuildHasher, { #[inline] pub fn with_hasher(hasher: S) -> HashSet { HashSet(StdSet::with_hasher(hasher)) } #[inline] pub fn with_capacity_and_hasher(capacity: usize, hasher: S) -> HashSet { HashSet(StdSet::with_capacity_and_hasher(capacity, hasher)) } #[inline] pub fn try_reserve(&mut self, additional: usize) -> Result<(), FailedAllocationError> { Ok(self.reserve(additional)) } #[inline] pub fn try_shrink_to_fit(&mut self) -> Result<(), FailedAllocationError> { Ok(self.shrink_to_fit()) } #[inline] pub fn try_insert(&mut self, value: T) -> Result { Ok(self.insert(value)) } } // Pass through trait impls // We can't derive these since the bounds are not obvious to the derive macro impl Default for HashMap { fn default() -> Self { HashMap(Default::default()) } } impl fmt::Debug for HashMap where K: Eq + Hash + fmt::Debug, V: fmt::Debug, S: BuildHasher, { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { self.0.fmt(f) } } impl PartialEq for HashMap where K: Eq + Hash, V: PartialEq, S: BuildHasher, { fn eq(&self, other: &HashMap) -> bool { self.0.eq(&other.0) } } impl Eq for HashMap where K: Eq + Hash, V: Eq, S: BuildHasher, { } impl<'a, K, V, S> IntoIterator for &'a HashMap where K: Eq + Hash, S: BuildHasher, { type Item = (&'a K, &'a V); type IntoIter = MapIter<'a, K, V>; fn into_iter(self) -> MapIter<'a, K, V> { self.0.iter() } } impl<'a, K, V, S> IntoIterator for &'a mut HashMap where K: Eq + Hash, S: BuildHasher, { type Item = (&'a K, &'a mut V); type IntoIter = MapIterMut<'a, K, V>; fn into_iter(self) -> MapIterMut<'a, K, V> { self.0.iter_mut() } } impl Default for HashSet { fn default() -> Self { HashSet(Default::default()) } } impl fmt::Debug for HashSet where T: Eq + Hash + fmt::Debug, S: BuildHasher, { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { self.0.fmt(f) } } impl PartialEq for HashSet where T: Eq + Hash, S: BuildHasher, { fn eq(&self, other: &HashSet) -> bool { self.0.eq(&other.0) } } impl Eq for HashSet where T: Eq + Hash, S: BuildHasher, { } impl<'a, T, S> IntoIterator for &'a HashSet where T: Eq + Hash, S: BuildHasher, { type Item = &'a T; type IntoIter = SetIter<'a, T>; fn into_iter(self) -> SetIter<'a, T> { self.0.iter() } } impl IntoIterator for HashSet where T: Eq + Hash, S: BuildHasher, { type Item = T; type IntoIter = SetIntoIter; fn into_iter(self) -> SetIntoIter { self.0.into_iter() } }