mirror of
https://github.com/tokio-rs/tokio.git
synced 2025-10-01 12:20:39 +00:00
Implement Stream for Receiver
That's... what it is!
This commit is contained in:
parent
cff73b7a9d
commit
d1cd455225
@ -2,6 +2,7 @@ use std::io;
|
|||||||
use std::sync::mpsc::TryRecvError;
|
use std::sync::mpsc::TryRecvError;
|
||||||
|
|
||||||
use futures::{Future, Poll};
|
use futures::{Future, Poll};
|
||||||
|
use futures::stream::Stream;
|
||||||
use futures_io::IoFuture;
|
use futures_io::IoFuture;
|
||||||
use mio::channel;
|
use mio::channel;
|
||||||
|
|
||||||
@ -20,10 +21,11 @@ pub struct Sender<T> {
|
|||||||
/// The receiving half of a channel used for processing messages sent by a
|
/// The receiving half of a channel used for processing messages sent by a
|
||||||
/// `Sender`.
|
/// `Sender`.
|
||||||
///
|
///
|
||||||
/// A `Receiver` cannot be cloned and is not `Sync`, so only one thread can
|
/// A `Receiver` cannot be cloned, so only one thread can receive messages at a
|
||||||
/// receive messages at a time.
|
/// time.
|
||||||
///
|
///
|
||||||
/// This type is created by the `LoopHandle::channel` method.
|
/// This type is created by the `LoopHandle::channel` method and implements the
|
||||||
|
/// `Stream` trait to represent received messages.
|
||||||
pub struct Receiver<T> {
|
pub struct Receiver<T> {
|
||||||
rx: ReadinessStream<channel::Receiver<T>>,
|
rx: ReadinessStream<channel::Receiver<T>>,
|
||||||
}
|
}
|
||||||
@ -80,28 +82,18 @@ impl<T> Clone for Sender<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Receiver<T> {
|
impl<T> Stream for Receiver<T> {
|
||||||
/// Attempts to receive a message sent on this channel.
|
type Item = T;
|
||||||
///
|
type Error = io::Error;
|
||||||
/// This method will attempt to dequeue any messages sent on this channel
|
|
||||||
/// from any corresponding sender. If no message is available, but senders
|
fn poll(&mut self) -> Poll<Option<T>, io::Error> {
|
||||||
/// are still detected, then `Poll::NotReady` is returned and the current
|
|
||||||
/// future task is scheduled to receive a notification when a message is
|
|
||||||
/// available.
|
|
||||||
///
|
|
||||||
/// If an I/O error happens or if all senders have gone away (the channel is
|
|
||||||
/// disconnected) then `Poll::Err` will be returned.
|
|
||||||
pub fn recv(&self) -> Poll<T, io::Error> {
|
|
||||||
match self.rx.get_ref().try_recv() {
|
match self.rx.get_ref().try_recv() {
|
||||||
Ok(t) => Poll::Ok(t),
|
Ok(t) => Poll::Ok(Some(t)),
|
||||||
Err(TryRecvError::Empty) => {
|
Err(TryRecvError::Empty) => {
|
||||||
self.rx.need_read();
|
self.rx.need_read();
|
||||||
Poll::NotReady
|
Poll::NotReady
|
||||||
}
|
}
|
||||||
Err(TryRecvError::Disconnected) => {
|
Err(TryRecvError::Disconnected) => Poll::Ok(None),
|
||||||
Poll::Err(io::Error::new(io::ErrorKind::Other,
|
|
||||||
"channel has been disconnected"))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user