use crate::task::{CompletionReceiver, TaskId};
use crate::wheel::Wheel;
use parking_lot::Mutex;
use std::sync::Arc;
pub struct TimerHandle {
pub(crate) task_id: TaskId,
pub(crate) wheel: Arc<Mutex<Wheel>>,
}
impl TimerHandle {
#[inline]
pub(crate) fn new(task_id: TaskId, wheel: Arc<Mutex<Wheel>>) -> Self {
Self { task_id, wheel }
}
#[inline]
pub fn cancel(&self) -> bool {
let mut wheel = self.wheel.lock();
wheel.cancel(self.task_id)
}
#[inline]
pub fn postpone(
&self,
new_delay: std::time::Duration,
callback: Option<crate::task::CallbackWrapper>,
) -> bool {
let mut wheel = self.wheel.lock();
wheel.postpone(self.task_id, new_delay, callback)
}
}
pub struct TimerHandleWithCompletion {
handle: TimerHandle,
pub(crate) completion_rx: CompletionReceiver,
}
impl TimerHandleWithCompletion {
pub(crate) fn new(handle: TimerHandle, completion_rx: CompletionReceiver) -> Self {
Self {
handle,
completion_rx,
}
}
pub fn cancel(&self) -> bool {
self.handle.cancel()
}
pub fn postpone(
&self,
new_delay: std::time::Duration,
callback: Option<crate::task::CallbackWrapper>,
) -> bool {
self.handle.postpone(new_delay, callback)
}
pub fn into_parts(self) -> (CompletionReceiver, TimerHandle) {
(self.completion_rx, self.handle)
}
}
pub struct BatchHandle {
pub(crate) task_ids: Vec<TaskId>,
pub(crate) wheel: Arc<Mutex<Wheel>>,
}
impl BatchHandle {
#[inline]
pub(crate) fn new(task_ids: Vec<TaskId>, wheel: Arc<Mutex<Wheel>>) -> Self {
Self { task_ids, wheel }
}
#[inline]
pub fn cancel_all(self) -> usize {
let mut wheel = self.wheel.lock();
wheel.cancel_batch(&self.task_ids)
}
#[inline]
pub fn into_handles(self) -> Vec<TimerHandle> {
self.task_ids
.into_iter()
.map(|task_id| TimerHandle::new(task_id, self.wheel.clone()))
.collect()
}
#[inline]
pub fn len(&self) -> usize {
self.task_ids.len()
}
#[inline]
pub fn is_empty(&self) -> bool {
self.task_ids.is_empty()
}
#[inline]
pub fn task_ids(&self) -> &[TaskId] {
&self.task_ids
}
#[inline]
pub fn postpone_all(self, new_delay: std::time::Duration) -> usize {
let updates: Vec<_> = self.task_ids.iter().map(|&id| (id, new_delay)).collect();
let mut wheel = self.wheel.lock();
wheel.postpone_batch(updates)
}
#[inline]
pub fn postpone_each(self, delays: Vec<std::time::Duration>) -> usize {
let updates: Vec<_> = self.task_ids.into_iter().zip(delays).collect();
let mut wheel = self.wheel.lock();
wheel.postpone_batch(updates)
}
#[inline]
pub fn postpone_each_with_callbacks(
self,
updates: Vec<(std::time::Duration, Option<crate::task::CallbackWrapper>)>,
) -> usize {
let updates_with_ids: Vec<_> = self
.task_ids
.into_iter()
.zip(updates)
.map(|(id, (delay, callback))| (id, delay, callback))
.collect();
let mut wheel = self.wheel.lock();
wheel.postpone_batch_with_callbacks(updates_with_ids)
}
}
pub struct BatchHandleWithCompletion {
handles: BatchHandle,
completion_rxs: Vec<CompletionReceiver>,
}
impl BatchHandleWithCompletion {
#[inline]
pub(crate) fn new(handles: BatchHandle, completion_rxs: Vec<CompletionReceiver>) -> Self {
Self {
handles,
completion_rxs,
}
}
#[inline]
pub fn cancel_all(self) -> usize {
self.handles.cancel_all()
}
#[inline]
pub fn into_handles(self) -> Vec<TimerHandleWithCompletion> {
self.handles
.into_handles()
.into_iter()
.zip(self.completion_rxs)
.map(|(handle, rx)| TimerHandleWithCompletion::new(handle, rx))
.collect()
}
#[inline]
pub fn len(&self) -> usize {
self.handles.len()
}
#[inline]
pub fn is_empty(&self) -> bool {
self.handles.is_empty()
}
#[inline]
pub fn task_ids(&self) -> &[TaskId] {
self.handles.task_ids()
}
#[inline]
pub fn into_parts(self) -> (Vec<CompletionReceiver>, BatchHandle) {
let handle = BatchHandle::new(self.handles.task_ids.clone(), self.handles.wheel);
(self.completion_rxs, handle)
}
#[inline]
pub fn postpone_all(self, new_delay: std::time::Duration) -> usize {
self.handles.postpone_all(new_delay)
}
#[inline]
pub fn postpone_each(self, delays: Vec<std::time::Duration>) -> usize {
self.handles.postpone_each(delays)
}
#[inline]
pub fn postpone_each_with_callbacks(
self,
updates: Vec<(std::time::Duration, Option<crate::task::CallbackWrapper>)>,
) -> usize {
self.handles.postpone_each_with_callbacks(updates)
}
}
impl IntoIterator for BatchHandleWithCompletion {
type Item = TimerHandleWithCompletion;
type IntoIter = BatchHandleWithCompletionIter;
#[inline]
fn into_iter(self) -> Self::IntoIter {
BatchHandleWithCompletionIter {
task_ids: self.handles.task_ids.into_iter(),
completion_rxs: self.completion_rxs.into_iter(),
wheel: self.handles.wheel,
}
}
}
pub struct BatchHandleWithCompletionIter {
task_ids: std::vec::IntoIter<TaskId>,
completion_rxs: std::vec::IntoIter<CompletionReceiver>,
wheel: Arc<Mutex<Wheel>>,
}
impl Iterator for BatchHandleWithCompletionIter {
type Item = TimerHandleWithCompletion;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
match (self.task_ids.next(), self.completion_rxs.next()) {
(Some(task_id), Some(rx)) => Some(TimerHandleWithCompletion::new(
TimerHandle::new(task_id, self.wheel.clone()),
rx,
)),
_ => None,
}
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.task_ids.size_hint()
}
}
impl ExactSizeIterator for BatchHandleWithCompletionIter {
#[inline]
fn len(&self) -> usize {
self.task_ids.len()
}
}