mirror of
https://github.com/async-rs/async-std.git
synced 2025-03-01 07:39:40 +00:00
Added FromStream + Extend for BTreeSet
This commit is contained in:
parent
6c2ffd7181
commit
3160dc8189
4 changed files with 49 additions and 0 deletions
16
src/collections/btree_set/extend.rs
Normal file
16
src/collections/btree_set/extend.rs
Normal file
|
@ -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);
|
||||
}))
|
||||
}
|
||||
}
|
24
src/collections/btree_set/from_stream.rs
Normal file
24
src/collections/btree_set/from_stream.rs
Normal file
|
@ -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
|
||||
})
|
||||
}
|
||||
}
|
7
src/collections/btree_set/mod.rs
Normal file
7
src/collections/btree_set/mod.rs
Normal file
|
@ -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…
Reference in a new issue