pub struct StatusExecutor<T, S> { /* private fields */ }
Expand description
A StatusExecutor
runs a function in some Context
(this will usually be a thread)
For the general behaviour, see Executor
.
StatusExecutor
additionally takes a Status S
(which has to be Send + 'static
for the underlying channel).
This status can then be updated from within the work function.
Implementations§
Source§impl<T, S> StatusExecutor<T, S>
impl<T, S> StatusExecutor<T, S>
Sourcepub fn new<E, F>(e: E, f: F) -> Self
pub fn new<E, F>(e: E, f: F) -> Self
Run f
in the Execution context _e
, creating an Executor
§Arguments
e
: The context to run on. ForStdContext
orRayonGlobalContext
you can just use::default()
, they are stateless.f
: The function to run.f
must beFnOnce + Send + 'static
- as if you would pass it tostd::thread::spawn
Additionally, f
takes a StatusSender<S>
that can be use to update the status.
Examples found in repository?
11fn main() {
12 let e1 = StatusExecutor::new(StdContext::default(), takes_a_while);
13 std::thread::sleep(std::time::Duration::from_millis(100));
14 let e2 = StatusExecutor::new(StdContext::default(), takes_a_while);
15
16 while !e1.is_done() || !e2.is_done() {
17 if let Some(s) = e1.status() {
18 println!("E1: {}", s);
19 }
20 if let Some(s) = e2.status() {
21 println!("E2: {}", s);
22 }
23 }
24
25 println!(
26 "E1: {} - {}",
27 e1.latest_status().unwrap(),
28 e1.take_result().unwrap()
29 );
30 println!(
31 "E2: {} - {}",
32 e2.latest_status().unwrap(),
33 e2.take_result().unwrap()
34 );
35}
More examples
12fn main() {
13 let e1 = StatusExecutor::new(StdContext::default(), takes_a_while);
14 std::thread::sleep(std::time::Duration::from_millis(100));
15 let e2 = StatusExecutor::new(StdContext::default(), takes_a_while);
16
17 while !e1.is_done() || !e2.is_done() {
18 if let Some(s) = e1.status() {
19 println!("E1: {}", s);
20 }
21 if let Some(s) = e2.status() {
22 println!("E2: {}", s);
23 }
24 }
25
26 println!(
27 "E1: {} - {}",
28 e1.latest_status().unwrap(),
29 e1.take_result().unwrap()
30 );
31 println!(
32 "E2: {} - {}",
33 e2.latest_status().unwrap(),
34 e2.take_result().unwrap()
35 );
36}
Sourcepub fn is_done(&self) -> bool
pub fn is_done(&self) -> bool
Returns true if the function passed into StatusExecutor::new()
has completed
Examples found in repository?
11fn main() {
12 let e1 = StatusExecutor::new(StdContext::default(), takes_a_while);
13 std::thread::sleep(std::time::Duration::from_millis(100));
14 let e2 = StatusExecutor::new(StdContext::default(), takes_a_while);
15
16 while !e1.is_done() || !e2.is_done() {
17 if let Some(s) = e1.status() {
18 println!("E1: {}", s);
19 }
20 if let Some(s) = e2.status() {
21 println!("E2: {}", s);
22 }
23 }
24
25 println!(
26 "E1: {} - {}",
27 e1.latest_status().unwrap(),
28 e1.take_result().unwrap()
29 );
30 println!(
31 "E2: {} - {}",
32 e2.latest_status().unwrap(),
33 e2.take_result().unwrap()
34 );
35}
More examples
12fn main() {
13 let e1 = StatusExecutor::new(StdContext::default(), takes_a_while);
14 std::thread::sleep(std::time::Duration::from_millis(100));
15 let e2 = StatusExecutor::new(StdContext::default(), takes_a_while);
16
17 while !e1.is_done() || !e2.is_done() {
18 if let Some(s) = e1.status() {
19 println!("E1: {}", s);
20 }
21 if let Some(s) = e2.status() {
22 println!("E2: {}", s);
23 }
24 }
25
26 println!(
27 "E1: {} - {}",
28 e1.latest_status().unwrap(),
29 e1.take_result().unwrap()
30 );
31 println!(
32 "E2: {} - {}",
33 e2.latest_status().unwrap(),
34 e2.take_result().unwrap()
35 );
36}
Sourcepub fn result(&self) -> Option<Arc<T>>
pub fn result(&self) -> Option<Arc<T>>
Returns Some(Arc<T>)
if the function passed into StatusExecutor::new()
has completed, or None
otherwise
Sourcepub fn take_result(self) -> Option<T>
pub fn take_result(self) -> Option<T>
Consumes this Executor
, returning either Some(T)
if the executor has completed, or None
if it has not.
StatusSender::send
will begin to return false if you take before the execution is done, so you could abort processing that way.
Examples found in repository?
11fn main() {
12 let e1 = StatusExecutor::new(StdContext::default(), takes_a_while);
13 std::thread::sleep(std::time::Duration::from_millis(100));
14 let e2 = StatusExecutor::new(StdContext::default(), takes_a_while);
15
16 while !e1.is_done() || !e2.is_done() {
17 if let Some(s) = e1.status() {
18 println!("E1: {}", s);
19 }
20 if let Some(s) = e2.status() {
21 println!("E2: {}", s);
22 }
23 }
24
25 println!(
26 "E1: {} - {}",
27 e1.latest_status().unwrap(),
28 e1.take_result().unwrap()
29 );
30 println!(
31 "E2: {} - {}",
32 e2.latest_status().unwrap(),
33 e2.take_result().unwrap()
34 );
35}
More examples
12fn main() {
13 let e1 = StatusExecutor::new(StdContext::default(), takes_a_while);
14 std::thread::sleep(std::time::Duration::from_millis(100));
15 let e2 = StatusExecutor::new(StdContext::default(), takes_a_while);
16
17 while !e1.is_done() || !e2.is_done() {
18 if let Some(s) = e1.status() {
19 println!("E1: {}", s);
20 }
21 if let Some(s) = e2.status() {
22 println!("E2: {}", s);
23 }
24 }
25
26 println!(
27 "E1: {} - {}",
28 e1.latest_status().unwrap(),
29 e1.take_result().unwrap()
30 );
31 println!(
32 "E2: {} - {}",
33 e2.latest_status().unwrap(),
34 e2.take_result().unwrap()
35 );
36}
Sourcepub fn status(&self) -> Option<S>
pub fn status(&self) -> Option<S>
Returns a status Some(S)
if there are any status infos pending, or None
if there isn’t anything new.
Examples found in repository?
11fn main() {
12 let e1 = StatusExecutor::new(StdContext::default(), takes_a_while);
13 std::thread::sleep(std::time::Duration::from_millis(100));
14 let e2 = StatusExecutor::new(StdContext::default(), takes_a_while);
15
16 while !e1.is_done() || !e2.is_done() {
17 if let Some(s) = e1.status() {
18 println!("E1: {}", s);
19 }
20 if let Some(s) = e2.status() {
21 println!("E2: {}", s);
22 }
23 }
24
25 println!(
26 "E1: {} - {}",
27 e1.latest_status().unwrap(),
28 e1.take_result().unwrap()
29 );
30 println!(
31 "E2: {} - {}",
32 e2.latest_status().unwrap(),
33 e2.take_result().unwrap()
34 );
35}
More examples
12fn main() {
13 let e1 = StatusExecutor::new(StdContext::default(), takes_a_while);
14 std::thread::sleep(std::time::Duration::from_millis(100));
15 let e2 = StatusExecutor::new(StdContext::default(), takes_a_while);
16
17 while !e1.is_done() || !e2.is_done() {
18 if let Some(s) = e1.status() {
19 println!("E1: {}", s);
20 }
21 if let Some(s) = e2.status() {
22 println!("E2: {}", s);
23 }
24 }
25
26 println!(
27 "E1: {} - {}",
28 e1.latest_status().unwrap(),
29 e1.take_result().unwrap()
30 );
31 println!(
32 "E2: {} - {}",
33 e2.latest_status().unwrap(),
34 e2.take_result().unwrap()
35 );
36}
Sourcepub fn latest_status(&self) -> Option<S>
pub fn latest_status(&self) -> Option<S>
Checks for a new status and returns the latest one it has seen.
Similar to StatusExecutor::status
, but will always return something.
Technically, since this is using try_iter, you could produce status faster than consuming them with this function Then it would block. However, for latest_status to be slower than a producer, the producer would basically have to call send() in an infinite loop That’s not an intended use case, so its deemed an acceptable issue.
Examples found in repository?
11fn main() {
12 let e1 = StatusExecutor::new(StdContext::default(), takes_a_while);
13 std::thread::sleep(std::time::Duration::from_millis(100));
14 let e2 = StatusExecutor::new(StdContext::default(), takes_a_while);
15
16 while !e1.is_done() || !e2.is_done() {
17 if let Some(s) = e1.status() {
18 println!("E1: {}", s);
19 }
20 if let Some(s) = e2.status() {
21 println!("E2: {}", s);
22 }
23 }
24
25 println!(
26 "E1: {} - {}",
27 e1.latest_status().unwrap(),
28 e1.take_result().unwrap()
29 );
30 println!(
31 "E2: {} - {}",
32 e2.latest_status().unwrap(),
33 e2.take_result().unwrap()
34 );
35}
More examples
12fn main() {
13 let e1 = StatusExecutor::new(StdContext::default(), takes_a_while);
14 std::thread::sleep(std::time::Duration::from_millis(100));
15 let e2 = StatusExecutor::new(StdContext::default(), takes_a_while);
16
17 while !e1.is_done() || !e2.is_done() {
18 if let Some(s) = e1.status() {
19 println!("E1: {}", s);
20 }
21 if let Some(s) = e2.status() {
22 println!("E2: {}", s);
23 }
24 }
25
26 println!(
27 "E1: {} - {}",
28 e1.latest_status().unwrap(),
29 e1.take_result().unwrap()
30 );
31 println!(
32 "E2: {} - {}",
33 e2.latest_status().unwrap(),
34 e2.take_result().unwrap()
35 );
36}