[][src]Struct glommio::timer::TimerActionOnce

pub struct TimerActionOnce<T> { /* fields omitted */ }

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]

Creates a TimerActionOnce that will execute the associated future once after some time is passed

Arguments

  • when a Duration that represents when to execute the action.
  • action a Future to be executed after when 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]

Creates a TimerActionOnce that will execute the associated future once after some time is passed in a specific Task Queue

Arguments

  • when a Duration that represents when to execute the action.
  • action a Future to be executed after when is elapsed.
  • tq the TaskQueueHandle 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]

Creates a TimerActionOnce that will execute the associated future once at a specific time

Arguments

  • when an Instant that represents when to execute the action.
  • action a Future to be executed at time when.

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]

Creates a TimerActionOnce that will execute the associated future once at a specific time in a specific Task Queue.

Arguments

  • when an Instant that represents when to execute the action.
  • action a Future to be executed at time when.
  • tq the TaskQueueHandle 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

impl<T: Debug> Debug for TimerActionOnce<T>[src]

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]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

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]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.