pub struct SyncSender<T> { /* fields omitted */ }
The sending-half of Rust's synchronous sync_channel
type.
Messages can be sent through this channel with send
or try_send
.
send
will block if there is no space in the internal buffer.
use std::sync::mpsc::sync_channel;
use std::thread;
let (sync_sender, receiver) = sync_channel(2);
let sync_sender2 = sync_sender.clone();
thread::spawn(move || {
sync_sender.send(1).unwrap();
sync_sender.send(2).unwrap();
});
thread::spawn(move || {
sync_sender2.send(3).unwrap();
println!("Thread unblocked!");
});
let mut msg;
msg = receiver.recv().unwrap();
println!("message {} received", msg);
msg = receiver.recv().unwrap();
println!("message {} received", msg);
msg = receiver.recv().unwrap();
println!("message {} received", msg);Run
Sends a value on this synchronous channel.
This function will block until space in the internal buffer becomes
available or a receiver is available to hand off the message to.
Note that a successful send does not guarantee that the receiver will
ever see the data if there is a buffer on this channel. Items may be
enqueued in the internal buffer for the receiver to receive at a later
time. If the buffer size is 0, however, the channel becomes a rendezvous
channel and it guarantees that the receiver has indeed received
the data if this function returns success.
This function will never panic, but it may return Err
if the
Receiver
has disconnected and is no longer able to receive
information.
use std::sync::mpsc::sync_channel;
use std::thread;
let (sync_sender, receiver) = sync_channel(0);
thread::spawn(move || {
println!("sending message...");
sync_sender.send(1).unwrap();
println!("...message received!");
});
let msg = receiver.recv().unwrap();
assert_eq!(1, msg);Run
Attempts to send a value on this channel without blocking.
This method differs from send
by returning immediately if the
channel's buffer is full or no receiver is waiting to acquire some
data. Compared with send
, this function has two failure cases
instead of one (one for disconnection, one for a full buffer).
See send
for notes about guarantees of whether the
receiver has received the data or not if this function is successful.
use std::sync::mpsc::sync_channel;
use std::thread;
let (sync_sender, receiver) = sync_channel(1);
let sync_sender2 = sync_sender.clone();
thread::spawn(move || {
sync_sender.send(1).unwrap();
sync_sender.send(2).unwrap();
});
thread::spawn(move || {
sync_sender2.try_send(3).is_err();
});
let mut msg;
msg = receiver.recv().unwrap();
println!("message {} received", msg);
msg = receiver.recv().unwrap();
println!("message {} received", msg);
match receiver.try_recv() {
Ok(msg) => println!("message {} received", msg),
Err(_) => println!("the third message was never sent"),
}Run
Formats the value using the given formatter. Read more
Executes the destructor for this type. Read more
Performs copy-assignment from source
. Read more
🔬 This is a nightly-only experimental API. (
try_from
#33417)
The type returned in the event of a conversion error.
🔬 This is a nightly-only experimental API. (
try_from
#33417)
🔬 This is a nightly-only experimental API. (
try_from
#33417)
The type returned in the event of a conversion error.
🔬 This is a nightly-only experimental API. (
try_from
#33417)
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more
🔬 This is a nightly-only experimental API. (get_type_id
#27745)
this method will likely be replaced by an associated static
type Owned = T
Creates owned data from borrowed data, usually by cloning. Read more
🔬 This is a nightly-only experimental API. (toowned_clone_into
#41263)
recently added
Uses borrowed data to replace owned data, usually by cloning. Read more