async_std/string/
extend.rs

1use std::borrow::Cow;
2use std::pin::Pin;
3
4use crate::prelude::*;
5use crate::stream::{self, IntoStream};
6
7impl stream::Extend<char> for String {
8    fn extend<'a, S: IntoStream<Item = char> + 'a>(
9        &'a mut self,
10        stream: S,
11    ) -> Pin<Box<dyn Future<Output = ()> + 'a + Send>> 
12    where
13        <S as IntoStream>::IntoStream: Send,
14    {
15        let stream = stream.into_stream();
16        self.reserve(stream.size_hint().0);
17
18        Box::pin(async move {
19            pin_utils::pin_mut!(stream);
20
21            while let Some(item) = stream.next().await {
22                self.push(item);
23            }
24        })
25    }
26}
27
28impl<'b> stream::Extend<&'b char> for String {
29    fn extend<'a, S: IntoStream<Item = &'b char> + 'a>(
30        &'a mut self,
31        stream: S,
32    ) -> Pin<Box<dyn Future<Output = ()> + 'a + Send>> 
33    where
34        <S as IntoStream>::IntoStream: Send,
35    {
36        let stream = stream.into_stream();
37
38        Box::pin(async move {
39            pin_utils::pin_mut!(stream);
40
41            while let Some(item) = stream.next().await {
42                self.push(*item);
43            }
44        })
45    }
46}
47
48impl<'b> stream::Extend<&'b str> for String {
49    fn extend<'a, S: IntoStream<Item = &'b str> + 'a>(
50        &'a mut self,
51        stream: S,
52    ) -> Pin<Box<dyn Future<Output = ()> + 'a + Send>> 
53    where
54        <S as IntoStream>::IntoStream: Send,
55    {
56        let stream = stream.into_stream();
57
58        Box::pin(async move {
59            pin_utils::pin_mut!(stream);
60
61            while let Some(item) = stream.next().await {
62                self.push_str(item);
63            }
64        })
65    }
66}
67
68impl stream::Extend<String> for String {
69    fn extend<'a, S: IntoStream<Item = String> + 'a>(
70        &'a mut self,
71        stream: S,
72    ) -> Pin<Box<dyn Future<Output = ()> + 'a + Send>>
73    where
74        <S as IntoStream>::IntoStream: Send,
75    {
76        let stream = stream.into_stream();
77
78        Box::pin(async move {
79            pin_utils::pin_mut!(stream);
80
81            while let Some(item) = stream.next().await {
82                self.push_str(&item);
83            }
84        })
85    }
86}
87
88impl<'b> stream::Extend<Cow<'b, str>> for String {
89    fn extend<'a, S: IntoStream<Item = Cow<'b, str>> + 'a>(
90        &'a mut self,
91        stream: S,
92    ) -> Pin<Box<dyn Future<Output = ()> + 'a + Send>>
93    where
94        <S as IntoStream>::IntoStream: Send,
95    {
96        let stream = stream.into_stream();
97
98        Box::pin(async move {
99            pin_utils::pin_mut!(stream);
100
101            while let Some(item) = stream.next().await {
102                self.push_str(&item);
103            }
104        })
105    }
106}