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>> {
12        let stream = stream.into_stream();
13        self.reserve(stream.size_hint().0);
14
15        Box::pin(async move {
16            pin_utils::pin_mut!(stream);
17
18            while let Some(item) = stream.next().await {
19                self.push(item);
20            }
21        })
22    }
23}
24
25impl<'b> stream::Extend<&'b char> for String {
26    fn extend<'a, S: IntoStream<Item = &'b char> + 'a>(
27        &'a mut self,
28        stream: S,
29    ) -> Pin<Box<dyn Future<Output = ()> + 'a>> {
30        let stream = stream.into_stream();
31
32        Box::pin(async move {
33            pin_utils::pin_mut!(stream);
34
35            while let Some(item) = stream.next().await {
36                self.push(*item);
37            }
38        })
39    }
40}
41
42impl<'b> stream::Extend<&'b str> for String {
43    fn extend<'a, S: IntoStream<Item = &'b str> + 'a>(
44        &'a mut self,
45        stream: S,
46    ) -> Pin<Box<dyn Future<Output = ()> + 'a>> {
47        let stream = stream.into_stream();
48
49        Box::pin(async move {
50            pin_utils::pin_mut!(stream);
51
52            while let Some(item) = stream.next().await {
53                self.push_str(item);
54            }
55        })
56    }
57}
58
59impl stream::Extend<String> for String {
60    fn extend<'a, S: IntoStream<Item = String> + 'a>(
61        &'a mut self,
62        stream: S,
63    ) -> Pin<Box<dyn Future<Output = ()> + 'a>> {
64        let stream = stream.into_stream();
65
66        Box::pin(async move {
67            pin_utils::pin_mut!(stream);
68
69            while let Some(item) = stream.next().await {
70                self.push_str(&item);
71            }
72        })
73    }
74}
75
76impl<'b> stream::Extend<Cow<'b, str>> for String {
77    fn extend<'a, S: IntoStream<Item = Cow<'b, str>> + 'a>(
78        &'a mut self,
79        stream: S,
80    ) -> Pin<Box<dyn Future<Output = ()> + 'a>> {
81        let stream = stream.into_stream();
82
83        Box::pin(async move {
84            pin_utils::pin_mut!(stream);
85
86            while let Some(item) = stream.next().await {
87                self.push_str(&item);
88            }
89        })
90    }
91}