Skip to main content

IntervalHandle

Struct IntervalHandle 

Source
pub struct IntervalHandle { /* private fields */ }
Expand description

A handle that controls a running interval task.

When the handle is dropped (without calling clear()), the associated interval task will be cancelled on the next tick boundary.

Call clear() to synchronously wait for the interval task to shut down (by sending a shutdown signal through the channel).

§Panics

clear() will panic if the spawned interval task has already panicked (detected by a closed channel).

Implementations§

Source§

impl IntervalHandle

Source

pub fn clear(self)

Stops the interval loop.

Sends a shutdown signal to the spawned task. The loop will break on the next iteration, after any in-flight call to func completes.

§Panics

Panics with the message "Interval was panic" if the spawned task has already panicked (i.e., the oneshot receiver was dropped without receiving the shutdown signal).

§Example
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::Mutex;
use tokio::time::interval;
use tokio_interval_set::set_interval;

let rt = tokio::runtime::Builder::new_current_thread()
    .enable_time()
    .build()
    .unwrap();

rt.block_on(async {
    let count = Arc::new(Mutex::new(0u64));
    let count_clone = Arc::clone(&count);
    let handle = set_interval(
        interval(Duration::from_secs(1)),
        move || {
            let count = Arc::clone(&count_clone);
            async move {
                let mut c = count.lock().await;
                *c += 1;
            }
        },
    );

    tokio::time::sleep(Duration::from_secs(3)).await;
    handle.clear(); // ✅ clean shutdown
    assert_eq!(*count.lock().await, 3);
});

Trait Implementations§

Source§

impl Debug for IntervalHandle

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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, 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

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

Source§

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.