Use collect for decoding more collection types.

This commit is contained in:
Nicholas Nethercote 2023-10-06 10:05:07 +11:00
parent 5f69ca62f2
commit 2db1d59830

View File

@ -578,13 +578,7 @@ impl<D: Decoder, K, V> Decodable<D> for BTreeMap<K, V>
{
fn decode(d: &mut D) -> BTreeMap<K, V> {
let len = d.read_usize();
let mut map = BTreeMap::new();
for _ in 0..len {
let key = Decodable::decode(d);
let val = Decodable::decode(d);
map.insert(key, val);
}
map
(0..len).map(|_| (Decodable::decode(d), Decodable::decode(d))).collect()
}
}
@ -606,11 +600,7 @@ impl<D: Decoder, T> Decodable<D> for BTreeSet<T>
{
fn decode(d: &mut D) -> BTreeSet<T> {
let len = d.read_usize();
let mut set = BTreeSet::new();
for _ in 0..len {
set.insert(Decodable::decode(d));
}
set
(0..len).map(|_| Decodable::decode(d)).collect()
}
}
@ -637,14 +627,7 @@ impl<D: Decoder, K, V, S> Decodable<D> for HashMap<K, V, S>
{
fn decode(d: &mut D) -> HashMap<K, V, S> {
let len = d.read_usize();
let state = Default::default();
let mut map = HashMap::with_capacity_and_hasher(len, state);
for _ in 0..len {
let key = Decodable::decode(d);
let val = Decodable::decode(d);
map.insert(key, val);
}
map
(0..len).map(|_| (Decodable::decode(d), Decodable::decode(d))).collect()
}
}
@ -668,12 +651,7 @@ impl<D: Decoder, T, S> Decodable<D> for HashSet<T, S>
{
fn decode(d: &mut D) -> HashSet<T, S> {
let len = d.read_usize();
let state = Default::default();
let mut set = HashSet::with_capacity_and_hasher(len, state);
for _ in 0..len {
set.insert(Decodable::decode(d));
}
set
(0..len).map(|_| Decodable::decode(d)).collect()
}
}
@ -700,14 +678,7 @@ impl<D: Decoder, K, V, S> Decodable<D> for indexmap::IndexMap<K, V, S>
{
fn decode(d: &mut D) -> indexmap::IndexMap<K, V, S> {
let len = d.read_usize();
let state = Default::default();
let mut map = indexmap::IndexMap::with_capacity_and_hasher(len, state);
for _ in 0..len {
let key = Decodable::decode(d);
let val = Decodable::decode(d);
map.insert(key, val);
}
map
(0..len).map(|_| (Decodable::decode(d), Decodable::decode(d))).collect()
}
}
@ -731,12 +702,7 @@ impl<D: Decoder, T, S> Decodable<D> for indexmap::IndexSet<T, S>
{
fn decode(d: &mut D) -> indexmap::IndexSet<T, S> {
let len = d.read_usize();
let state = Default::default();
let mut set = indexmap::IndexSet::with_capacity_and_hasher(len, state);
for _ in 0..len {
set.insert(Decodable::decode(d));
}
set
(0..len).map(|_| Decodable::decode(d)).collect()
}
}