Struct local_sync::oneshot::Receiver
source · pub struct Receiver<T> { /* private fields */ }
Expand description
Receive a value from the associated Sender
.
A pair of both a Sender
and a Receiver
are created by the
channel
function.
Examples
use local_sync::oneshot;
#[monoio::main]
async fn main() {
let (tx, rx) = oneshot::channel();
monoio::spawn(async move {
if let Err(_) = tx.send(3) {
println!("the receiver dropped");
}
});
match rx.await {
Ok(v) => println!("got = {:?}", v),
Err(_) => println!("the sender dropped"),
}
}
If the sender is dropped without sending, the receiver will fail with
error::RecvError
:
use local_sync::oneshot;
#[monoio::main]
async fn main() {
let (tx, rx) = oneshot::channel::<u32>();
monoio::spawn(async move {
drop(tx);
});
match rx.await {
Ok(_) => panic!("This doesn't happen"),
Err(_) => println!("the sender dropped"),
}
}
Implementations§
source§impl<T> Receiver<T>
impl<T> Receiver<T>
sourcepub fn close(&mut self)
pub fn close(&mut self)
Prevents the associated Sender
handle from sending a value.
Any send
operation which happens after calling close
is guaranteed
to fail. After calling close
, try_recv
should be called to
receive a value if one was sent before the call to close
completed.
This function is useful to perform a graceful shutdown and ensure that a value will not be sent into the channel and never received.
close
is no-op if a message is already received or the channel
is already closed.
Examples
Prevent a value from being sent
use local_sync::oneshot;
use local_sync::oneshot::error::TryRecvError;
#[monoio::main]
async fn main() {
let (tx, mut rx) = oneshot::channel();
assert!(!tx.is_closed());
rx.close();
assert!(tx.is_closed());
assert!(tx.send("never received").is_err());
match rx.try_recv() {
Err(TryRecvError::Closed) => {}
_ => unreachable!(),
}
}
Receive a value sent before calling close
use local_sync::oneshot;
#[monoio::main]
async fn main() {
let (tx, mut rx) = oneshot::channel();
assert!(tx.send("will receive").is_ok());
rx.close();
let msg = rx.try_recv().unwrap();
assert_eq!(msg, "will receive");
}
sourcepub fn try_recv(&mut self) -> Result<T, TryRecvError>
pub fn try_recv(&mut self) -> Result<T, TryRecvError>
Attempts to receive a value.
If a pending value exists in the channel, it is returned. If no value has been sent, the current task will not be registered for future notification.
This function is useful to call from outside the context of an asynchronous task.
Return
Ok(T)
if a value is pending in the channel.Err(TryRecvError::Empty)
if no value has been sent yet.Err(TryRecvError::Closed)
if the sender has dropped without sending a value.
Examples
try_recv
before a value is sent, then after.
use local_sync::oneshot;
use local_sync::oneshot::error::TryRecvError;
#[monoio::main]
async fn main() {
let (tx, mut rx) = oneshot::channel();
match rx.try_recv() {
// The channel is currently empty
Err(TryRecvError::Empty) => {}
_ => unreachable!(),
}
// Send a value
tx.send("hello").unwrap();
match rx.try_recv() {
Ok(value) => assert_eq!(value, "hello"),
_ => unreachable!(),
}
}
try_recv
when the sender dropped before sending a value
use local_sync::oneshot;
use local_sync::oneshot::error::TryRecvError;
#[monoio::main]
async fn main() {
let (tx, mut rx) = oneshot::channel::<()>();
drop(tx);
match rx.try_recv() {
// The channel will never receive a value.
Err(TryRecvError::Closed) => {}
_ => unreachable!(),
}
}
Trait Implementations§
Auto Trait Implementations§
impl<T> !RefUnwindSafe for Receiver<T>
impl<T> !Send for Receiver<T>
impl<T> !Sync for Receiver<T>
impl<T> Unpin for Receiver<T>
impl<T> !UnwindSafe for Receiver<T>
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> FutureExt for Twhere
T: Future + ?Sized,
impl<T> FutureExt for Twhere T: Future + ?Sized,
source§fn map<U, F>(self, f: F) -> Map<Self, F>where
F: FnOnce(Self::Output) -> U,
Self: Sized,
fn map<U, F>(self, f: F) -> Map<Self, F>where F: FnOnce(Self::Output) -> U, Self: Sized,
source§fn map_into<U>(self) -> MapInto<Self, U>where
Self::Output: Into<U>,
Self: Sized,
fn map_into<U>(self) -> MapInto<Self, U>where Self::Output: Into<U>, Self: Sized,
source§fn then<Fut, F>(self, f: F) -> Then<Self, Fut, F>where
F: FnOnce(Self::Output) -> Fut,
Fut: Future,
Self: Sized,
fn then<Fut, F>(self, f: F) -> Then<Self, Fut, F>where F: FnOnce(Self::Output) -> Fut, Fut: Future, Self: Sized,
f
. Read moresource§fn left_future<B>(self) -> Either<Self, B>where
B: Future<Output = Self::Output>,
Self: Sized,
fn left_future<B>(self) -> Either<Self, B>where B: Future<Output = Self::Output>, Self: Sized,
source§fn right_future<A>(self) -> Either<A, Self>where
A: Future<Output = Self::Output>,
Self: Sized,
fn right_future<A>(self) -> Either<A, Self>where A: Future<Output = Self::Output>, Self: Sized,
source§fn into_stream(self) -> IntoStream<Self>where
Self: Sized,
fn into_stream(self) -> IntoStream<Self>where Self: Sized,
source§fn flatten(self) -> Flatten<Self>where
Self::Output: Future,
Self: Sized,
fn flatten(self) -> Flatten<Self>where Self::Output: Future, Self: Sized,
source§fn flatten_stream(self) -> FlattenStream<Self>where
Self::Output: Stream,
Self: Sized,
fn flatten_stream(self) -> FlattenStream<Self>where Self::Output: Stream, Self: Sized,
source§fn fuse(self) -> Fuse<Self>where
Self: Sized,
fn fuse(self) -> Fuse<Self>where Self: Sized,
poll
will never again be called once it has
completed. This method can be used to turn any Future
into a
FusedFuture
. Read moresource§fn inspect<F>(self, f: F) -> Inspect<Self, F>where
F: FnOnce(&Self::Output),
Self: Sized,
fn inspect<F>(self, f: F) -> Inspect<Self, F>where F: FnOnce(&Self::Output), Self: Sized,
source§fn unit_error(self) -> UnitError<Self>where
Self: Sized,
fn unit_error(self) -> UnitError<Self>where Self: Sized,
Future<Output = T>
into a
TryFuture<Ok = T, Error = ()
>.source§fn never_error(self) -> NeverError<Self>where
Self: Sized,
fn never_error(self) -> NeverError<Self>where Self: Sized,
Future<Output = T>
into a
TryFuture<Ok = T, Error = Never
>.