pub struct Executor<'a, const C: usize = 64> { /* private fields */ }
Expand description
An async executor.
§Examples
A multi-threaded executor:
ⓘ
use async_channel::unbounded;
use easy_parallel::Parallel;
use edge_executor::{Executor, block_on};
let ex: Executor = Default::default();
let (signal, shutdown) = unbounded::<()>();
Parallel::new()
// Run four executor threads.
.each(0..4, |_| block_on(ex.run(shutdown.recv())))
// Run the main future on the current thread.
.finish(|| block_on(async {
println!("Hello world!");
drop(signal);
}));
Implementations§
Source§impl<'a, const C: usize> Executor<'a, C>
impl<'a, const C: usize> Executor<'a, C>
Sourcepub const fn new() -> Self
pub const fn new() -> Self
Creates a new executor.
§Examples
use edge_executor::Executor;
let ex: Executor = Default::default();
Sourcepub fn spawn<F>(&self, fut: F) -> Task<F::Output> ⓘ
pub fn spawn<F>(&self, fut: F) -> Task<F::Output> ⓘ
Spawns a task onto the executor.
§Examples
use edge_executor::Executor;
let ex: Executor = Default::default();
let task = ex.spawn(async {
println!("Hello world");
});
Note that if the executor’s queue size is equal to the number of currently spawned and running tasks, spawning this additional task might cause the executor to panic later, when the task is scheduled for polling.
Sourcepub fn try_tick(&self) -> bool
pub fn try_tick(&self) -> bool
Attempts to run a task if at least one is scheduled.
Running a scheduled task means simply polling its future once.
§Examples
use edge_executor::Executor;
let ex: Executor = Default::default();
assert!(!ex.try_tick()); // no tasks to run
let task = ex.spawn(async {
println!("Hello world");
});
assert!(ex.try_tick()); // a task was found
Sourcepub async fn tick(&self)
pub async fn tick(&self)
Runs a single task asynchronously.
Running a task means simply polling its future once.
If no tasks are scheduled when this method is called, it will wait until one is scheduled.
§Examples
use edge_executor::{Executor, block_on};
let ex: Executor = Default::default();
let task = ex.spawn(async {
println!("Hello world");
});
block_on(ex.tick()); // runs the task
Sourcepub async fn run<F>(&self, fut: F) -> F::Output
pub async fn run<F>(&self, fut: F) -> F::Output
Runs the executor asynchronously until the given future completes.
§Examples
use edge_executor::{Executor, block_on};
let ex: Executor = Default::default();
let task = ex.spawn(async { 1 + 2 });
let res = block_on(ex.run(async { task.await * 2 }));
assert_eq!(res, 6);
Trait Implementations§
impl<'a, const C: usize> Send for Executor<'a, C>
impl<'a, const C: usize> Sync for Executor<'a, C>
Auto Trait Implementations§
impl<'a, const C: usize = 64> !Freeze for Executor<'a, C>
impl<'a, const C: usize = 64> !RefUnwindSafe for Executor<'a, C>
impl<'a, const C: usize> Unpin for Executor<'a, C>
impl<'a, const C: usize = 64> !UnwindSafe for Executor<'a, C>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more