Skip to content

Commit 87772a9

Browse files
author
Anton Melnikov
committed
feat(coio): mpsc docs
1 parent a4a883d commit 87772a9

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

src/coio.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,10 @@ pub(crate) fn write(fd: RawFd, buf: &[u8], timeout: Option<Duration>) -> Result<
290290
}
291291
}
292292

293+
/// Creates a new asynchronous channel, returning the sender/receiver halves.
294+
///
295+
/// All data sent on the Sender will become available on the [Receiver] in the same order as it was sent,
296+
/// and no `send` will block the calling fiber, `recv` will block until a message is available.
293297
pub fn channel<T>(capacity: usize) -> (Sender<T>, Receiver<T>) {
294298
let chan = Rc::new(Chan {
295299
buffer: RefCell::new(VecDeque::with_capacity(capacity)),
@@ -301,9 +305,14 @@ pub fn channel<T>(capacity: usize) -> (Sender<T>, Receiver<T>) {
301305
(Sender(chan.clone()), Receiver(chan))
302306
}
303307

308+
/// The sending-half of channel.
309+
///
310+
/// Messages can be sent through this channel with `send`. Can be cloned.
304311
pub struct Sender<T>(Rc<Chan<T>>);
305312

306313
impl<T> Sender<T> {
314+
/// Attempts to send a value on this channel, returning it back if it could not be sent.
315+
/// This method will never block.
307316
pub fn send(&self, value: T) -> Result<(), io::Error> {
308317
if !self.0.rx_is_active.get() {
309318
return Err(io::ErrorKind::NotConnected.into());
@@ -338,9 +347,11 @@ impl<T> Drop for Sender<T> {
338347
}
339348
}
340349

350+
/// The receiving half of channel.
341351
pub struct Receiver<T>(Rc<Chan<T>>);
342352

343353
impl<T> Receiver<T> {
354+
/// Attempts to wait for a value on this receiver, returning `None` if the corresponding channel has hung up.
344355
pub fn recv(&self) -> Option<T> {
345356
if self.0.buffer.borrow().len() == 0 {
346357
if self.0.tx_count.get().is_zero() {

0 commit comments

Comments
 (0)