TimerHandleWithCompletion

Struct TimerHandleWithCompletion 

Source
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

Source

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);
Source

pub fn postpone( &self, new_delay: Duration, callback: Option<CallbackWrapper>, ) -> bool

Postpone the timer

§Parameters
  • new_delay: New delay duration, recalculated from current time
  • callback: New callback function, pass None to keep original callback, pass Some to 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);
Source

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§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.