use pi_async_task::Runnable;
use pi_result::RawResult as Result;
use crate::management::QueueCleanupProgress;
use std::collections::VecDeque;
use std::sync::Arc;
pub trait ExternalTaskQueue: Send + Sync + 'static {
fn try_push(&self, task: Runnable) -> Result<(), ExternalPushError>;
fn try_take_batch(&self) -> BatchAttempt;
fn len(&self) -> usize;
}
impl<Q: ExternalTaskQueue + ?Sized> ExternalTaskQueue for Arc<Q> {
fn try_push(&self, task: Runnable) -> Result<(), ExternalPushError> {
(**self).try_push(task)
}
fn try_take_batch(&self) -> BatchAttempt {
(**self).try_take_batch()
}
fn len(&self) -> usize {
(**self).len()
}
}
pub struct ExternalPushError {
pub task: Runnable,
pub cause: ExternalPushErrorKind,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ExternalPushErrorKind {
Busy,
Closed,
Capacity,
CountOverflow,
LinkMissing,
}
pub enum BatchAttempt {
Ready(TaskBatch),
Empty,
WouldBlock,
Busy,
Closed,
}
pub struct TaskBatch {
pub(crate) tasks: VecDeque<Runnable>,
}
impl TaskBatch {
pub fn from_tasks(tasks: VecDeque<Runnable>) -> Self {
batch_from_inner(tasks)
}
}
impl TaskBatch {
pub fn len(&self) -> usize {
batch_len_inner(self)
}
}
impl TaskBatch {
pub fn is_empty(&self) -> bool {
batch_empty_inner(self)
}
}
impl IntoIterator for TaskBatch {
type Item = Runnable;
type IntoIter = std::collections::vec_deque::IntoIter<Runnable>;
fn into_iter(self) -> Self::IntoIter {
batch_into_iter_inner(self)
}
}
pub trait ExternalQueueManager {
type Handle: ExternalTaskQueue + Clone;
fn handle(&self) -> Self::Handle;
fn clear(&mut self) -> pi_result::Result<QueueCleanupProgress>;
fn close(&mut self) -> pi_result::Result<QueueCleanupProgress>;
}
include!("external_inner.rs");