/
fake.rs
149 lines (114 loc) · 3.78 KB
/
fake.rs
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
//! 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::hash::{BuildHasher, Hash};
use std::collections::HashMap as StdMap;
use std::collections::HashSet as StdSet;
use std::ops::{Deref, DerefMut};
pub use std::collections::hash_map::{Entry, RandomState};
pub struct HashMap<K, V, S = RandomState>(StdMap<K, V, S>);
use FailedAllocationError;
impl<K, V, S> Deref for HashMap<K, V, S> {
type Target = StdMap<K, V, S>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<K, V, S> DerefMut for HashMap<K, V, S> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl<K: Hash + Eq, V> HashMap<K, V, RandomState> {
#[inline]
pub fn new() -> HashMap<K, V, RandomState> {
HashMap(StdMap::new())
}
#[inline]
pub fn with_capacity(capacity: usize) -> HashMap<K, V, RandomState> {
HashMap(StdMap::with_capacity(capacity))
}
#[inline]
pub fn try_with_capacity(capacity: usize) -> Result<HashMap<K, V, RandomState>, FailedAllocationError> {
Ok(HashMap(StdMap::with_capacity(capacity)))
}
}
impl<K, V, S> HashMap<K, V, S>
where K: Eq + Hash,
S: BuildHasher
{
#[inline]
pub fn try_with_hasher(hash_builder: S) -> Result<HashMap<K, V, S>, FailedAllocationError> {
Ok(HashMap(StdMap::with_hasher(hash_builder)))
}
#[inline]
pub fn try_with_capacity_and_hasher(capacity: usize, hash_builder: S) -> Result<HashMap<K, V, S>, FailedAllocationError> {
Ok(HashMap(StdMap::with_capacity_and_hasher(capacity, hash_builder)))
}
pub fn with_capacity_and_hasher(capacity: usize, hash_builder: S) -> HashMap<K, V, S> {
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<Entry<K, V>, FailedAllocationError> {
Ok(self.entry(key))
}
#[inline]
pub fn try_insert(&mut self, k: K, v: V) -> Result<Option<V>, FailedAllocationError> {
Ok(self.insert(k, v))
}
}
pub struct HashSet<T, S = RandomState>(StdSet<T, S>);
impl<T, S> Deref for HashSet<T, S> {
type Target = StdSet<T, S>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<T, S> DerefMut for HashSet<T, S> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl<T: Hash + Eq> HashSet<T, RandomState> {
#[inline]
pub fn new() -> HashSet<T, RandomState> {
HashSet(StdSet::new())
}
#[inline]
pub fn with_capacity(capacity: usize) -> HashSet<T, RandomState> {
HashSet(StdSet::with_capacity(capacity))
}
}
impl<T, S> HashSet<T, S>
where T: Eq + Hash,
S: BuildHasher
{
#[inline]
pub fn with_hasher(hasher: S) -> HashSet<T, S> {
HashSet(StdSet::with_hasher(hasher))
}
#[inline]
pub fn with_capacity_and_hasher(capacity: usize, hasher: S) -> HashSet<T, S> {
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<bool, FailedAllocationError> {
Ok(self.insert(value))
}
}