-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fs,sync: expose poll_ fns on misc types (#3308)
Includes methods on: * fs::DirEntry * io::Lines * io::Split * sync::mpsc::Receiver * sync::misc::UnboundedReceiver
- Loading branch information
Showing
9 changed files
with
128 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,42 @@ | ||
#![allow(dead_code)] | ||
|
||
use async_stream::stream; | ||
use tokio::sync::mpsc::{self, Sender, UnboundedSender}; | ||
use std::pin::Pin; | ||
use std::task::{Context, Poll}; | ||
use tokio::sync::mpsc::{self, Receiver, Sender, UnboundedReceiver, UnboundedSender}; | ||
use tokio_stream::Stream; | ||
|
||
struct UnboundedStream<T> { | ||
recv: UnboundedReceiver<T>, | ||
} | ||
impl<T> Stream for UnboundedStream<T> { | ||
type Item = T; | ||
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<T>> { | ||
Pin::into_inner(self).recv.poll_recv(cx) | ||
} | ||
} | ||
|
||
pub fn unbounded_channel_stream<T: Unpin>() -> (UnboundedSender<T>, impl Stream<Item = T>) { | ||
let (tx, mut rx) = mpsc::unbounded_channel(); | ||
let (tx, rx) = mpsc::unbounded_channel(); | ||
|
||
let stream = stream! { | ||
while let Some(item) = rx.recv().await { | ||
yield item; | ||
} | ||
}; | ||
let stream = UnboundedStream { recv: rx }; | ||
|
||
(tx, stream) | ||
} | ||
|
||
struct BoundedStream<T> { | ||
recv: Receiver<T>, | ||
} | ||
impl<T> Stream for BoundedStream<T> { | ||
type Item = T; | ||
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<T>> { | ||
Pin::into_inner(self).recv.poll_recv(cx) | ||
} | ||
} | ||
|
||
pub fn channel_stream<T: Unpin>(size: usize) -> (Sender<T>, impl Stream<Item = T>) { | ||
let (tx, mut rx) = mpsc::channel(size); | ||
let (tx, rx) = mpsc::channel(size); | ||
|
||
let stream = stream! { | ||
while let Some(item) = rx.recv().await { | ||
yield item; | ||
} | ||
}; | ||
let stream = BoundedStream { recv: rx }; | ||
|
||
(tx, stream) | ||
} |