Struct glommio::timer::TimerActionOnce
source · pub struct TimerActionOnce<T> { /* private fields */ }
Expand description
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§
source§impl<T: 'static> TimerActionOnce<T>
impl<T: 'static> TimerActionOnce<T>
sourcepub fn do_in(
when: Duration,
action: impl Future<Output = T> + 'static
) -> TimerActionOnce<T>
pub fn do_in( 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::{timer::TimerActionOnce, LocalExecutorBuilder};
use std::time::Duration;
let handle = LocalExecutorBuilder::default()
.spawn(|| async move {
let action = TimerActionOnce::do_in(Duration::from_millis(100), async move {
println!("Executed once");
});
action.join().await;
})
.unwrap();
handle.join().unwrap();
sourcepub fn do_in_into(
when: Duration,
action: impl Future<Output = T> + 'static,
tq: TaskQueueHandle
) -> Result<TimerActionOnce<T>, ()>
pub fn do_in_into( when: Duration, action: impl Future<Output = T> + 'static, tq: TaskQueueHandle ) -> Result<TimerActionOnce<T>, ()>
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::{timer::TimerActionOnce, Latency, LocalExecutorBuilder, Shares};
use std::time::Duration;
let handle = LocalExecutorBuilder::default()
.spawn(|| async move {
let tq = glommio::executor().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();
sourcepub fn do_at(
when: Instant,
action: impl Future<Output = T> + 'static
) -> TimerActionOnce<T>
pub fn do_at( 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::{timer::TimerActionOnce, LocalExecutorBuilder};
use std::time::{Duration, Instant};
let handle = LocalExecutorBuilder::default()
.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();
sourcepub fn do_at_into(
when: Instant,
action: impl Future<Output = T> + 'static,
tq: TaskQueueHandle
) -> Result<TimerActionOnce<T>, ()>
pub fn do_at_into( when: Instant, action: impl Future<Output = T> + 'static, tq: TaskQueueHandle ) -> Result<TimerActionOnce<T>, ()>
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 task queue we want.
§Examples
use glommio::{timer::TimerActionOnce, Latency, LocalExecutorBuilder, Shares};
use std::time::{Duration, Instant};
let handle = LocalExecutorBuilder::default()
.spawn(|| async move {
let tq = glommio::executor().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();
sourcepub async fn cancel(self)
pub async fn cancel(self)
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::{timer::TimerActionOnce, LocalExecutorBuilder};
use std::time::Duration;
let handle = LocalExecutorBuilder::default()
.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();
sourcepub fn destroy(&self)
pub fn destroy(&self)
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::{timer::TimerActionOnce, LocalExecutorBuilder};
use std::time::Duration;
let handle = LocalExecutorBuilder::default()
.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();
sourcepub async fn join(self) -> Option<T>
pub async fn join(self) -> Option<T>
Waits for a TimerActionOnce
to return
Returns an Option
with value None if the task was canceled and Some
if the action finished successfully
§Examples
use glommio::{timer::TimerActionOnce, LocalExecutorBuilder};
use std::time::Duration;
let handle = LocalExecutorBuilder::default()
.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();
sourcepub fn rearm_in(&self, dur: Duration)
pub fn rearm_in(&self, dur: Duration)
Rearm a TimerActionOnce
, so it fires in the specified Duration
from now
§Examples
use glommio::{timer::TimerActionOnce, LocalExecutorBuilder};
use std::time::Duration;
let handle = LocalExecutorBuilder::default()
.spawn(|| async move {
let action = TimerActionOnce::do_in(Duration::from_millis(100), async move {
println!("hello");
});
action.rearm_in(Duration::from_millis(100));
action.join().await
})
.unwrap();
handle.join().unwrap();
sourcepub fn rearm_at(&self, when: Instant)
pub fn rearm_at(&self, when: Instant)
Rearm a TimerActionOnce
, so it fires at the specified Instant
§Examples
use glommio::{timer::TimerActionOnce, LocalExecutorBuilder};
use std::time::{Duration, Instant};
let handle = LocalExecutorBuilder::default()
.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();