Added FromStream + Extend for BTreeSet

staging
Sunjay Varma 5 years ago
parent 6c2ffd7181
commit 3160dc8189

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

@ -0,0 +1,24 @@
use std::pin::Pin;
use std::collections::BTreeSet;
use crate::stream::{Extend, FromStream, IntoStream};
impl<T: Ord> FromStream<T> for BTreeSet<T> {
#[inline]
fn from_stream<'a, S: IntoStream<Item = T>>(
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 = BTreeSet::new();
out.stream_extend(stream).await;
out
})
}
}

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

@ -7,8 +7,10 @@ pub mod vec_deque;
pub mod hash_map;
pub mod hash_set;
pub mod btree_map;
pub mod btree_set;
pub use vec_deque::VecDeque;
pub use hash_map::HashMap;
pub use hash_set::HashSet;
pub use btree_map::BTreeMap;
pub use btree_set::BTreeSet;

Loading…
Cancel
Save