pub struct TimerHandleWithCompletion { /* private fields */ }Expand description
Timer handle with completion receiver for managing timer lifecycle
Note: This type does not implement Clone to prevent duplicate cancellation of the same timer. Each timer should have only one owner.
包含完成通知接收器的定时器句柄,用于管理定时器生命周期
注意:此类型未实现 Clone 以防止重复取消同一定时器。每个定时器应该只有一个所有者。
Implementations§
Source§impl TimerHandleWithCompletion
impl TimerHandleWithCompletion
Sourcepub fn cancel(&self) -> bool
pub fn cancel(&self) -> bool
Cancel the timer
§Returns
Returns true if task exists and is successfully cancelled, otherwise false
取消定时器
§返回值
如果任务存在且成功取消则返回 true,否则返回 false
§Examples (示例)
let timer = TimerWheel::with_defaults();
let callback = Some(CallbackWrapper::new(|| async {}));
let task = TimerTask::new_oneshot(Duration::from_secs(1), callback);
let allocated_handle = timer.allocate_handle();
let handle = timer.register(allocated_handle, task);
// Cancel the timer
let success = handle.cancel();
println!("Canceled successfully: {}", success);Sourcepub fn postpone(
&self,
new_delay: Duration,
callback: Option<CallbackWrapper>,
) -> bool
pub fn postpone( &self, new_delay: Duration, callback: Option<CallbackWrapper>, ) -> bool
Postpone the timer
§Parameters
new_delay: New delay duration, recalculated from current timecallback: New callback function, passNoneto keep original callback, passSometo replace with new callback
§Returns
Returns true if task exists and is successfully postponed, otherwise false
推迟定时器
§参数
new_delay: 新的延迟时间,从当前时间重新计算callback: 新的回调函数,传递None保持原始回调,传递Some替换为新的回调
§返回值
如果任务存在且成功推迟则返回 true,否则返回 false
§Examples (示例)
let timer = TimerWheel::with_defaults();
let callback = Some(CallbackWrapper::new(|| async {}));
let task = TimerTask::new_oneshot(Duration::from_secs(1), callback);
let allocated_handle = timer.allocate_handle();
let handle = timer.register(allocated_handle, task);
// Postpone to 5 seconds
let success = handle.postpone(Duration::from_secs(5), None);
println!("Postponed successfully: {}", success);Sourcepub fn into_parts(self) -> (CompletionReceiver, TimerHandle)
pub fn into_parts(self) -> (CompletionReceiver, TimerHandle)
Split handle into completion receiver and timer handle
将句柄拆分为完成通知接收器和定时器句柄
§Examples (示例)
let timer = TimerWheel::with_defaults();
let callback = Some(CallbackWrapper::new(|| async {
println!("Timer fired!");
}));
let task = TimerTask::new_oneshot(Duration::from_secs(1), callback);
let allocated_handle = timer.allocate_handle();
let handle = timer.register(allocated_handle, task);
// Split into receiver and handle
// 拆分为接收器和句柄
let (rx, handle) = handle.into_parts();
// Wait for timer completion
// 等待定时器完成
use kestrel_timer::CompletionReceiver;
match rx {
CompletionReceiver::OneShot(receiver) => {
receiver.recv().await.unwrap();
},
_ => {}
}
println!("Timer completed!");Auto Trait Implementations§
impl !RefUnwindSafe for TimerHandleWithCompletion
impl !UnwindSafe for TimerHandleWithCompletion
impl Freeze for TimerHandleWithCompletion
impl Send for TimerHandleWithCompletion
impl Sync for TimerHandleWithCompletion
impl Unpin for TimerHandleWithCompletion
impl UnsafeUnpin for TimerHandleWithCompletion
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