Skip to content

Commit 1baee98

Browse files
authored
Merge pull request #375 from sunjay/fromstream-pathbuf
Added Extend + FromStream for PathBuf
2 parents f262fd8 + 0d4a907 commit 1baee98

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

src/path/pathbuf.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
use std::ffi::{OsStr, OsString};
2+
#[cfg(feature = "unstable")]
3+
use std::pin::Pin;
24

35
use crate::path::Path;
6+
#[cfg(feature = "unstable")]
7+
use crate::prelude::*;
8+
#[cfg(feature = "unstable")]
9+
use crate::stream::{Extend, FromStream, IntoStream};
410

511
/// This struct is an async version of [`std::path::PathBuf`].
612
///
@@ -233,3 +239,44 @@ impl AsRef<std::path::Path> for PathBuf {
233239
self.inner.as_ref()
234240
}
235241
}
242+
243+
#[cfg(feature = "unstable")]
244+
impl<P: AsRef<Path>> Extend<P> for PathBuf {
245+
fn stream_extend<'a, S: IntoStream<Item = P>>(
246+
&'a mut self,
247+
stream: S,
248+
) -> Pin<Box<dyn Future<Output = ()> + 'a>>
249+
where
250+
P: 'a,
251+
<S as IntoStream>::IntoStream: 'a,
252+
{
253+
let stream = stream.into_stream();
254+
255+
//TODO: This can be added back in once this issue is resolved:
256+
// https://github.com/rust-lang/rust/issues/58234
257+
//self.reserve(stream.size_hint().0);
258+
259+
Box::pin(stream.for_each(move |item| self.push(item.as_ref())))
260+
}
261+
}
262+
263+
#[cfg(feature = "unstable")]
264+
impl<'b, P: AsRef<Path> + 'b> FromStream<P> for PathBuf {
265+
#[inline]
266+
fn from_stream<'a, S: IntoStream<Item = P>>(
267+
stream: S,
268+
) -> Pin<Box<dyn core::future::Future<Output = Self> + 'a>>
269+
where
270+
<S as IntoStream>::IntoStream: 'a,
271+
{
272+
let stream = stream.into_stream();
273+
274+
Box::pin(async move {
275+
pin_utils::pin_mut!(stream);
276+
277+
let mut out = Self::new();
278+
out.stream_extend(stream).await;
279+
out
280+
})
281+
}
282+
}

0 commit comments

Comments
 (0)