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>
impl<Role1, T1: 'static, Cont1, Role2, T2: 'static, Cont2> Choice<Role1, T1, Cont1, Role2, T2, Cont2>
Sourcepub fn recv<Tr: Transport>(
self,
c1: &mut Channel<Role1, Tr>,
c2: &mut Channel<Role2, Tr>,
) -> Result<ChoiceResult<T1, Cont1, T2, Cont2>>
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§
Auto Trait Implementations§
impl<Role1, T1, Cont1, Role2, T2, Cont2> Freeze for Choice<Role1, T1, Cont1, Role2, T2, Cont2>
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>
impl<Role1, T1, Cont1, Role2, T2, Cont2> Sync for Choice<Role1, T1, Cont1, Role2, T2, Cont2>
impl<Role1, T1, Cont1, Role2, T2, Cont2> Unpin for Choice<Role1, T1, Cont1, Role2, T2, Cont2>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more