pub trait TimerManager: Send + Sync {
// Required methods
fn set_timer<'life0, 'async_trait>(
&'life0 self,
delay: Duration,
mode: TimerMode,
callback: TimerCallback,
) -> Pin<Box<dyn Future<Output = Result<TimerId, HostApiError>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait;
fn cancel_timer<'life0, 'async_trait>(
&'life0 self,
id: TimerId,
) -> Pin<Box<dyn Future<Output = Result<(), HostApiError>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait;
fn cancel_all<'life0, 'async_trait>(
&'life0 self,
) -> Pin<Box<dyn Future<Output = Result<(), HostApiError>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait;
}Expand description
定时器管理器 trait,定义平台无关的定时调度能力契约。 支持一次性定时和重复定时的创建与取消。 内部基于 tokio::sync::Notify(条件变量机制)实现触发。
Required Methods§
Sourcefn set_timer<'life0, 'async_trait>(
&'life0 self,
delay: Duration,
mode: TimerMode,
callback: TimerCallback,
) -> Pin<Box<dyn Future<Output = Result<TimerId, HostApiError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn set_timer<'life0, 'async_trait>(
&'life0 self,
delay: Duration,
mode: TimerMode,
callback: TimerCallback,
) -> Pin<Box<dyn Future<Output = Result<TimerId, HostApiError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
创建一个定时器。
参数:
- delay: 延迟时长(OneShot 为触发延迟,Interval 为重复间隔)
- mode: 定时模式(一次性或重复)
- callback: 触发时调用的回调函数,接收 TimerId 作为参数
返回:TimerId,可用于后续取消定时器。
Sourcefn cancel_timer<'life0, 'async_trait>(
&'life0 self,
id: TimerId,
) -> Pin<Box<dyn Future<Output = Result<(), HostApiError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn cancel_timer<'life0, 'async_trait>(
&'life0 self,
id: TimerId,
) -> Pin<Box<dyn Future<Output = Result<(), HostApiError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
取消指定 ID 的定时器。
参数:id - 要取消的定时器 ID。 返回:成功返回 Ok(()),定时器不存在时也返回 Ok(())。
Sourcefn cancel_all<'life0, 'async_trait>(
&'life0 self,
) -> Pin<Box<dyn Future<Output = Result<(), HostApiError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn cancel_all<'life0, 'async_trait>(
&'life0 self,
) -> Pin<Box<dyn Future<Output = Result<(), HostApiError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
取消所有定时器。
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".