Struct StatusExecutor

Source
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>
where T: Send + Sync + 'static, S: Send + Clone + 'static,

Source

pub fn new<E, F>(e: E, f: F) -> Self
where F: FnOnce(StatusSender<S>) -> T + Send + 'static, E: Context,

Run f in the Execution context _e, creating an Executor

§Arguments
  • e: The context to run on. For StdContext or RayonGlobalContext you can just use ::default(), they are stateless.
  • f: The function to run. f must be FnOnce + Send + 'static - as if you would pass it to std::thread::spawn

Additionally, f takes a StatusSender<S> that can be use to update the status.

Examples found in repository?
examples/with_panic.rs (line 12)
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
Hide additional examples
examples/with_std.rs (line 13)
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}
Source

pub fn is_done(&self) -> bool

Returns true if the function passed into StatusExecutor::new() has completed

Examples found in repository?
examples/with_panic.rs (line 16)
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
Hide additional examples
examples/with_std.rs (line 17)
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}
Source

pub fn result(&self) -> Option<Arc<T>>

Returns Some(Arc<T>) if the function passed into StatusExecutor::new() has completed, or None otherwise

Source

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.

There is no way to retrieve the data if you call this before the work function is done. So you probably only want to only `Executor::take()` if `Executor::is_done()` returnd true before.

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?
examples/with_panic.rs (line 28)
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
Hide additional examples
examples/with_std.rs (line 29)
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}
Source

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?
examples/with_panic.rs (line 17)
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
Hide additional examples
examples/with_std.rs (line 18)
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}
Source

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?
examples/with_panic.rs (line 27)
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
Hide additional examples
examples/with_std.rs (line 28)
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}

Auto Trait Implementations§

§

impl<T, S> !Freeze for StatusExecutor<T, S>

§

impl<T, S> RefUnwindSafe for StatusExecutor<T, S>

§

impl<T, S> Send for StatusExecutor<T, S>
where S: Send, T: Sync + Send,

§

impl<T, S> !Sync for StatusExecutor<T, S>

§

impl<T, S> Unpin for StatusExecutor<T, S>
where S: Unpin,

§

impl<T, S> UnwindSafe for StatusExecutor<T, S>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.