From 3160dc8189dbcf7f982b9ed3cab2089b97e75046 Mon Sep 17 00:00:00 2001 From: Sunjay Varma Date: Wed, 2 Oct 2019 20:54:02 -0400 Subject: [PATCH] Added FromStream + Extend for BTreeSet --- src/collections/btree_set/extend.rs | 16 ++++++++++++++++ src/collections/btree_set/from_stream.rs | 24 ++++++++++++++++++++++++ src/collections/btree_set/mod.rs | 7 +++++++ src/collections/mod.rs | 2 ++ 4 files changed, 49 insertions(+) create mode 100644 src/collections/btree_set/extend.rs create mode 100644 src/collections/btree_set/from_stream.rs create mode 100644 src/collections/btree_set/mod.rs diff --git a/src/collections/btree_set/extend.rs b/src/collections/btree_set/extend.rs new file mode 100644 index 00000000..2fd1d1d8 --- /dev/null +++ b/src/collections/btree_set/extend.rs @@ -0,0 +1,16 @@ +use std::pin::Pin; +use std::collections::BTreeSet; + +use crate::prelude::*; +use crate::stream::{Extend, IntoStream}; + +impl Extend for BTreeSet { + fn stream_extend<'a, S: IntoStream + 'a>( + &'a mut self, + stream: S, + ) -> Pin + 'a>> { + Box::pin(stream.into_stream().for_each(move |item| { + self.insert(item); + })) + } +} diff --git a/src/collections/btree_set/from_stream.rs b/src/collections/btree_set/from_stream.rs new file mode 100644 index 00000000..b4b654f5 --- /dev/null +++ b/src/collections/btree_set/from_stream.rs @@ -0,0 +1,24 @@ +use std::pin::Pin; +use std::collections::BTreeSet; + +use crate::stream::{Extend, FromStream, IntoStream}; + +impl FromStream for BTreeSet { + #[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 = BTreeSet::new(); + out.stream_extend(stream).await; + out + }) + } +} diff --git a/src/collections/btree_set/mod.rs b/src/collections/btree_set/mod.rs new file mode 100644 index 00000000..e8a572ab --- /dev/null +++ b/src/collections/btree_set/mod.rs @@ -0,0 +1,7 @@ +//! The Rust B-Tree Set + +mod extend; +mod from_stream; + +#[doc(inline)] +pub use std::collections::BTreeSet; diff --git a/src/collections/mod.rs b/src/collections/mod.rs index 4ed8e359..d77d443f 100644 --- a/src/collections/mod.rs +++ b/src/collections/mod.rs @@ -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;