Skip to content

Commit d750b15

Browse files
committed
Added documentation and examples for the init() callback
1 parent 28d2db5 commit d750b15

File tree

6 files changed

+66
-0
lines changed

6 files changed

+66
-0
lines changed

concurrency/src/tasks/gen_server.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,9 @@ where
146146
}
147147
}
148148

149+
/// Initialization function. It's called before main loop. It
150+
/// can be overrided on implementations in case initial steps are
151+
/// required.
149152
fn init(
150153
&mut self,
151154
_handle: &GenServerHandle<Self>,

concurrency/src/threads/gen_server.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,9 @@ where
119119
}
120120
}
121121

122+
/// Initialization function. It's called before main loop. It
123+
/// can be overrided on implementations in case initial steps are
124+
/// required.
122125
fn init(
123126
&mut self,
124127
_handle: &GenServerHandle<Self>,

examples/bank/src/main.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,33 @@ use spawned_rt::tasks as rt;
3131

3232
fn main() {
3333
rt::run(async {
34+
// Starting the bank
3435
let mut name_server = Bank::start(HashMap::new());
3536

37+
// Testing initial balance for "main" account
38+
let result = Bank::withdraw(&mut name_server, "main".to_string(), 15).await;
39+
tracing::info!("Withdraw result {result:?}");
40+
assert_eq!(
41+
result,
42+
Ok(BankOutMessage::WidrawOk {
43+
who: "main".to_string(),
44+
amount: 985
45+
})
46+
);
47+
3648
let joe = "Joe".to_string();
3749

50+
// Error on deposit for an unexistent account
3851
let result = Bank::deposit(&mut name_server, joe.clone(), 10).await;
3952
tracing::info!("Deposit result {result:?}");
4053
assert_eq!(result, Err(BankError::NotACustomer { who: joe.clone() }));
4154

55+
// Account creation
4256
let result = Bank::new_account(&mut name_server, "Joe".to_string()).await;
4357
tracing::info!("New account result {result:?}");
4458
assert_eq!(result, Ok(BankOutMessage::Welcome { who: joe.clone() }));
4559

60+
// Deposit
4661
let result = Bank::deposit(&mut name_server, "Joe".to_string(), 10).await;
4762
tracing::info!("Deposit result {result:?}");
4863
assert_eq!(
@@ -53,6 +68,7 @@ fn main() {
5368
})
5469
);
5570

71+
// Deposit
5672
let result = Bank::deposit(&mut name_server, "Joe".to_string(), 30).await;
5773
tracing::info!("Deposit result {result:?}");
5874
assert_eq!(
@@ -63,6 +79,7 @@ fn main() {
6379
})
6480
);
6581

82+
// Withdrawal
6683
let result = Bank::withdraw(&mut name_server, "Joe".to_string(), 15).await;
6784
tracing::info!("Withdraw result {result:?}");
6885
assert_eq!(
@@ -73,6 +90,7 @@ fn main() {
7390
})
7491
);
7592

93+
// Withdrawal with not enough balance
7694
let result = Bank::withdraw(&mut name_server, "Joe".to_string(), 45).await;
7795
tracing::info!("Withdraw result {result:?}");
7896
assert_eq!(
@@ -83,6 +101,7 @@ fn main() {
83101
})
84102
);
85103

104+
// Full withdrawal
86105
let result = Bank::withdraw(&mut name_server, "Joe".to_string(), 25).await;
87106
tracing::info!("Withdraw result {result:?}");
88107
assert_eq!(
@@ -93,6 +112,7 @@ fn main() {
93112
})
94113
);
95114

115+
// Stopping the bank
96116
let result = Bank::stop(&mut name_server).await;
97117
tracing::info!("Stop result {result:?}");
98118
assert_eq!(result, Ok(BankOutMessage::Stopped));

examples/bank/src/server.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,16 @@ impl GenServer for Bank {
5151
Self {}
5252
}
5353

