From b2174576b27b4995cea5cc40754933b33370d990 Mon Sep 17 00:00:00 2001 From: Sunjay Varma Date: Wed, 2 Oct 2019 21:03:54 -0400 Subject: [PATCH] Added FromStream + Extend for LinkedList --- src/collections/linked_list/extend.rs | 18 ++++++++++++++++ src/collections/linked_list/from_stream.rs | 24 ++++++++++++++++++++++ src/collections/linked_list/mod.rs | 7 +++++++ src/collections/mod.rs | 2 ++ 4 files changed, 51 insertions(+) create mode 100644 src/collections/linked_list/extend.rs create mode 100644 src/collections/linked_list/from_stream.rs create mode 100644 src/collections/linked_list/mod.rs diff --git a/src/collections/linked_list/extend.rs b/src/collections/linked_list/extend.rs new file mode 100644 index 0000000..8f82547 --- /dev/null +++ b/src/collections/linked_list/extend.rs @@ -0,0 +1,18 @@ +use std::pin::Pin; +use std::collections::LinkedList; + +use crate::prelude::*; +use crate::stream::{Extend, IntoStream}; + +impl Extend for LinkedList { + 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_back(item))) + } +} diff --git a/src/collections/linked_list/from_stream.rs b/src/collections/linked_list/from_stream.rs new file mode 100644 index 0000000..40b1100 --- /dev/null +++ b/src/collections/linked_list/from_stream.rs @@ -0,0 +1,24 @@ +use std::pin::Pin; +use std::collections::LinkedList; + +use crate::stream::{Extend, FromStream, IntoStream}; + +impl FromStream for LinkedList { + #[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 = LinkedList::new(); + out.stream_extend(stream).await; + out + }) + } +} diff --git a/src/collections/linked_list/mod.rs b/src/collections/linked_list/mod.rs new file mode 100644 index 0000000..bd009ea --- /dev/null +++ b/src/collections/linked_list/mod.rs @@ -0,0 +1,7 @@ +//! The Rust doubly-linked list with owned nodes + +mod extend; +mod from_stream; + +#[doc(inline)] +pub use std::collections::LinkedList; diff --git a/src/collections/mod.rs b/src/collections/mod.rs index 68c1a7d..2ccdb23 100644 --- a/src/collections/mod.rs +++ b/src/collections/mod.rs @@ -9,6 +9,7 @@ pub mod hash_set; pub mod btree_map; pub mod btree_set; pub mod binary_heap; +pub mod linked_list; pub use vec_deque::VecDeque; pub use hash_map::HashMap; @@ -16,3 +17,4 @@ pub use hash_set::HashSet; pub use btree_map::BTreeMap; pub use btree_set::BTreeSet; pub use binary_heap::BinaryHeap; +pub use linked_list::LinkedList;