Choice

Struct Choice 

Source
pub struct Choice<Role1, T1, Cont1, Role2, T2, Cont2> { /* private fields */ }
Expand description

This node allows receiving from either R1 or R2, depending on an external choice.

The choice is modeled as a tuple on the sender side. The Other is either another Choice or End.

Implementations§

Source§

impl<Role1, T1: 'static, Cont1, Role2, T2: 'static, Cont2> Choice<Role1, T1, Cont1, Role2, T2, Cont2>

Source

pub fn recv<Tr: Transport>( self, c1: &mut Channel<Role1, Tr>, c2: &mut Channel<Role2, Tr>, ) -> Result<ChoiceResult<T1, Cont1, T2, Cont2>>

Examples found in repository?
examples/loop_three.rs (line 40)
30fn main() -> Result<()> {
31    let (mut ch_qa, mut ch_aq) = pair::<Q, A, _>(&Crossbeam);
32    let (mut ch_ab, mut ch_ba) = pair::<A, B, _>(&Crossbeam);
33    let (mut ch_ac, mut ch_ca) = pair::<A, C, _>(&Crossbeam);
34    let (mut ch_bc, mut ch_cb) = pair::<B, C, _>(&Crossbeam);
35
36    let thread_a = spawn(move || -> Result<End> {
37        let mut prot = prot_a().rec();
38        let mut ch_aq2 = ch_aq.clone();
39        loop {
40            match prot.recv(&mut ch_aq, &mut ch_aq2)? {
41                One(value, cont) => {
42                    let cont = cont.send(&mut ch_ab, value)?;
43                    let (value, cont) = match cont.recv(&mut ch_ac, &mut ch_ab)? {
44                        One(value, cont) => (value, cont),
45                        Two(value, cont) => (value, cont),
46                    };
47                    let cont = cont.send(&mut ch_aq, value)?;
48                    prot = cont.rec();
49                }
50                Two(v, cont) => {
51                    println!("process A got string {}", v);
52                    return Ok(cont);
53                }
54            }
55        }
56    });
57
58    let thread_b = spawn(move || -> Result<End> {
59        let mut prot = prot_b().rec();
60        loop {
61            let (value, p) = prot.recv(&mut ch_ba)?;
62            if value > 100 {
63                let cont = p.0.send(&mut ch_bc, value)?;
64                prot = cont.rec();
65            } else {
66                let cont = p.1.send(&mut ch_ba, value)?;
67                prot = cont.rec();
68            }
69        }
70    });
71
72    let thread_c = spawn(move || -> Result<End> {
73        let mut prot = prot_c().rec();
74        loop {
75            let (value, p) = prot.recv(&mut ch_cb)?;
76            prot = p.send(&mut ch_ca, value)?.rec();
77        }
78    });
79
80    // use the current thread for role Q
81    let prot = prot_q();
82    let prot = prot.send(&mut ch_qa, 1)?;
83    let (value, prot) = prot.recv(&mut ch_qa)?;
84    println!("received {}", value);
85    let _prot: End = prot.send(&mut ch_qa, "stop".to_string())?;
86
87    // all threads end now because A shuts down, killing the channel to B (which then shuts down),
88    // killing the channel to C (which then shuts down)
89    println!("1 {:?}", thread_a.join().unwrap());
90    println!("2 {:?}", thread_b.join().unwrap());
91    println!("3 {:?}", thread_c.join().unwrap());
92
93    Ok(())
94}

Trait Implementations§

Source§

impl<Role1, T1, Cont1: Default, Role2, T2, Cont2: Default> Default for Choice<Role1, T1, Cont1, Role2, T2, Cont2>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<Role1, T1, Cont1, Role2, T2, Cont2> Freeze for Choice<Role1, T1, Cont1, Role2, T2, Cont2>
where Cont1: Freeze, Cont2: Freeze,

§

impl<Role1, T1, Cont1, Role2, T2, Cont2> RefUnwindSafe for Choice<Role1, T1, Cont1, Role2, T2, Cont2>
where Cont1: RefUnwindSafe, Cont2: RefUnwindSafe, Role1: RefUnwindSafe, T1: RefUnwindSafe, Role2: RefUnwindSafe, T2: RefUnwindSafe,

§

impl<Role1, T1, Cont1, Role2, T2, Cont2> Send for Choice<Role1, T1, Cont1, Role2, T2, Cont2>
where Cont1: Send, Cont2: Send, Role1: Send, T1: Send, Role2: Send, T2: Send,

§

impl<Role1, T1, Cont1, Role2, T2, Cont2> Sync for Choice<Role1, T1, Cont1, Role2, T2, Cont2>
where Cont1: Sync, Cont2: Sync, Role1: Sync, T1: Sync, Role2: Sync, T2: Sync,

§

impl<Role1, T1, Cont1, Role2, T2, Cont2> Unpin for Choice<Role1, T1, Cont1, Role2, T2, Cont2>
where Cont1: Unpin, Cont2: Unpin, Role1: Unpin, T1: Unpin, Role2: Unpin, T2: Unpin,

§

impl<Role1, T1, Cont1, Role2, T2, Cont2> UnwindSafe for Choice<Role1, T1, Cont1, Role2, T2, Cont2>
where Cont1: UnwindSafe, Cont2: UnwindSafe, Role1: UnwindSafe, T1: UnwindSafe, Role2: UnwindSafe, T2: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.