Skip to main content

Executor

Struct Executor 

Source
pub struct Executor { /* private fields */ }
Expand description

The main executor that runs async tasks and handles I/O events.

Implementations§

Source§

impl Executor

Source

pub fn new() -> Result<Self>

Creates a new executor with default ID generator.

Examples found in repository?
examples/basic_spawn.rs (line 4)
3fn main() {
4    let mut executor = Executor::new().unwrap();
5    executor.spawn(Task::new(async {
6        println!("Hello from an async task!");
7    }));
8    executor.run();
9}
More examples
Hide additional examples
examples/custom_id_timer.rs (line 6)
5fn main() {
6    let executor = Arc::new(Mutex::new(Executor::new().unwrap()));
7    let timer_data = executor.lock().unwrap().timer_data();
8    let start = std::time::Instant::now();
9
10    {
11        let mut exec = executor.lock().unwrap();
12        exec.spawn(Task::new(async move {
13            println!("Starting timer at {:?}", start.elapsed());
14            std::io::Write::flush(&mut std::io::stdout()).unwrap();
15            let timer = TimerFuture::new(Duration::from_millis(500), timer_data);
16            timer.await;
17            println!("Timer fired at {:?}", start.elapsed());
18        }));
19    }
20
21    executor.lock().unwrap().run();
22}
Source

pub fn spawn(&mut self, task: Task)

Spawns a new task with an async future.

Examples found in repository?
examples/basic_spawn.rs (lines 5-7)
3fn main() {
4    let mut executor = Executor::new().unwrap();
5    executor.spawn(Task::new(async {
6        println!("Hello from an async task!");
7    }));
8    executor.run();
9}
More examples
Hide additional examples
examples/custom_id_timer.rs (lines 12-18)
5fn main() {
6    let executor = Arc::new(Mutex::new(Executor::new().unwrap()));
7    let timer_data = executor.lock().unwrap().timer_data();
8    let start = std::time::Instant::now();
9
10    {
11        let mut exec = executor.lock().unwrap();
12        exec.spawn(Task::new(async move {
13            println!("Starting timer at {:?}", start.elapsed());
14            std::io::Write::flush(&mut std::io::stdout()).unwrap();
15            let timer = TimerFuture::new(Duration::from_millis(500), timer_data);
16            timer.await;
17            println!("Timer fired at {:?}", start.elapsed());
18        }));
19    }
20
21    executor.lock().unwrap().run();
22}
Source

pub fn register_timer(&mut self, deadline: Instant, waker: Waker)

Registers a timer to wake at the given deadline.

Source

pub fn timer_data(&self) -> Arc<Mutex<TimerData>>

Gets the timer data for creating timers.

Examples found in repository?
examples/custom_id_timer.rs (line 7)
5fn main() {
6    let executor = Arc::new(Mutex::new(Executor::new().unwrap()));
7    let timer_data = executor.lock().unwrap().timer_data();
8    let start = std::time::Instant::now();
9
10    {
11        let mut exec = executor.lock().unwrap();
12        exec.spawn(Task::new(async move {
13            println!("Starting timer at {:?}", start.elapsed());
14            std::io::Write::flush(&mut std::io::stdout()).unwrap();
15            let timer = TimerFuture::new(Duration::from_millis(500), timer_data);
16            timer.await;
17            println!("Timer fired at {:?}", start.elapsed());
18        }));
19    }
20
21    executor.lock().unwrap().run();
22}
Source

pub fn run(&mut self)

Runs the executor event loop until all tasks complete.

Examples found in repository?
examples/basic_spawn.rs (line 8)
3fn main() {
4    let mut executor = Executor::new().unwrap();
5    executor.spawn(Task::new(async {
6        println!("Hello from an async task!");
7    }));
8    executor.run();
9}
More examples
Hide additional examples
examples/custom_id_timer.rs (line 21)
5fn main() {
6    let executor = Arc::new(Mutex::new(Executor::new().unwrap()));
7    let timer_data = executor.lock().unwrap().timer_data();
8    let start = std::time::Instant::now();
9
10    {
11        let mut exec = executor.lock().unwrap();
12        exec.spawn(Task::new(async move {
13            println!("Starting timer at {:?}", start.elapsed());
14            std::io::Write::flush(&mut std::io::stdout()).unwrap();
15            let timer = TimerFuture::new(Duration::from_millis(500), timer_data);
16            timer.await;
17            println!("Timer fired at {:?}", start.elapsed());
18        }));
19    }
20
21    executor.lock().unwrap().run();
22}
Source

pub fn reactor(&mut self) -> &mut Reactor

Returns a mutable reference to the reactor for I/O operations.

Auto Trait Implementations§

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.