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
impl IntervalHandle
Sourcepub fn clear(self)
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§
Auto Trait Implementations§
impl !RefUnwindSafe for IntervalHandle
impl !UnwindSafe for IntervalHandle
impl Freeze for IntervalHandle
impl Send for IntervalHandle
impl Sync for IntervalHandle
impl Unpin for IntervalHandle
impl UnsafeUnpin for IntervalHandle
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more