pub struct BatchHandleWithCompletion { /* private fields */ }Expand description
Batch timer handle with completion receivers 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 BatchHandleWithCompletion
impl BatchHandleWithCompletion
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 handles = timer.allocate_handles(10);
let tasks: Vec<_> = (0..10)
.map(|_| TimerTask::new_oneshot(Duration::from_secs(1), None))
.collect();
let batch = timer.register_batch(handles, tasks).unwrap();
let cancelled = batch.cancel_all();
println!("Canceled {} timers", cancelled);Sourcepub fn into_handles(self) -> Vec<TimerHandleWithCompletion>
pub fn into_handles(self) -> Vec<TimerHandleWithCompletion>
Convert batch handle to Vec of individual timer handles with completion receivers
Consumes BatchHandleWithCompletion and creates independent TimerHandleWithCompletion for each task
将批量句柄转换为单个定时器句柄的 Vec
消费 BatchHandleWithCompletion 并为每个任务创建独立的 TimerHandleWithCompletion
§Examples (示例)
let timer = TimerWheel::with_defaults();
let handles = timer.allocate_handles(3);
let tasks: Vec<_> = (0..3)
.map(|_| TimerTask::new_oneshot(Duration::from_secs(1), None))
.collect();
let batch = timer.register_batch(handles, tasks).unwrap();
// Convert to individual handles
// 转换为单个句柄
let handles = batch.into_handles();
for handle in handles {
// Can operate each handle individually
// 可以单独操作每个句柄
}Sourcepub fn into_parts(self) -> (Vec<CompletionReceiver>, BatchHandle)
pub fn into_parts(self) -> (Vec<CompletionReceiver>, BatchHandle)
Split batch handle into completion receivers and batch handle
将批量句柄拆分为完成通知接收器列表和批量句柄
§Examples (示例)
let timer = TimerWheel::with_defaults();
let handles = timer.allocate_handles(3);
let tasks: Vec<_> = (0..3)
.map(|_| TimerTask::new_oneshot(Duration::from_secs(1), None))
.collect();
let batch = timer.register_batch(handles, tasks).unwrap();
// Split into receivers and handle
// 拆分为接收器和句柄
use kestrel_timer::CompletionReceiver;
let (receivers, batch_handle) = batch.into_parts();
for rx in receivers {
tokio::spawn(async move {
match rx {
CompletionReceiver::OneShot(receiver) => {
receiver.wait().await;
println!("A timer completed!");
},
_ => {}
}
});
}Sourcepub fn postpone_all(self, new_delay: Duration) -> usize
pub fn postpone_all(self, new_delay: Duration) -> usize
Batch postpone timers (keep original callbacks)
§Parameters
new_delay: New delay duration applied to all timers
§Returns
Number of successfully postponed tasks
批量推迟定时器 (保持原始回调)
§参数
new_delay: 应用于所有定时器的新延迟时间
§返回值
成功推迟的任务数量
§Examples (示例)
let timer = TimerWheel::with_defaults();
let handles = timer.allocate_handles(10);
let tasks: Vec<_> = (0..10)
.map(|_| TimerTask::new_oneshot(Duration::from_secs(1), None))
.collect();
let batch = timer.register_batch(handles, tasks).unwrap();
// Postpone all timers to 5 seconds
let postponed = batch.postpone_all(Duration::from_secs(5));
println!("Postponed {} timers", postponed);Sourcepub fn postpone_each(self, delays: Vec<Duration>) -> usize
pub fn postpone_each(self, delays: Vec<Duration>) -> usize
Batch postpone timers with individual delays (keep original callbacks)
§Parameters
delays: List of new delay durations for each timer (must match the number of tasks)
§Returns
Number of successfully postponed tasks
批量推迟定时器,每个定时器使用不同延迟 (保持原始回调)
§参数
delays: 每个定时器的新延迟时间列表(必须与任务数量匹配)
§返回值
成功推迟的任务数量
§Examples (示例)
let timer = TimerWheel::with_defaults();
let handles = timer.allocate_handles(3);
let tasks: Vec<_> = (0..3)
.map(|_| TimerTask::new_oneshot(Duration::from_secs(1), None))
.collect();
let batch = timer.register_batch(handles, tasks).unwrap();
// Postpone each timer with different delays
let new_delays = vec![
Duration::from_secs(2),
Duration::from_secs(3),
Duration::from_secs(4),
];
let postponed = batch.postpone_each(new_delays);
println!("Postponed {} timers", postponed);Sourcepub fn postpone_each_with_callbacks(
self,
updates: Vec<(Duration, Option<CallbackWrapper>)>,
) -> usize
pub fn postpone_each_with_callbacks( self, updates: Vec<(Duration, Option<CallbackWrapper>)>, ) -> usize
Batch postpone timers with individual delays and callbacks
§Parameters
updates: List of tuples of (new delay, new callback) for each timer
§Returns
Number of successfully postponed tasks
批量推迟定时器,每个定时器使用不同延迟和回调
§参数
updates: 每个定时器的 (新延迟, 新回调) 元组列表
§返回值
成功推迟的任务数量
§Examples (示例)
let timer = TimerWheel::with_defaults();
let handles = timer.allocate_handles(3);
let tasks: Vec<_> = (0..3)
.map(|_| TimerTask::new_oneshot(Duration::from_secs(1), None))
.collect();
let batch = timer.register_batch(handles, tasks).unwrap();
// Postpone each timer with different delays and callbacks
let updates = vec![
(Duration::from_secs(2), Some(CallbackWrapper::new(|| async {}))),
(Duration::from_secs(3), None),
(Duration::from_secs(4), Some(CallbackWrapper::new(|| async {}))),
];
let postponed = batch.postpone_each_with_callbacks(updates);
println!("Postponed {} timers", postponed);Trait Implementations§
Source§impl IntoIterator for BatchHandleWithCompletion
Implement IntoIterator to allow direct iteration over BatchHandleWithCompletion
impl IntoIterator for BatchHandleWithCompletion
Implement IntoIterator to allow direct iteration over BatchHandleWithCompletion
实现 IntoIterator 以允许直接迭代 BatchHandleWithCompletion
§Examples (示例)
let timer = TimerWheel::with_defaults();
let handles = timer.allocate_handles(3);
let tasks: Vec<_> = (0..3)
.map(|_| TimerTask::new_oneshot(Duration::from_secs(1), None))
.collect();
let batch = timer.register_batch(handles, tasks).unwrap();
// Iterate directly, each element is an independent TimerHandleWithCompletion
// 直接迭代,每个元素是一个独立的 TimerHandleWithCompletion
for handle in batch {
// Can operate each handle individually
// 可以单独操作每个句柄
}