Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
IntoStream for vec
Signed-off-by: Yoshua Wuyts <yoshuawuyts@gmail.com>
  • Loading branch information
yoshuawuyts committed Sep 18, 2019
commit f56fbda134d23d3813f933c8d28ac8cd80fbf599
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ mod result;
pub mod stream;
pub mod sync;
pub mod task;
mod vec;
pub mod vec;

#[cfg_attr(feature = "docs", doc(cfg(unstable)))]
#[cfg(feature = "unstable")]
Expand Down
16 changes: 8 additions & 8 deletions src/stream/into_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ pub trait IntoStream {
fn into_stream(self) -> Self::IntoStream;
}

impl<I: Stream + Send> IntoStream for I {
type Item = I::Item;
type IntoStream = I;
// impl<I: Stream + Send> IntoStream for I {
// type Item = I::Item;
// type IntoStream = I;

#[inline]
fn into_stream(self) -> I {
self
}
}
// #[inline]
// fn into_stream(self) -> I {
// self
// }
// }
89 changes: 89 additions & 0 deletions src/vec/into_stream.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
use std::pin::Pin;
use std::task::{Context, Poll};

/// A stream that moves out of a vector.
#[derive(Debug)]
pub struct IntoStream<T> {
iter: std::vec::IntoIter<T>,
}

impl<T: Send> crate::stream::IntoStream for Vec<T> {
type Item = T;
type IntoStream = IntoStream<T>;

/// Creates a consuming iterator, that is, one that moves each value out of
/// the vector (from start to end). The vector cannot be used after calling
/// this.
///
/// # Examples
///
/// ```
/// let v = vec!["a".to_string(), "b".to_string()];
/// for s in v.into_stream() {
/// // s has type String, not &String
/// println!("{}", s);
/// }
/// ```
#[inline]
fn into_stream(mut self) -> IntoStream<T> {
let iter = self.into_iter();
IntoStream { iter }
}
}

impl<T: Send> futures_core::stream::Stream for IntoStream<T> {
type Item = T;

fn poll_next(mut self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Option<Self::Item>> {
Poll::Ready(self.iter.next())
}
}

/// Slice stream.
#[derive(Debug)]
pub struct Stream<'a, T: 'a> {
iter: std::slice::Iter<'a, T>,
}

impl<'a, T: Sync> futures_core::stream::Stream for Stream<'a, T> {
type Item = &'a T;

fn poll_next(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Option<Self::Item>> {
Poll::Ready(self.iter.next())
}
}

impl<'a, T: Sync> crate::stream::IntoStream for &'a Vec<T> {
type Item = &'a T;
type IntoStream = Stream<'a, T>;

fn into_stream(self) -> Stream<'a, T> {
let iter = self.iter();
Stream { iter }
}
}

/// Mutable slice stream.
#[derive(Debug)]
pub struct StreamMut<'a, T: 'a> {
iter: std::slice::IterMut<'a, T>,
}

impl<'a, T: Sync> futures_core::stream::Stream for StreamMut<'a, T> {
type Item = &'a mut T;

fn poll_next(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Option<Self::Item>> {
Poll::Ready(self.iter.next())
}
}

impl<'a, T: Send + Sync> crate::stream::IntoStream for &'a mut Vec<T> {
type Item = &'a mut T;

type IntoStream = StreamMut<'a, T>;

fn into_stream(self) -> StreamMut<'a, T> {
let iter = self.iter_mut();
StreamMut { iter }
}
}
12 changes: 7 additions & 5 deletions src/vec/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
//! The Rust core allocation and collections library
//! Core allocation and collections library.
//!
//! This library provides smart pointers and collections for managing
//! heap-allocated values.
//! This library contains stream types for `std::vec::Vec`.

mod from_stream;
mod into_stream;

#[doc(inline)]
pub use std::vec::Vec;
// #[doc(inline)]
// pub use std::vec::Vec;

pub use into_stream::{IntoStream, Stream, StreamMut};