Skip to content

Commit 66e51bf

Browse files
committed
Improve Actor.for_each_child(id, handle) API
1 parent e8221a4 commit 66e51bf

File tree

3 files changed

+16
-19
lines changed

3 files changed

+16
-19
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "fp_rust"
3-
version = "0.3.3"
3+
version = "0.3.4"
44
license = "MIT"
55
authors = ["JunYi JohnTeee Lee <johnteee@gmail.com>"]
66
edition = "2018"

README.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -416,11 +416,9 @@ let mut root = ActorAsync::new(
416416
result_string_thread.push_back(ids.clone());
417417
}
418418

419-
this.for_each_child(move |iter| {
420-
for (id, handle) in iter {
421-
println!("Actor Shutdown id {:?}", id);
422-
handle.send(Value::Shutdown);
423-
}
419+
this.for_each_child(move |id, handle| {
420+
println!("Actor Shutdown id {:?}", id);
421+
handle.send(Value::Shutdown);
424422
});
425423
this.stop();
426424
}

src/actor.rs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
In this module there're implementations & tests of `Actor`.
33
*/
44

5-
use std::collections::{self, HashMap};
5+
use std::collections::HashMap;
66
use std::sync::{
77
atomic::{AtomicBool, Ordering},
88
Arc, Mutex,
@@ -38,6 +38,8 @@ pub trait Actor<Msg, ContextValue, HandleType, Functor>: UniqueId<String> {
3838
fn get_handle(&self) -> HandleType;
3939
fn get_handle_child(&self, name: impl Into<String>) -> Option<HandleType>;
4040
fn get_handle_parent(&self) -> Option<HandleType>;
41+
42+
fn for_each_child(&self, func: impl FnMut(&String, &mut HandleType));
4143
}
4244

4345
pub trait Handle<Msg>: UniqueId<String> {
@@ -147,13 +149,6 @@ where
147149
}
148150
}
149151

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-
157152
pub fn is_started(&mut self) -> bool {
158153
let started_alive = self.started_alive.lock().unwrap();
159154
let &(ref started, _) = &*started_alive;
@@ -312,6 +307,12 @@ where
312307
fn get_handle_parent(&self) -> Option<HandleAsync<Msg>> {
313308
return self.parent_handle.clone();
314309
}
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+
}
315316
}
316317

317318
#[test]
@@ -368,11 +369,9 @@ fn test_actor_common() {
368369
result_string_thread.push_back(ids.clone());
369370
}
370371

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);
376375
});
377376
this.stop();
378377
}

0 commit comments

Comments
 (0)