pub struct BatchHandle { /* private fields */ }Expand description
Batch timer handle for managing batch-scheduled timers
Note: This type does not implement Clone to prevent duplicate cancellation of the same batch of timers. Use into_iter() or into_handles() to access individual timer handles.
批量定时器句柄,用于管理批量调度的定时器
注意:此类型未实现 Clone 以防止重复取消同一批定时器。使用 into_iter() 或 into_handles() 访问单个定时器句柄。
Implementations§
Source§impl BatchHandle
impl BatchHandle
Sourcepub fn cancel_all(self) -> usize
pub fn cancel_all(self) -> usize
Cancel all timers in batch
§Returns
Number of successfully cancelled tasks
批量取消所有定时器
§返回值
成功取消的任务数量
§Examples (示例)
let timer = TimerWheel::with_defaults();
let delays: Vec<Duration> = (0..10)
.map(|_| Duration::from_secs(1))
.collect();
let tasks = TimerWheel::create_batch(delays);
let batch = timer.register_batch(tasks);
let cancelled = batch.cancel_all();
println!("Canceled {} timers", cancelled);Sourcepub fn into_handles(self) -> Vec<TimerHandle>
pub fn into_handles(self) -> Vec<TimerHandle>
Convert batch handle to Vec of individual timer handles
Consumes BatchHandle and creates independent TimerHandle for each task
将批量句柄转换为单个定时器句柄的 Vec
消费 BatchHandle 并为每个任务创建独立的 TimerHandle
§Examples (示例)
let timer = TimerWheel::with_defaults();
let delays: Vec<Duration> = (0..3)
.map(|_| Duration::from_secs(1))
.collect();
let tasks = TimerWheel::create_batch(delays);
let batch = timer.register_batch(tasks);
// Convert to individual handles
// 转换为单个句柄
let handles = batch.into_handles();
for handle in handles {
// Can operate each handle individually
// 可以单独操作每个句柄
}Sourcepub fn completion_receivers(
&mut self,
) -> &mut Vec<Receiver<TaskCompletionReason>>
pub fn completion_receivers( &mut self, ) -> &mut Vec<Receiver<TaskCompletionReason>>
Sourcepub fn into_completion_receivers(self) -> Vec<Receiver<TaskCompletionReason>>
pub fn into_completion_receivers(self) -> Vec<Receiver<TaskCompletionReason>>
Consume handle and return all completion receivers
§Returns
List of completion receivers for all tasks
消费句柄并返回所有完成通知接收器
§返回值
所有任务的完成通知接收器列表
§Examples (示例)
let timer = TimerWheel::with_defaults();
let delays: Vec<Duration> = (0..3)
.map(|_| Duration::from_secs(1))
.collect();
let tasks = TimerWheel::create_batch(delays);
let batch = timer.register_batch(tasks);
// Get all completion receivers
// 获取所有完成通知接收器
let receivers = batch.into_completion_receivers();
for rx in receivers {
tokio::spawn(async move {
if rx.await.is_ok() {
println!("A timer completed!");
}
});
}Trait Implementations§
Source§impl IntoIterator for BatchHandle
Implement IntoIterator to allow direct iteration over BatchHandle
impl IntoIterator for BatchHandle
Implement IntoIterator to allow direct iteration over BatchHandle
实现 IntoIterator 以允许直接迭代 BatchHandle
§Examples (示例)
let timer = TimerWheel::with_defaults();
let delays: Vec<Duration> = (0..3)
.map(|_| Duration::from_secs(1))
.collect();
let tasks = TimerWheel::create_batch(delays);
let batch = timer.register_batch(tasks);
// Iterate directly, each element is an independent TimerHandle
// 直接迭代,每个元素是一个独立的 TimerHandle
for handle in batch {
// Can operate each handle individually
// 可以单独操作每个句柄
}