Skip to content

Commit 526d318

Browse files
committed
Added FromStream + Extend for BTreeMap
1 parent 74a6bd9 commit 526d318

File tree

4 files changed

+51
-2
lines changed

4 files changed

+51
-2
lines changed

src/collections/btree_map/extend.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
use std::pin::Pin;
2+
use std::collections::BTreeMap;
3+
4+
use crate::prelude::*;
5+
use crate::stream::{Extend, IntoStream};
6+
7+
impl<K: Ord, V> Extend<(K, V)> for BTreeMap<K, V> {
8+
fn stream_extend<'a, S: IntoStream<Item = (K, V)> + 'a>(
9+
&'a mut self,
10+
stream: S,
11+
) -> Pin<Box<dyn Future<Output = ()> + 'a>> {
12+
Box::pin(stream.into_stream().for_each(move |(k, v)| {
13+
self.insert(k, v);
14+
}))
15+
}
16+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use std::pin::Pin;
2+
use std::collections::BTreeMap;
3+
4+
use crate::stream::{Extend, FromStream, IntoStream};
5+
6+
impl<K: Ord, V> FromStream<(K, V)> for BTreeMap<K, V> {
7+
#[inline]
8+
fn from_stream<'a, S: IntoStream<Item = (K, V)>>(
9+
stream: S,
10+
) -> Pin<Box<dyn core::future::Future<Output = Self> + 'a>>
11+
where
12+
<S as IntoStream>::IntoStream: 'a,
13+
{
14+
let stream = stream.into_stream();
15+
16+
Box::pin(async move {
17+
pin_utils::pin_mut!(stream);
18+
19+
let mut out = BTreeMap::new();
20+
out.stream_extend(stream).await;
21+
out
22+
})
23+
}
24+
}

src/collections/btree_map/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//! The Rust B-Tree Map
2+
3+
mod extend;
4+
mod from_stream;
5+
6+
#[doc(inline)]
7+
pub use std::collections::BTreeMap;

src/collections/mod.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
//! This library provides efficient implementations of the most common general purpose programming
44
//! data structures.
55
6-
mod vec_deque;
6+
pub mod vec_deque;
7+
pub mod btree_map;
78

8-
pub use vec_deque::*;
9+
pub use vec_deque::VecDeque;
10+
pub use btree_map::BTreeMap;

0 commit comments

Comments
 (0)