pub struct BatchHandle { /* private fields */ }Expand description
批量定时器句柄,用于管理批量调度的定时器
通过共享 Wheel 引用减少内存开销,同时提供批量操作和迭代器访问能力。
注意:此类型不实现 Clone,以防止重复取消同一批定时器。
如需访问单个定时器句柄,请使用 into_iter() 或 into_handles() 进行转换。
Implementations§
Source§impl BatchHandle
impl BatchHandle
Sourcepub fn cancel_all(self) -> usize
pub fn cancel_all(self) -> usize
批量取消所有定时器
§返回
成功取消的任务数量
§示例
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!("取消了 {} 个定时器", cancelled);Sourcepub fn into_handles(self) -> Vec<TimerHandle>
pub fn into_handles(self) -> Vec<TimerHandle>
将批量句柄转换为单个定时器句柄的 Vec
消耗 BatchHandle,为每个任务创建独立的 TimerHandle。
§示例
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);
// 转换为独立的句柄
let handles = batch.into_handles();
for handle in handles {
// 可以单独操作每个句柄
}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>>
消耗句柄,返回所有完成通知接收器
§返回
所有任务的完成通知接收器列表
§示例
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);
// 获取所有完成通知接收器
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
实现 IntoIterator,允许直接迭代 BatchHandle
impl IntoIterator for BatchHandle
实现 IntoIterator,允许直接迭代 BatchHandle
§示例
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);
// 直接迭代,每个元素都是独立的 TimerHandle
for handle in batch {
// 可以单独操作每个句柄
}Auto Trait Implementations§
impl Freeze for BatchHandle
impl !RefUnwindSafe for BatchHandle
impl Send for BatchHandle
impl Sync for BatchHandle
impl Unpin for BatchHandle
impl !UnwindSafe for BatchHandle
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more