Skip to content

Commit 471dddc

Browse files
committed
Make the ATM example use Offer2 and Offer3
1 parent 637646e commit 471dddc

File tree

1 file changed

+27
-35
lines changed

1 file changed

+27
-35
lines changed

examples/atm.rs

Lines changed: 27 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,14 @@ use session_types::*;
33
use std::thread::spawn;
44

55
type Id = String;
6-
type Atm = Recv<Id, Choose<Rec<AtmInner>, Eps>>;
6+
type Atm = Recv<Id, Choose2<Rec<AtmInner>, Eps>>;
77

8-
type AtmInner = Offer<AtmDeposit,
9-
Offer<AtmWithdraw,
10-
Offer<AtmBalance,
11-
Eps>>>;
8+
type AtmInner = Offer3<AtmDeposit,
9+
AtmWithdraw,
10+
Eps>;
1211

1312
type AtmDeposit = Recv<u64, Send<u64, Var<Z>>>;
14-
type AtmWithdraw = Recv<u64, Choose<Var<Z>, Var<Z>>>;
15-
type AtmBalance = Send<u64, Var<Z>>;
13+
type AtmWithdraw = Recv<u64, Choose2<Var<Z>, Var<Z>>>;
1614

1715
type Client = <Atm as HasDual>::Dual;
1816

@@ -30,59 +28,53 @@ fn atm(c: Chan<(), Atm>) {
3028
c.sel1().enter()
3129
};
3230
let mut balance = 0;
31+
3332
loop {
34-
c = offer! {
35-
c,
36-
Deposit => {
33+
c = match c.offer() {
34+
Branch3::B1(c) => {
3735
let (c, amt) = c.recv();
38-
balance += amt;
39-
c.send(balance).zero()
36+
balance = amt;
37+
c.send(balance).zero() // c.send(new_bal): Chan<(AtmInner, ()) Var<Z>>
4038
},
41-
Withdraw => {
39+
Branch3::B2(c) => {
4240
let (c, amt) = c.recv();
43-
if amt > balance {
44-
c.sel2().zero()
45-
} else {
46-
balance -= amt;
41+
if amt <= balance {
42+
balance = balance - amt;
4743
c.sel1().zero()
44+
} else {
45+
c.sel2().zero()
4846
}
4947
},
50-
Balance => {
51-
c.send(balance).zero()
52-
},
53-
Quit => {
54-
c.close();
55-
break
56-
}
57-
}
48+
Branch3::B3(c) => { c.close(); break }
49+
};
5850
}
5951
}
6052

6153
fn deposit_client(c: Chan<(), Client>) {
6254
let c = match c.send("Deposit Client".to_string()).offer() {
63-
Left(c) => c.enter(),
64-
Right(_) => panic!("deposit_client: expected to be approved")
55+
B1(c) => c.enter(),
56+
B2(_) => panic!("deposit_client: expected to be approved")
6557
};
6658

6759
let (c, new_balance) = c.sel1().send(200).recv();
6860
println!("deposit_client: new balance: {}", new_balance);
69-
c.zero().skip3().close();
61+
c.zero().sel3().close();
7062
}
7163

7264
fn withdraw_client(c: Chan<(), Client>) {
7365
let c = match c.send("Withdraw Client".to_string()).offer() {
74-
Left(c) => c.enter(),
75-
Right(_) => panic!("withdraw_client: expected to be approved")
66+
B1(c) => c.enter(),
67+
B2(_) => panic!("withdraw_client: expected to be approved")
7668
};
7769

78-
match c.sel2().sel1().send(100).offer() {
79-
Left(c) => {
70+
match c.sel2().send(100).offer() {
71+
B1(c) => {
8072
println!("withdraw_client: Successfully withdrew 100");
81-
c.zero().skip3().close();
73+
c.zero().sel3().close();
8274
}
83-
Right(c) => {
75+
B2(c) => {
8476
println!("withdraw_client: Could not withdraw. Depositing instead.");
85-
c.zero().sel1().send(50).recv().0.zero().skip3().close();
77+
c.zero().sel1().send(50).recv().0.zero().sel3().close();
8678
}
8779
}
8880
}

0 commit comments

Comments
 (0)