pub struct TimerHandle { /* private fields */ }Expand description
定时器句柄,用于管理定时器的生命周期
注意:此类型不实现 Clone,以防止重复取消同一个定时器。 每个定时器只应有一个所有者。
Implementations§
Source§impl TimerHandle
impl TimerHandle
Sourcepub fn cancel(&self) -> bool
pub fn cancel(&self) -> bool
取消定时器
§返回
如果任务存在且成功取消返回 true,否则返回 false
§示例
let timer = TimerWheel::with_defaults();
let callback = Some(CallbackWrapper::new(|| async {}));
let task = TimerWheel::create_task(Duration::from_secs(1), callback);
let handle = timer.register(task);
// 取消定时器
let success = handle.cancel();
println!("取消成功: {}", success);Sourcepub fn completion_receiver(&mut self) -> &mut CompletionReceiver
pub fn completion_receiver(&mut self) -> &mut CompletionReceiver
获取完成通知接收器的可变引用
§示例
let timer = TimerWheel::with_defaults();
let callback = Some(CallbackWrapper::new(|| async {
println!("Timer fired!");
}));
let task = TimerWheel::create_task(Duration::from_secs(1), callback);
let handle = timer.register(task);
// 等待定时器完成(使用 into_completion_receiver 消耗句柄)
handle.into_completion_receiver().0.await.ok();
println!("Timer completed!");Sourcepub fn into_completion_receiver(self) -> CompletionReceiver
pub fn into_completion_receiver(self) -> CompletionReceiver
消耗句柄,返回完成通知接收器
§示例
let timer = TimerWheel::with_defaults();
let callback = Some(CallbackWrapper::new(|| async {
println!("Timer fired!");
}));
let task = TimerWheel::create_task(Duration::from_secs(1), callback);
let handle = timer.register(task);
// 等待定时器完成
handle.into_completion_receiver().0.await.ok();
println!("Timer completed!");Auto Trait Implementations§
impl !RefUnwindSafe for TimerHandle
impl !UnwindSafe for TimerHandle
impl Freeze for TimerHandle
impl Send for TimerHandle
impl Sync for TimerHandle
impl Unpin for TimerHandle
impl UnsafeUnpin for TimerHandle
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