Skip to content

Changes Extend trait in order to allow streams that yield references #266

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

Merged
merged 3 commits into from
Oct 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ cfg_if! {
mod vec;
mod result;
mod option;
mod string;
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/stream/extend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ pub trait Extend<A> {
fn stream_extend<'a, T: IntoStream<Item = A> + 'a>(
&'a mut self,
stream: T,
) -> Pin<Box<dyn Future<Output = ()> + 'a>>;
) -> Pin<Box<dyn Future<Output = ()> + 'a>>
where
A: 'a;
}

impl Extend<()> for () {
Expand Down
67 changes: 67 additions & 0 deletions src/string/extend.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
use std::borrow::Cow;
use std::pin::Pin;

use crate::prelude::*;
use crate::stream::{Extend, IntoStream};

impl Extend<char> for String {
fn stream_extend<'a, S: IntoStream<Item = char> + '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 |c| self.push(c)))
}
}

impl<'b> Extend<&'b char> for String {
fn stream_extend<'a, S: IntoStream<Item = &'b char> + 'a>(
&'a mut self,
//TODO: Remove the underscore when uncommenting the body of this impl
_stream: S,
) -> Pin<Box<dyn Future<Output = ()> + 'a>>
where
'b: 'a,
{
//TODO: This can be uncommented when `copied` is added to Stream/StreamExt
//Box::pin(stream.into_stream().copied())
unimplemented!()
}
}

impl<'b> Extend<&'b str> for String {
fn stream_extend<'a, S: IntoStream<Item = &'b str> + 'a>(
&'a mut self,
stream: S,
) -> Pin<Box<dyn Future<Output = ()> + 'a>>
where
'b: 'a,
{
Box::pin(stream.into_stream().for_each(move |s| self.push_str(s)))
}
}

impl Extend<String> for String {
fn stream_extend<'a, S: IntoStream<Item = String> + 'a>(
&'a mut self,
stream: S,
) -> Pin<Box<dyn Future<Output = ()> + 'a>> {
Box::pin(stream.into_stream().for_each(move |s| self.push_str(&s)))
}
}

impl<'b> Extend<Cow<'b, str>> for String {
fn stream_extend<'a, S: IntoStream<Item = Cow<'b, str>> + 'a>(
&'a mut self,
stream: S,
) -> Pin<Box<dyn Future<Output = ()> + 'a>>
where
'b: 'a,
{
Box::pin(stream.into_stream().for_each(move |s| self.push_str(&s)))
}
}
104 changes: 104 additions & 0 deletions src/string/from_stream.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
use std::borrow::Cow;
use std::pin::Pin;

use crate::stream::{Extend, FromStream, IntoStream};

impl FromStream<char> for String {
#[inline]
fn from_stream<'a, S: IntoStream<Item = char>>(
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 = String::new();
out.stream_extend(stream).await;
out
})
}
}

impl<'b> FromStream<&'b char> for String {
#[inline]
fn from_stream<'a, S: IntoStream<Item = &'b char>>(
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 = String::new();
out.stream_extend(stream).await;
out
})
}
}

impl<'b> FromStream<&'b str> for String {
#[inline]
fn from_stream<'a, S: IntoStream<Item = &'b str>>(
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 = String::new();
out.stream_extend(stream).await;
out
})
}
}

impl FromStream<String> for String {
#[inline]
fn from_stream<'a, S: IntoStream<Item = String>>(
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 = String::new();
out.stream_extend(stream).await;
out
})
}
}

impl<'b> FromStream<Cow<'b, str>> for String {
#[inline]
fn from_stream<'a, S: IntoStream<Item = Cow<'b, str>>>(
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 = String::new();
out.stream_extend(stream).await;
out
})
}
}
9 changes: 9 additions & 0 deletions src/string/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//! The Rust core string library
//!
//! This library provides a UTF-8 encoded, growable string.

mod extend;
mod from_stream;

#[doc(inline)]
pub use std::string::String;