As @burntsushi pointed out (and I had been looking into) chan is not necessarily the best crate for ergonomic channels -- crossbeam-channel is. However, there are several things that I think are unergonomic about both APIs that I would like ergo_sync to improve upon. I am very amenable to discussion on these points.
Panicing on Error
If you are trying to send but the receiver is disconnected, chan blocks forever and crossbeam-channel returns a RecvError. I would like to panic in the general case.
I would also like to panic for the reverse case (receiving when there are no senders).
Better macros / syntax
However much I would like to do these things I would not like to wrap the types or add a method.
[X] Instead I would like to propose the ch! macro (done in this commit with slightly different syntax)
Note: I chose the @ since it is similar to rust's pattern mattching syntax of assigning a matched variable. =<- is another possibility although I would want to use the same pattern in ch!andch_select!.
// Sending
ch!(send <- /* expression */); // blocks until the expression is sent or panics if there is no receiver.
// Receiving
let x;
ch!(x @<- recv); // blocks until a value is recieved, stored in x
ch!(let y @<- recv); // same as above but without the `let` block.
ch!(let mut z @<- recv); // can also specify mutability
In addition there should be a ch_select! macro which is kind of a combination of ch! above and select_loop!: (nevermind, select_loop will be fine for now)
ch_select!{
send <- /* expression*/ => { /* event expresssion */ },
y @<- recv => { /* expresion where you can use y */ },
// "default" cases"
disconnected => { ... } // if all cases would disconnect
would_block() => { ... } // if all cases would block
timed_out(timeout) => { ... } // if all cases would timeout
}
As @burntsushi pointed out (and I had been looking into)
chan
is not necessarily the best crate for ergonomic channels -- crossbeam-channel is. However, there are several things that I think are unergonomic about both APIs that I would likeergo_sync
to improve upon. I am very amenable to discussion on these points.Panicing on Error
send
but the receiver is disconnected,chan
blocks forever andcrossbeam-channel
returns aRecvError
. I would like to panic in the general case.Better macros / syntax
However much I would like to do these things I would not like to wrap the types or add a method.
ch!
macro (done in this commit with slightly different syntax)In addition there should be a: (nevermind, select_loop will be fine for now)ch_select!
macro which is kind of a combination ofch!
above andselect_loop!