mirror of
https://github.com/async-rs/async-std.git
synced 2025-02-28 07:09:40 +00:00
Added FromStream + Extend for LinkedList
This commit is contained in:
parent
bd0808eedd
commit
b2174576b2
4 changed files with 51 additions and 0 deletions
18
src/collections/linked_list/extend.rs
Normal file
18
src/collections/linked_list/extend.rs
Normal file
|
@ -0,0 +1,18 @@
|
|||
use std::pin::Pin;
|
||||
use std::collections::LinkedList;
|
||||
|
||||
use crate::prelude::*;
|
||||
use crate::stream::{Extend, IntoStream};
|
||||
|
||||
impl<T> Extend<T> for LinkedList<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_back(item)))
|
||||
}
|
||||
}
|
24
src/collections/linked_list/from_stream.rs
Normal file
24
src/collections/linked_list/from_stream.rs
Normal file
|
@ -0,0 +1,24 @@
|
|||
use std::pin::Pin;
|
||||
use std::collections::LinkedList;
|
||||
|
||||
use crate::stream::{Extend, FromStream, IntoStream};
|
||||
|
||||
impl<T> FromStream<T> for LinkedList<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 = LinkedList::new();
|
||||
out.stream_extend(stream).await;
|
||||
out
|
||||
})
|
||||
}
|
||||
}
|
7
src/collections/linked_list/mod.rs
Normal file
7
src/collections/linked_list/mod.rs
Normal file
|
@ -0,0 +1,7 @@
|
|||
//! The Rust doubly-linked list with owned nodes
|
||||
|
||||
mod extend;
|
||||
mod from_stream;
|
||||
|
||||
#[doc(inline)]
|
||||
pub use std::collections::LinkedList;
|
|
@ -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;
|
||||
|
|
Loading…
Reference in a new issue