[][src]Macro fumio_utils::mpsc

macro_rules! mpsc {
    (mod $($tt:tt)*) => { ... };
    (pub mod $($tt:tt)*) => { ... };
    (pub(crate) mod $($tt:tt)*) => { ... };
}

Create a list "link" and "head" datatype; the "link" type must be used in a data structure as member; this data structure then can be pushed as Arc<...> to the list head.

A single thread can then collect popped nodes by iterating over head.start_pop(); the returned iterator must be dropped before starting a new run. Due to these restrictions the function is unsafe.

You also need to ensure a node is only pushed to a single list at a time; such gate needs to be an atomic.

Example

Memory synchronization missing as it is only a single thread example.

fn main() {
    use std::sync::Arc;
    mod example {
        fumio_utils::mpsc! {
            pub mod ex1 {
                link MyLink;
                head MyHead;
                member my_link of MyData;
            }
        }

        pub struct MyData {
            my_link: MyLink,
            pub value: u32,
        }

        impl MyData {
            pub fn new(value: u32) -> Self {
                MyData {
                    my_link: MyLink::default(),
                    value,
                }
            }
        }
    }
    use example::*;

    let list = MyHead::new();
    let data1 = Arc::new(MyData::new(1));
    let data2 = Arc::new(MyData::new(2));
    list.push(data1);
    list.push(data2);
    let mut counter = 1;
    for item in unsafe { list.start_pop() } {
        assert_eq!(counter, item.value);
        counter += 1;
    }
}