|
2 | 2 | In this module there're implementations & tests of `Actor`.
|
3 | 3 | */
|
4 | 4 |
|
5 |
| -use std::collections::{self, HashMap}; |
| 5 | +use std::collections::HashMap; |
6 | 6 | use std::sync::{
|
7 | 7 | atomic::{AtomicBool, Ordering},
|
8 | 8 | Arc, Mutex,
|
@@ -38,6 +38,8 @@ pub trait Actor<Msg, ContextValue, HandleType, Functor>: UniqueId<String> {
|
38 | 38 | fn get_handle(&self) -> HandleType;
|
39 | 39 | fn get_handle_child(&self, name: impl Into<String>) -> Option<HandleType>;
|
40 | 40 | fn get_handle_parent(&self) -> Option<HandleType>;
|
| 41 | + |
| 42 | + fn for_each_child(&self, func: impl FnMut(&String, &mut HandleType)); |
41 | 43 | }
|
42 | 44 |
|
43 | 45 | pub trait Handle<Msg>: UniqueId<String> {
|
@@ -147,13 +149,6 @@ where
|
147 | 149 | }
|
148 | 150 | }
|
149 | 151 |
|
150 |
| - pub fn for_each_child( |
151 |
| - &self, |
152 |
| - mut func: impl FnMut(collections::hash_map::IterMut<String, HandleAsync<Msg>>), |
153 |
| - ) { |
154 |
| - func(self.children_handle_map.lock().unwrap().iter_mut()); |
155 |
| - } |
156 |
| - |
157 | 152 | pub fn is_started(&mut self) -> bool {
|
158 | 153 | let started_alive = self.started_alive.lock().unwrap();
|
159 | 154 | let &(ref started, _) = &*started_alive;
|
@@ -312,6 +307,12 @@ where
|
312 | 307 | fn get_handle_parent(&self) -> Option<HandleAsync<Msg>> {
|
313 | 308 | return self.parent_handle.clone();
|
314 | 309 | }
|
| 310 | + |
| 311 | + fn for_each_child(&self, mut func: impl FnMut(&String, &mut HandleAsync<Msg>)) { |
| 312 | + for (id, handle) in self.children_handle_map.lock().unwrap().iter_mut() { |
| 313 | + func(id, handle); |
| 314 | + } |
| 315 | + } |
315 | 316 | }
|
316 | 317 |
|
317 | 318 | #[test]
|
@@ -368,11 +369,9 @@ fn test_actor_common() {
|
368 | 369 | result_string_thread.push_back(ids.clone());
|
369 | 370 | }
|
370 | 371 |
|
371 |
| - this.for_each_child(move |iter| { |
372 |
| - for (id, handle) in iter { |
373 |
| - println!("Actor Shutdown id {:?}", id); |
374 |
| - handle.send(Value::Shutdown); |
375 |
| - } |
| 372 | + this.for_each_child(move |id, handle| { |
| 373 | + println!("Actor Shutdown id {:?}", id); |
| 374 | + handle.send(Value::Shutdown); |
376 | 375 | });
|
377 | 376 | this.stop();
|
378 | 377 | }
|
|
0 commit comments