TimerHandle

Struct TimerHandle 

Source
pub struct TimerHandle { /* private fields */ }
Expand description

定时器句柄,用于管理定时器的生命周期

注意:此类型不实现 Clone,以防止重复取消同一个定时器。 每个定时器只应有一个所有者。

Implementations§

Source§

impl TimerHandle

Source

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

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

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§

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.