From de2bc1e83b981254bd035fee06c367dc1529e086 Mon Sep 17 00:00:00 2001 From: Sunjay Varma Date: Tue, 1 Oct 2019 23:22:01 -0400 Subject: [PATCH] Added FromStream + Extend for BTreeMap --- src/collections/btree_map/extend.rs | 16 ++++++++++++++++ src/collections/btree_map/from_stream.rs | 24 ++++++++++++++++++++++++ src/collections/btree_map/mod.rs | 7 +++++++ src/collections/mod.rs | 6 ++++-- 4 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 src/collections/btree_map/extend.rs create mode 100644 src/collections/btree_map/from_stream.rs create mode 100644 src/collections/btree_map/mod.rs diff --git a/src/collections/btree_map/extend.rs b/src/collections/btree_map/extend.rs new file mode 100644 index 0000000..2e20848 --- /dev/null +++ b/src/collections/btree_map/extend.rs @@ -0,0 +1,16 @@ +use std::pin::Pin; +use std::collections::BTreeMap; + +use crate::prelude::*; +use crate::stream::{Extend, IntoStream}; + +impl Extend<(K, V)> for BTreeMap { + fn stream_extend<'a, S: IntoStream + 'a>( + &'a mut self, + stream: S, + ) -> Pin + 'a>> { + Box::pin(stream.into_stream().for_each(move |(k, v)| { + self.insert(k, v); + })) + } +} diff --git a/src/collections/btree_map/from_stream.rs b/src/collections/btree_map/from_stream.rs new file mode 100644 index 0000000..cdc51b9 --- /dev/null +++ b/src/collections/btree_map/from_stream.rs @@ -0,0 +1,24 @@ +use std::pin::Pin; +use std::collections::BTreeMap; + +use crate::stream::{Extend, FromStream, IntoStream}; + +impl FromStream<(K, V)> for BTreeMap { + #[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 = BTreeMap::new(); + out.stream_extend(stream).await; + out + }) + } +} diff --git a/src/collections/btree_map/mod.rs b/src/collections/btree_map/mod.rs new file mode 100644 index 0000000..49f9084 --- /dev/null +++ b/src/collections/btree_map/mod.rs @@ -0,0 +1,7 @@ +//! The Rust B-Tree Map + +mod extend; +mod from_stream; + +#[doc(inline)] +pub use std::collections::BTreeMap; diff --git a/src/collections/mod.rs b/src/collections/mod.rs index e20a321..8dec302 100644 --- a/src/collections/mod.rs +++ b/src/collections/mod.rs @@ -3,6 +3,8 @@ //! This library provides efficient implementations of the most common general purpose programming //! data structures. -mod vec_deque; +pub mod vec_deque; +pub mod btree_map; -pub use vec_deque::*; +pub use vec_deque::VecDeque; +pub use btree_map::BTreeMap;