pub struct TimerHandle { /* private fields */ }Expand description
定时器句柄,用于管理定时器的生命周期 (Timer handle for managing timer lifecycle)
注意:此类型不实现 Clone,以防止重复取消同一个定时器。每个定时器只应有一个所有者。 Note: This type does not implement Clone to prevent duplicate cancellation of the same timer. Each timer should have only one owner.
Implementations§
Source§impl TimerHandle
impl TimerHandle
Sourcepub fn cancel(&self) -> bool
pub fn cancel(&self) -> bool
取消定时器 (Cancel the timer)
§返回 (Returns)
如果任务存在且成功取消返回 true,否则返回 false (Returns true if task exists and is successfully cancelled, otherwise false)
§示例 (Examples)
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!("Canceled successfully: {}", success);Sourcepub fn completion_receiver(&mut self) -> &mut CompletionReceiver
pub fn completion_receiver(&mut self) -> &mut CompletionReceiver
获取完成通知接收器的可变引用 (Get mutable reference to completion receiver)
§示例 (Examples)
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 消耗句柄)
// (Wait for timer completion (consume handle using 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
消耗句柄,返回完成通知接收器 (Consume handle and return completion receiver)
§示例 (Examples)
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);
// 等待定时器完成
// (Wait for timer completion)
handle.into_completion_receiver().0.await.ok();
println!("Timer completed!");Auto Trait Implementations§
impl Freeze for TimerHandle
impl !RefUnwindSafe for TimerHandle
impl Send for TimerHandle
impl Sync for TimerHandle
impl Unpin for TimerHandle
impl !UnwindSafe 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