async_std/collections/hash_map/
extend.rs1use std::collections::HashMap;
2use std::hash::{BuildHasher, Hash};
3use std::pin::Pin;
4
5use crate::prelude::*;
6use crate::stream::{self, IntoStream};
7
8impl<K, V, H> stream::Extend<(K, V)> for HashMap<K, V, H>
9where
10 K: Eq + Hash,
11 H: BuildHasher + Default,
12{
13 fn extend<'a, S: IntoStream<Item = (K, V)> + 'a>(
14 &'a mut self,
15 stream: S,
16 ) -> Pin<Box<dyn Future<Output = ()> + 'a>> {
17 let stream = stream.into_stream();
18
19 let additional = if self.is_empty() {
27 stream.size_hint().0
28 } else {
29 (stream.size_hint().0 + 1) / 2
30 };
31 self.reserve(additional);
32
33 Box::pin(stream.for_each(move |(k, v)| {
34 self.insert(k, v);
35 }))
36 }
37}