Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IntoStream for vec #213

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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};