54+
// Initializing "main" account with 1000 in balance to test init() callback.
55+
async fn init(
56+
&mut self,
57+
_handle: &GenServerHandle<Self>,
58+
mut state: Self::State,
59+
) -> Result<Self::State, Self::Error> {
60+
state.insert("main".to_string(), 1000);
61+
Ok(state)
62+
}
63+
5464
async fn handle_call(
5565
&mut self,
5666
message: Self::CallMsg,

examples/bank_threads/src/main.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,33 @@ use spawned_rt::threads as rt;
3131

3232
fn main() {
3333
rt::run(|| {
34+
// Starting the bank
3435
let mut name_server = Bank::start(HashMap::new());
3536

37+
// Testing initial balance for "main" account
38+
let result = Bank::withdraw(&mut name_server, "main".to_string(), 15);
39+
tracing::info!("Withdraw result {result:?}");
40+
assert_eq!(
41+
result,
42+
Ok(BankOutMessage::WidrawOk {
43+
who: "main".to_string(),
44+
amount: 985
45+
})
46+
);
47+
3648
let joe = "Joe".to_string();
3749

50+
// Error on deposit for an unexistent account
3851
let result = Bank::deposit(&mut name_server, joe.clone(), 10);
3952
tracing::info!("Deposit result {result:?}");
4053
assert_eq!(result, Err(BankError::NotACustomer { who: joe.clone() }));
4154

55+
// Account creation
4256
let result = Bank::new_account(&mut name_server, "Joe".to_string());
4357
tracing::info!("New account result {result:?}");
4458
assert_eq!(result, Ok(BankOutMessage::Welcome { who: joe.clone() }));
4559

60+
// Deposit
4661
let result = Bank::deposit(&mut name_server, "Joe".to_string(), 10);
4762
tracing::info!("Deposit result {result:?}");
4863
assert_eq!(
@@ -53,6 +68,7 @@ fn main() {
5368
})
5469
);
5570

71+
// Deposit
5672
let result = Bank::deposit(&mut name_server, "Joe".to_string(), 30);
5773
tracing::info!("Deposit result {result:?}");
5874
assert_eq!(
@@ -63,6 +79,7 @@ fn main() {
6379
})
6480
);
6581

82+
// Withdrawal
6683
let result = Bank::withdraw(&mut name_server, "Joe".to_string(), 15);
6784
tracing::info!("Withdraw result {result:?}");
6885
assert_eq!(
@@ -73,6 +90,7 @@ fn main() {
7390
})
7491
);
7592

93+
// Withdrawal with not enough balance
7694
let result = Bank::withdraw(&mut name_server, "Joe".to_string(), 45);
7795
tracing::info!("Withdraw result {result:?}");
7896
assert_eq!(
@@ -83,6 +101,7 @@ fn main() {
83101
})
84102
);
85103

104+
// Full withdrawal
86105
let result = Bank::withdraw(&mut name_server, "Joe".to_string(), 25);
87106
tracing::info!("Withdraw result {result:?}");
88107
assert_eq!(
@@ -93,6 +112,7 @@ fn main() {
93112
})
94113
);
95114

115+
// Stopping the bank
96116
let result = Bank::stop(&mut name_server);
97117
tracing::info!("Stop result {result:?}");
98118
assert_eq!(result, Ok(BankOutMessage::Stopped));

examples/bank_threads/src/server.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,16 @@ impl GenServer for Bank {
4747
Self {}
4848
}
4949

50+
// Initializing "main" account with 1000 in balance to test init() callback.
51+
fn init(
52+
&mut self,
53+
_handle: &GenServerHandle<Self>,
54+
mut state: Self::State,
55+
) -> Result<Self::State, Self::Error> {
56+
state.insert("main".to_string(), 1000);
57+
Ok(state)
58+
}
59+
5060
fn handle_call(
5161
&mut self,
5262
message: Self::CallMsg,

0 commit comments

Comments
 (0)