**CHANGES** extend trait in order to allow FromStream impls for String

staging
Sunjay Varma 5 years ago
parent 98c79f4ff9
commit b878855bc3

@ -68,6 +68,7 @@ cfg_if! {
mod vec;
mod result;
mod option;
mod string;
}
}

@ -33,7 +33,7 @@ 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 () {

@ -0,0 +1,60 @@
use std::pin::Pin;
use std::borrow::Cow;
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
// let (lower_bound, _) = stream.size_hint();
// self.reserve(lower_bound);
//TODO: This can just be: stream.for_each(move |c| self.push(c))
Box::pin(stream.fold((), 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,
stream: S,
) -> Pin<Box<dyn Future<Output = ()> + 'a>> where 'b: 'a {
//TODO: 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 {
//TODO: This can just be: stream.into_stream().for_each(move |s| self.push_str(s))
Box::pin(stream.into_stream().fold((), 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>> {
//TODO: This can just be: stream.into_stream().for_each(move |s| self.push_str(&s))
Box::pin(stream.into_stream().fold((), 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 {
//TODO: This can just be: stream.into_stream().for_each(move |s| self.push_str(&s))
Box::pin(stream.into_stream().fold((), move |(), s| self.push_str(&s)))
}
}

@ -0,0 +1,104 @@
use std::pin::Pin;
use std::borrow::Cow;
use crate::stream::{FromStream, IntoStream, Extend};
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
})
}
}

@ -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;
Loading…
Cancel
Save