Added FromStream + Extend for BTreeMap

staging
Sunjay Varma 5 years ago
parent 244c5159df
commit de2bc1e83b

@ -0,0 +1,16 @@
use std::pin::Pin;
use std::collections::BTreeMap;
use crate::prelude::*;
use crate::stream::{Extend, IntoStream};
impl<K: Ord, V> Extend<(K, V)> for BTreeMap<K, V> {
fn stream_extend<'a, S: IntoStream<Item = (K, V)> + 'a>(
&'a mut self,
stream: S,
) -> Pin<Box<dyn Future<Output = ()> + 'a>> {
Box::pin(stream.into_stream().for_each(move |(k, v)| {
self.insert(k, v);
}))
}
}

@ -0,0 +1,24 @@
use std::pin::Pin;
use std::collections::BTreeMap;
use crate::stream::{Extend, FromStream, IntoStream};
impl<K: Ord, V> FromStream<(K, V)> for BTreeMap<K, V> {
#[inline]
fn from_stream<'a, S: IntoStream<Item = (K, V)>>(
stream: S,
) -> Pin<Box<dyn core::future::Future<Output = Self> + 'a>>
where
<S as IntoStream>::IntoStream: 'a,
{
let stream = stream.into_stream();
Box::pin(async move {
pin_utils::pin_mut!(stream);
let mut out = BTreeMap::new();
out.stream_extend(stream).await;
out
})
}
}

@ -0,0 +1,7 @@
//! The Rust B-Tree Map
mod extend;
mod from_stream;
#[doc(inline)]
pub use std::collections::BTreeMap;

@ -3,6 +3,8 @@
//! This library provides efficient implementations of the most common general purpose programming //! This library provides efficient implementations of the most common general purpose programming
//! data structures. //! data structures.
mod vec_deque; pub mod vec_deque;
pub mod btree_map;
pub use vec_deque::*; pub use vec_deque::VecDeque;
pub use btree_map::BTreeMap;

Loading…
Cancel
Save