A one-shot channel (future).
Oneshot
can be used to send a single value between threads. Multiple threads can wait on it and
they will all receive the same value.
Example
use std::sync::Oneshot;
use std::thread::spawn;
let one_shot: Oneshot<i32> = Oneshot::new();
let t = spawn(|&one_shot| {
one_shot.send(42);
});
let value = one_shot.recv();
assert_eq!(value, 42);
t.join().unwrap();
Run this example
Methods
impl Oneshot<T> { ... }
-
fn new() -> Oneshot<T>
Creates a new one-shot channel.
-
fn from_value(value: T) -> Oneshot<T>
Creates a completed one-shot channel from a value
-
fn send(self: &mut Oneshot<T>, value: T)
Sets the value of the channel.
If the channel is already closed, this will return
Result::err(ChannelError::Closed)
.It is undefined behavior to call this function more than once on a single channel.
-
fn recv(self: &mut Oneshot<T>) -> T
Gets the value of the channel.
If the channel's value has already been set, this will return immediately. Otherwise, it will block until the value is set.
-
fn try_recv(self: &mut Oneshot<T>) -> Result<T, ChannelError>
Tries to get the value of the channel
If the channel's value has not been set, this will return WouldBlock.