pub struct Cosync<T: ?Sized> { /* private fields */ }
Expand description
A single-threaded, sequential, parameterized async task queue.
This executor allows you to queue multiple tasks in sequence, and to queue tasks within other tasks. Tasks are done in the order they are queued.
You can queue a task by using queue, by spawning a CosyncQueueHandle and calling queue, or, within a task, calling queue_task on CosyncInput.
Implementations
sourceimpl<T: 'static + ?Sized> Cosync<T>
impl<T: 'static + ?Sized> Cosync<T>
sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of tasks queued. This includes the task currently being executed. Use
is_executing to see if there is a task currently being executed (ie, it returned Pending
at some point in its execution).
sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if no futures are being executed and there are no futures in the queue.
sourcepub fn is_executing(&self) -> bool
pub fn is_executing(&self) -> bool
Returns true if cosync
has a Pending
future. It is possible for
the cosync
to have no Pending
future, but to have tasks queued still.
sourcepub fn create_queue_handle(&self) -> CosyncQueueHandle<T>
pub fn create_queue_handle(&self) -> CosyncQueueHandle<T>
Creates a queue handle which can be used to spawn tasks.
sourcepub fn queue<Task, Out>(&mut self, task: Task) where
Task: FnOnce(CosyncInput<T>) -> Out + Send + 'static,
Out: Future<Output = ()> + Send,
pub fn queue<Task, Out>(&mut self, task: Task) where
Task: FnOnce(CosyncInput<T>) -> Out + Send + 'static,
Out: Future<Output = ()> + Send,
Adds a new Task to the TaskQueue.
sourcepub fn run_blocking(&mut self, parameter: &mut T)
pub fn run_blocking(&mut self, parameter: &mut T)
Run all tasks in the queue to completion. You probably want run_until_stall
.
let mut cosync: Cosync<i32> = Cosync::new();
cosync.queue(move |mut input| async move {
let mut input = input.get();
*input = 10;
});
let mut value = 0;
cosync.run_blocking(&mut value);
assert_eq!(value, 10);
The function will block the calling thread until all tasks in the pool are complete, including any spawned while running existing tasks.
sourcepub fn run_until_stall(&mut self, parameter: &mut T)
pub fn run_until_stall(&mut self, parameter: &mut T)
Runs all tasks in the queue and returns if no more progress can be made on any task.
use cosync::{sleep_ticks, Cosync};
let mut cosync = Cosync::new();
cosync.queue(move |mut input| async move {
*input.get() = 10;
// this will make the executor stall for a call
// we call `run_until_stall` an additional time,
// so we'll complete this 1 tick sleep.
sleep_ticks(1).await;
*input.get() = 20;
});
let mut value = 0;
cosync.run_until_stall(&mut value);
assert_eq!(value, 10);
cosync.run_until_stall(&mut value);
assert_eq!(value, 20);
This function will not block the calling thread and will return the moment that there are no tasks left for which progress can be made; remaining incomplete tasks in the pool can continue with further use of one of the pool’s run or poll methods. While the function is running, all tasks in the pool will try to make progress.
Trait Implementations
Auto Trait Implementations
impl<T> !RefUnwindSafe for Cosync<T>
impl<T> !Send for Cosync<T>
impl<T> !Sync for Cosync<T>
impl<T: ?Sized> Unpin for Cosync<T>
impl<T> !UnwindSafe for Cosync<T>
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more