This commit is contained in:
Stjepan Glavina 2020-11-10 15:54:01 +01:00
parent b55198557b
commit 38141bb5b4
1 changed files with 46 additions and 12 deletions

View File

@ -92,6 +92,28 @@ impl<'a> Executor<'a> {
}
}
/// Returns `true` if there are no unfinished tasks.
///
/// # Examples
///
/// ```
/// use async_executor::Executor;
///
/// let ex = Executor::new();
/// assert!(ex.is_empty());
///
/// let task = ex.spawn(async {
/// println!("Hello world");
/// });
/// assert!(!ex.is_empty());
///
/// assert!(ex.try_tick());
/// assert!(ex.is_empty());
/// ```
pub fn is_empty(&self) -> bool {
self.state().active.lock().unwrap().is_empty()
}
/// Spawns a task onto the executor.
///
/// # Examples
@ -156,7 +178,7 @@ impl<'a> Executor<'a> {
}
}
/// Run a single task.
/// Runs a single task.
///
/// Running a task means simply polling its future once.
///
@ -214,11 +236,6 @@ impl<'a> Executor<'a> {
future.or(run_forever).await
}
/// Checks if the executor is empty and has no pending tasks to run.
pub fn is_empty(&self) -> bool {
self.state().active.lock().unwrap().is_empty()
}
/// Returns a function that schedules a runnable task when it gets woken up.
fn schedule(&self) -> impl Fn(Runnable) + Send + Sync + 'static {
let state = self.state().clone();
@ -303,6 +320,28 @@ impl<'a> LocalExecutor<'a> {
}
}
/// Returns `true` if there are no unfinished tasks.
///
/// # Examples
///
/// ```
/// use async_executor::LocalExecutor;
///
/// let local_ex = LocalExecutor::new();
/// assert!(local_ex.is_empty());
///
/// let task = local_ex.spawn(async {
/// println!("Hello world");
/// });
/// assert!(!local_ex.is_empty());
///
/// assert!(local_ex.try_tick());
/// assert!(local_ex.is_empty());
/// ```
pub fn is_empty(&self) -> bool {
self.inner().is_empty()
}
/// Spawns a task onto the executor.
///
/// # Examples
@ -356,7 +395,7 @@ impl<'a> LocalExecutor<'a> {
self.inner().try_tick()
}
/// Run a single task.
/// Runs a single task.
///
/// Running a task means simply polling its future once.
///
@ -398,11 +437,6 @@ impl<'a> LocalExecutor<'a> {
self.inner().run(future).await
}
/// Checks if the executor is empty and has no pending tasks to run.
pub fn is_empty(&self) -> bool {
self.inner().is_empty()
}
/// Returns a function that schedules a runnable task when it gets woken up.
fn schedule(&self) -> impl Fn(Runnable) + Send + Sync + 'static {
let state = self.inner().state().clone();