diff --git a/src/collections/binary_heap/extend.rs b/src/collections/binary_heap/extend.rs new file mode 100644 index 00000000..17098d9c --- /dev/null +++ b/src/collections/binary_heap/extend.rs @@ -0,0 +1,18 @@ +use std::pin::Pin; +use std::collections::BinaryHeap; + +use crate::prelude::*; +use crate::stream::{Extend, IntoStream}; + +impl Extend for BinaryHeap { + fn stream_extend<'a, S: IntoStream + 'a>( + &'a mut self, + stream: S, + ) -> Pin + '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))) + } +} diff --git a/src/collections/binary_heap/from_stream.rs b/src/collections/binary_heap/from_stream.rs new file mode 100644 index 00000000..96e55473 --- /dev/null +++ b/src/collections/binary_heap/from_stream.rs @@ -0,0 +1,24 @@ +use std::pin::Pin; +use std::collections::BinaryHeap; + +use crate::stream::{Extend, FromStream, IntoStream}; + +impl FromStream for BinaryHeap { + #[inline] + fn from_stream<'a, S: IntoStream>( + stream: S, + ) -> Pin + 'a>> + where + ::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 + }) + } +} diff --git a/src/collections/binary_heap/mod.rs b/src/collections/binary_heap/mod.rs new file mode 100644 index 00000000..35c406c0 --- /dev/null +++ b/src/collections/binary_heap/mod.rs @@ -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; diff --git a/src/collections/mod.rs b/src/collections/mod.rs index d77d443f..68c1a7d9 100644 --- a/src/collections/mod.rs +++ b/src/collections/mod.rs @@ -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;