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>

source

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

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 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::{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();
source

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 an Instant that represents when to execute the action.
  • action a Future to be executed at time when.
§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();
source

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

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();
source

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();
source

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();
source

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();
source

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

Trait Implementations§

source§

impl<T: Debug> Debug for TimerActionOnce<T>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T> Freeze for TimerActionOnce<T>

§

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§

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> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> Pointable for T

source§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

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>,

§

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.
source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more