Skip to main content

Periodic

Struct Periodic 

Source
pub struct Periodic<H, C: TimerConfig = BoxedTimers, Store: SlabStore<Item = WheelEntry<C::Storage>> = UnboundedSlab<WheelEntry<Box<dyn Handler<Instant>>>>> { /* private fields */ }
Expand description

Periodic timer wrapper — automatically reschedules after each firing.

Generic over the concrete handler type H — no nesting, no type erasure overhead. When stored in a wheel, the Periodic<H, C, Store> is wrapped in C::Storage (e.g. Box<dyn Handler<Instant>>) once at the outermost level. The inner handler H is stored directly, not wrapped.

This means Periodic<H> is size_of::<H>() + size_of::<Duration>() plus a small marker — compact enough to fit in inline storage (FlatVirtual) alongside typical handlers.

§Scheduling

schedule_forget is used for rescheduling. On bounded wheels, this panics if the slab is at capacity. This is a capacity planning error — size your wheel for peak concurrent timers including periodic overhead. See the store module documentation for the OOM-as-panic rationale.

§Cancellation

If the periodic timer is cancelled (via cancel) or dropped during shutdown, the inner handler is dropped normally — no leak.

§Example

use std::time::{Duration, Instant};
use nexus_rt::{IntoHandler, ResMut};
use nexus_rt::timer::{Periodic, TimerWheel};

fn heartbeat(mut counter: ResMut<u64>, _now: Instant) {
    *counter += 1;
}

let handler = heartbeat.into_handler(world.registry());
let periodic = Periodic::new(handler, Duration::from_millis(100));
world.resource_mut::<TimerWheel>()
    .schedule_forget(Instant::now(), Box::new(periodic));

Implementations§

Source§

impl<H, C: TimerConfig, Store: SlabStore<Item = WheelEntry<C::Storage>>> Periodic<H, C, Store>

Source

pub fn new(handler: H, interval: Duration) -> Self

Create a periodic wrapper around a handler.

C and Store determine how the handler is stored in the wheel and which wheel resource to look up on reschedule. Defaults are BoxedTimers + UnboundedSlab — override via type annotation or turbofish for inline/bounded configurations.

§Example
// Boxed + unbounded (defaults)
let p = Periodic::new(handler, Duration::from_millis(100));

// Inline + unbounded (Store must be specified when C changes)
use nexus_timer::store::UnboundedSlab;
let p: Periodic<_, InlineTimers, UnboundedSlab<_>> =
    Periodic::new(handler, Duration::from_millis(100));
Source

pub fn interval(&self) -> Duration

Returns the repetition interval.

Source

pub fn into_inner(self) -> Option<H>

Unwrap the inner handler, if present.

Returns None only during the transient state inside Handler::run (after fire, before reschedule).

Trait Implementations§

Source§

impl<H, C: TimerConfig, Store: SlabStore<Item = WheelEntry<C::Storage>>> Debug for Periodic<H, C, Store>

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<H, C, Store> Handler<Instant> for Periodic<H, C, Store>
where H: Handler<Instant> + 'static, C: TimerConfig, Store: SlabStore<Item = WheelEntry<C::Storage>> + 'static,

Source§

fn run(&mut self, world: &mut World, now: Instant)

Run this handler with the given event.
Source§

fn name(&self) -> &'static str

Returns the handler’s name. Read more

Auto Trait Implementations§

§

impl<H, C, Store> Freeze for Periodic<H, C, Store>
where H: Freeze,

§

impl<H, C, Store> RefUnwindSafe for Periodic<H, C, Store>
where H: RefUnwindSafe,

§

impl<H, C, Store> Send for Periodic<H, C, Store>
where H: Send,

§

impl<H, C, Store> Sync for Periodic<H, C, Store>
where H: Sync,

§

impl<H, C, Store> Unpin for Periodic<H, C, Store>
where H: Unpin,

§

impl<H, C, Store> UnsafeUnpin for Periodic<H, C, Store>
where H: UnsafeUnpin,

§

impl<H, C, Store> UnwindSafe for Periodic<H, C, Store>
where H: UnwindSafe,

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<E, H> IntoHandler<E, Resolved> for H
where H: Handler<E> + 'static,

Source§

type Handler = H

The concrete handler type produced.
Source§

fn into_handler(self, _registry: &Registry) -> H

Convert this function into a handler, resolving parameters from the registry.
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.