[−][src]Struct glommio::timer::TimerActionOnce
The TimerActionOnce struct provides an ergonomic way to fire an action at a later point in time.
In practice Timer
is hard to use because it will always block the
current task queue. This is rarely what one wants.
The TimerActionOnce creates a timer in the background and executes an action when the timer expires. It also provides a convenient way to cancel a timer.
Implementations
impl<T: 'static> TimerActionOnce<T>
[src]
pub fn do_in(
when: Duration,
action: impl Future<Output = T> + 'static
) -> TimerActionOnce<T>
[src]
when: Duration,
action: impl Future<Output = T> + 'static
) -> TimerActionOnce<T>
Creates a TimerActionOnce
that will execute the associated future once after some
time is passed
Arguments
when
aDuration
that represents when to execute the action.action
a Future to be executed afterwhen
is elapsed.
Examples
use glommio::LocalExecutorBuilder; use glommio::timer::TimerActionOnce; use std::time::Duration; let handle = LocalExecutorBuilder::new().spawn(|| async move { let action = TimerActionOnce::do_in(Duration::from_millis(100), async move { println!("Executed once"); }); action.join().await; }).unwrap(); handle.join().unwrap();
pub fn do_in_into(
when: Duration,
action: impl Future<Output = T> + 'static,
tq: TaskQueueHandle
) -> Result<TimerActionOnce<T>, QueueNotFoundError>
[src]
when: Duration,
action: impl Future<Output = T> + 'static,
tq: TaskQueueHandle
) -> Result<TimerActionOnce<T>, QueueNotFoundError>
Creates a TimerActionOnce
that will execute the associated future once after some
time is passed in a specific Task Queue
Arguments
when
aDuration
that represents when to execute the action.action
a Future to be executed afterwhen
is elapsed.tq
theTaskQueueHandle
for the TaskQueue we want.
Examples
use glommio::{LocalExecutorBuilder, Local, Latency, Shares}; use glommio::timer::TimerActionOnce; use std::time::Duration; let handle = LocalExecutorBuilder::new().spawn(|| async move { let tq = Local::create_task_queue(Shares::default(), Latency::NotImportant, "test"); let action = TimerActionOnce::do_in_into(Duration::from_millis(100), async move { println!("Executed once"); }, tq).unwrap(); action.join().await; }).unwrap(); handle.join().unwrap();
pub fn do_at(
when: Instant,
action: impl Future<Output = T> + 'static
) -> TimerActionOnce<T>
[src]
when: Instant,
action: impl Future<Output = T> + 'static
) -> TimerActionOnce<T>
Creates a TimerActionOnce
that will execute the associated future once at a specific time
Arguments
when
anInstant
that represents when to execute the action.action
a Future to be executed at timewhen
.
Examples
use glommio::LocalExecutorBuilder; use glommio::timer::TimerActionOnce; use std::time::{Instant, Duration}; let handle = LocalExecutorBuilder::new().spawn(|| async move { let when = Instant::now().checked_add(Duration::from_millis(100)).unwrap(); let action = TimerActionOnce::do_at(when, async move { println!("Executed once"); }); action.join().await; }).unwrap(); handle.join().unwrap();
pub fn do_at_into(
when: Instant,
action: impl Future<Output = T> + 'static,
tq: TaskQueueHandle
) -> Result<TimerActionOnce<T>, QueueNotFoundError>
[src]
when: Instant,
action: impl Future<Output = T> + 'static,
tq: TaskQueueHandle
) -> Result<TimerActionOnce<T>, QueueNotFoundError>
Creates a TimerActionOnce
that will execute the associated future once at a specific time
in a specific Task Queue.
Arguments
when
anInstant
that represents when to execute the action.action
a Future to be executed at timewhen
.tq
theTaskQueueHandle
for the TaskQueue we want.
Examples
use glommio::{LocalExecutorBuilder, Local, Latency, Shares}; use glommio::timer::TimerActionOnce; use std::time::{Instant, Duration}; let handle = LocalExecutorBuilder::new().spawn(|| async move { let tq = Local::create_task_queue(Shares::default(), Latency::NotImportant, "test"); let when = Instant::now().checked_add(Duration::from_millis(100)).unwrap(); let action = TimerActionOnce::do_at_into(when, async move { println!("Executed once"); }, tq).unwrap(); action.join().await; }).unwrap(); handle.join().unwrap();
pub async fn cancel(self)
[src]
Cancel an existing TimerActionOnce
and waits for it to return
If you want to cancel the timer but doesn't want to .await on it,
prefer destroy
.
Examples
use glommio::LocalExecutorBuilder; use glommio::timer::TimerActionOnce; use std::time::Duration; let handle = LocalExecutorBuilder::new().spawn(|| async move { let action = TimerActionOnce::do_in(Duration::from_millis(100), async move { println!("Will not execute this"); }); action.cancel().await; }).unwrap(); handle.join().unwrap();
pub fn destroy(&self)
[src]
Cancel an existing TimerActionOnce
, without waiting for it to return
This is a non-async version of cancel
. It will remove the timer if
it hasn't fired already and destroy the TimerActionOnce
releasing the resources
associated with it, but without blocking the current task. It is still possible
to join
the task if needed.
Examples
use glommio::LocalExecutorBuilder; use glommio::timer::TimerActionOnce; use std::time::Duration; let handle = LocalExecutorBuilder::new().spawn(|| async move { let action = TimerActionOnce::do_in(Duration::from_millis(100), async move { println!("Will not execute this"); }); action.destroy(); action.join().await; }).unwrap(); handle.join().unwrap();
pub async fn join(self) -> Option<T>
[src]
Waits for a TimerActionOnce
to return
Returns an Option
with value None if the task was canceled and Some if
the action finished successfuly
Examples
use glommio::LocalExecutorBuilder; use glommio::timer::TimerActionOnce; use std::time::Duration; let handle = LocalExecutorBuilder::new().spawn(|| async move { let action = TimerActionOnce::do_in(Duration::from_millis(100), async move { println!("Execute this in 100ms"); }); action.join().await; }).unwrap(); handle.join().unwrap();
pub fn rearm_in(&self, dur: Duration)
[src]
Rearm a TimerActionOnce
, so it fires in the specified Duration
from now
Examples
use glommio::LocalExecutorBuilder; use glommio::timer::TimerActionOnce; use std::time::Duration; let handle = LocalExecutorBuilder::new().spawn(|| async move { let action = TimerActionOnce::do_in(Duration::from_millis(100), async move { println!("hello"); }); action.rearm_in(Duration::from_millis(100)); }).unwrap(); handle.join().unwrap();
pub fn rearm_at(&self, when: Instant)
[src]
Rearm a TimerActionOnce
, so it fires at the specified Instant
Examples
use glommio::LocalExecutorBuilder; use glommio::timer::TimerActionOnce; use std::time::{Duration, Instant}; let handle = LocalExecutorBuilder::new().spawn(|| async move { let action = TimerActionOnce::do_in(Duration::from_millis(100), async move { println!("hello"); }); action.rearm_at(Instant::now()); }).unwrap(); handle.join().unwrap();
Trait Implementations
Auto Trait Implementations
impl<T> !RefUnwindSafe for TimerActionOnce<T>
impl<T> !Send for TimerActionOnce<T>
impl<T> !Sync for TimerActionOnce<T>
impl<T> Unpin for TimerActionOnce<T>
impl<T> !UnwindSafe for TimerActionOnce<T>
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> Same<T> for T
[src]
type Output = T
Should always be Self
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,