Add documentation for channel macro

This commit is contained in:
Daniel Romero 2017-02-03 11:15:26 +01:00
parent 2c187eb3ae
commit ec0e81f0ae

View file

@ -1,3 +1,61 @@
/// Creates an implentation of sender which can be used to share an async channel for multiple message types.
///
/// # Examples
///
/// ```
/// struct D;
/// struct A;
///
/// enum Msg {
/// FirstType(D),
/// SecondType(A)
/// }
///
/// fn main() {
/// let (tx, rx) = channel::<Msg>();
///
/// let d_sender = DSender::create(tx.clone());
/// let a_sender = ASender::create(tx.clone());
/// subscribe(d_sender.clone());
/// subscribe2(d_sender.clone());
/// subscribe3(a_sender.clone());
//
/// let mut i = 0;
//
/// for m in rx {
/// i += 1;
/// match m {
/// Msg::FirstType(_) => println!("m: D {}", i),
/// Msg::SecondType(_) => println!("m: A {}", i)
/// };
/// }
/// }
///
/// fn subscribe(sender: DSender) {
/// thread::spawn(move|| {
/// sender.send(D).unwrap();
/// });
/// }
/// fn subscribe2(sender: DSender) {
/// thread::spawn(move|| {
/// thread::sleep(time::Duration::from_millis(10));
/// sender.send(D).unwrap();
/// });
/// }
/// fn subscribe3(sender: ASender) {
/// thread::spawn(move|| {
/// sender.send(A).unwrap();
/// });
/// }
/// implement_sender!(name => DSender,
/// wrap => D,
/// with => Msg,
/// variant => FirstType)
/// implement_sender!(name => ASender,
/// wrap => A,
/// with => Msg,
/// variant => SecondType)
/// ```
macro_rules! implement_sender {
(name => $name:ident,
wrap => $wrap_type:ident,