2
0
Fork 1
mirror of https://github.com/async-rs/async-std.git synced 2025-01-16 10:49:55 +00:00

Added FromStream + Extend for BinaryHeap

This commit is contained in:
Sunjay Varma 2019-10-02 20:59:42 -04:00
parent 3160dc8189
commit bd0808eedd
4 changed files with 51 additions and 0 deletions

View file

@ -0,0 +1,18 @@
use std::pin::Pin;
use std::collections::BinaryHeap;
use crate::prelude::*;
use crate::stream::{Extend, IntoStream};
impl<T: Ord> Extend<T> for BinaryHeap<T> {
fn stream_extend<'a, S: IntoStream<Item = T> + 'a>(
&'a mut self,
stream: S,
) -> Pin<Box<dyn Future<Output = ()> + 'a>> {
let stream = stream.into_stream();
//TODO: Add this back in when size_hint is added to Stream/StreamExt
//let (lower_bound, _) = stream.size_hint();
//self.reserve(lower_bound);
Box::pin(stream.for_each(move |item| self.push(item)))
}
}

View file

@ -0,0 +1,24 @@
use std::pin::Pin;
use std::collections::BinaryHeap;
use crate::stream::{Extend, FromStream, IntoStream};
impl<T: Ord> FromStream<T> for BinaryHeap<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 = BinaryHeap::new();
out.stream_extend(stream).await;
out
})
}
}

View file

@ -0,0 +1,7 @@
//! The Rust priority queue implemented with a binary heap
mod extend;
mod from_stream;
#[doc(inline)]
pub use std::collections::BinaryHeap;

View file

@ -8,9 +8,11 @@ pub mod hash_map;
pub mod hash_set;
pub mod btree_map;
pub mod btree_set;
pub mod binary_heap;
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;
pub use binary_heap::BinaryHeap;