rust-crates / ergo_sync

making creating and synchronizing threads ergonomic, therefore fun!
http://docs.rs/ergo_sync
Other
5 stars 0 forks source link

Use `crossbeam-channel` for the channels #4

Closed vitiral closed 6 years ago

vitiral commented 6 years ago

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

Better macros / syntax

However much I would like to do these things I would not like to wrap the types or add a method.

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! and ch_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
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
}
vitiral commented 6 years ago

the major advantage of ch! being a macro is that panicing uses panic! and therefore contains the line number of failure.