pub struct ExecutorService<F, T>where
    F: FnOnce() -> T + Send + 'static,
    T: Send + 'static,{ /* private fields */ }
Expand description

The executor service that allows tasks to be submitted/executed on the underlying thread pool.

use executor_service::Executors;
use std::thread::sleep;
use core::time::Duration;

let mut executor_service = Executors::new_fixed_thread_pool(2).expect("Failed to create the thread pool");

let some_param = "Mr White";
let res = executor_service.submit_sync(move || {

  sleep(Duration::from_secs(5));
  println!("Hello {:}", some_param);
  println!("Long computation finished");
  2
}).expect("Failed to submit function");

println!("Result: {:#?}", res);
assert_eq!(res, 2);

Implementations§

source§

impl<F, T> ExecutorService<F, T>where F: FnOnce() -> T + Send + 'static, T: Send + 'static,

source

pub fn execute(&mut self, fun: F) -> Result<(), ExecutorServiceError>

Execute a function on the thread pool asynchronously with no return.

use executor_service::Executors;
use std::thread::sleep;
use core::time::Duration;
use std::thread;

let mut executor_service = Executors::new_fixed_thread_pool(2).expect("Failed to create the thread pool");

let some_param = "Mr White";
let res = executor_service.execute(move || {
  sleep(Duration::from_secs(1));
  println!("Hello {:} from thread {:}", some_param, thread::current().name().unwrap());
}).expect("Failed to execute function");

sleep(Duration::from_secs(3));
source

pub fn submit_sync(&mut self, fun: F) -> Result<T, ExecutorServiceError>

Submit a function and wait for its result synchronously

use executor_service::Executors;
use std::thread::sleep;
use core::time::Duration;

let mut executor_service = Executors::new_cached_thread_pool(None).expect("Failed to create the thread pool");

let some_param = "Mr White";
let res = executor_service.submit_sync(move || {

  sleep(Duration::from_secs(5));
  println!("Hello {:}", some_param);
  println!("Long computation finished");
  2
}).expect("Failed to submit function");

println!("Result: {:#?}", res);
assert_eq!(res, 2);
source

pub fn submit_async( &mut self, fun: F ) -> Result<Future<T>, ExecutorServiceError>

Submit a function and get a Future object to obtain the result asynchronously when needed.

use executor_service::Executors;
use std::thread::sleep;
use core::time::Duration;

let mut executor_service = Executors::new_cached_thread_pool(Some(5)).expect("Failed to create the thread pool");

let some_param = "Mr White";
let future = executor_service.submit_async(Box::new(move || {

  sleep(Duration::from_secs(3));
  println!("Hello {:}", some_param);
  println!("Long computation finished");
  "Some string result".to_string()
})).expect("Failed to submit function");

//Wait a bit more to see the future work.
println!("Main thread wait for 5 seconds");
sleep(Duration::from_secs(5));
let res = future.get().expect("Couldn't get a result");
println!("Result is {:}", &res);
assert_eq!(&res, "Some string result");
source

pub fn pool_type(&self) -> &PoolType

source

pub fn get_thread_count(&self) -> Result<u32, ExecutorServiceError>

Trait Implementations§

source§

impl<F, T> Drop for ExecutorService<F, T>where F: FnOnce() -> T + Send + 'static, T: Send + 'static,

source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<F, T> RefUnwindSafe for ExecutorService<F, T>

§

impl<F, T> Send for ExecutorService<F, T>

§

impl<F, T> Sync for ExecutorService<F, T>

§

impl<F, T> Unpin for ExecutorService<F, T>

§

impl<F, T> UnwindSafe for ExecutorService<F, T>

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

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

const: unstable · 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 Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

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

§

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

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.