Delete Decoder::read_map

This commit is contained in:
Mark Rousskov 2022-02-09 18:19:18 -05:00
parent 42904b0219
commit c6ad61a1bd
2 changed files with 26 additions and 37 deletions

View File

@ -81,15 +81,14 @@ where
V: Decodable<D>, V: Decodable<D>,
{ {
fn decode(d: &mut D) -> BTreeMap<K, V> { fn decode(d: &mut D) -> BTreeMap<K, V> {
d.read_map(|d, len| { let len = d.read_usize();
let mut map = BTreeMap::new(); let mut map = BTreeMap::new();
for _ in 0..len { for _ in 0..len {
let key = Decodable::decode(d); let key = Decodable::decode(d);
let val = Decodable::decode(d); let val = Decodable::decode(d);
map.insert(key, val); map.insert(key, val);
} }
map map
})
} }
} }
@ -145,16 +144,15 @@ where
S: BuildHasher + Default, S: BuildHasher + Default,
{ {
fn decode(d: &mut D) -> HashMap<K, V, S> { fn decode(d: &mut D) -> HashMap<K, V, S> {
d.read_map(|d, len| { let len = d.read_usize();
let state = Default::default(); let state = Default::default();
let mut map = HashMap::with_capacity_and_hasher(len, state); let mut map = HashMap::with_capacity_and_hasher(len, state);
for _ in 0..len { for _ in 0..len {
let key = Decodable::decode(d); let key = Decodable::decode(d);
let val = Decodable::decode(d); let val = Decodable::decode(d);
map.insert(key, val); map.insert(key, val);
} }
map map
})
} }
} }
@ -223,16 +221,15 @@ where
S: BuildHasher + Default, S: BuildHasher + Default,
{ {
fn decode(d: &mut D) -> indexmap::IndexMap<K, V, S> { fn decode(d: &mut D) -> indexmap::IndexMap<K, V, S> {
d.read_map(|d, len| { let len = d.read_usize();
let state = Default::default(); let state = Default::default();
let mut map = indexmap::IndexMap::with_capacity_and_hasher(len, state); let mut map = indexmap::IndexMap::with_capacity_and_hasher(len, state);
for _ in 0..len { for _ in 0..len {
let key = Decodable::decode(d); let key = Decodable::decode(d);
let val = Decodable::decode(d); let val = Decodable::decode(d);
map.insert(key, val); map.insert(key, val);
} }
map map
})
} }
} }

View File

@ -200,14 +200,6 @@ pub trait Decoder {
fn read_char(&mut self) -> char; fn read_char(&mut self) -> char;
fn read_str(&mut self) -> Cow<'_, str>; fn read_str(&mut self) -> Cow<'_, str>;
fn read_raw_bytes_into(&mut self, s: &mut [u8]); fn read_raw_bytes_into(&mut self, s: &mut [u8]);
fn read_map<T, F>(&mut self, f: F) -> T
where
F: FnOnce(&mut Self, usize) -> T,
{
let len = self.read_usize();
f(self, len)
}
} }
/// Trait for types that can be serialized /// Trait for types that can be serialized