Skip to main content

CallbackTemplate

Struct CallbackTemplate 

Source
pub struct CallbackTemplate<K: CallbackBlueprint>
where <K::Params as Param>::State: Copy,
{ /* private fields */ }
Expand description

Resolve-once template for context-owning handlers.

Like HandlerTemplate, but each generate call takes a K::Context to produce a TemplatedCallback with per-instance owned state.

§Examples

use nexus_rt::{WorldBuilder, ResMut, Handler};
use nexus_rt::template::{Blueprint, CallbackBlueprint, CallbackTemplate};

struct TimerCtx { order_id: u64, fires: u64 }

struct OnTimeout;
impl Blueprint for OnTimeout {
    type Event = ();
    type Params = (ResMut<'static, u64>,);
}
impl CallbackBlueprint for OnTimeout {
    type Context = TimerCtx;
}

fn on_timeout(ctx: &mut TimerCtx, mut counter: ResMut<u64>, _event: ()) {
    ctx.fires += 1;
    *counter += ctx.order_id;
}

let mut builder = WorldBuilder::new();
builder.register::<u64>(0);
let world = builder.build();

let template = CallbackTemplate::<OnTimeout>::new(on_timeout, world.registry());
let mut cb1 = template.generate(TimerCtx { order_id: 10, fires: 0 });
let mut cb2 = template.generate(TimerCtx { order_id: 20, fires: 0 });

// Each carries its own context, shares pre-resolved state.

Implementations§

Source§

impl<K: CallbackBlueprint> CallbackTemplate<K>
where <K::Params as Param>::State: Copy,

Source

pub fn new<F>(f: F, registry: &Registry) -> Self
where F: CallbackTemplateDispatch<K::Context, K::Params, K::Event>,

Create a template from a named function.

Same constraints as HandlerTemplate::new: F must be ZST, all resources must be registered.

§Panics
  • If any required resource is not registered.
  • If params have conflicting access.
Source

pub fn generate(&self, ctx: K::Context) -> TemplatedCallback<K>

Stamp out a new callback with the given context.

Auto Trait Implementations§

§

impl<K> Freeze for CallbackTemplate<K>
where <<K as Blueprint>::Params as Param>::State: Sized + Freeze,

§

impl<K> RefUnwindSafe for CallbackTemplate<K>
where <<K as Blueprint>::Params as Param>::State: Sized + RefUnwindSafe,

§

impl<K> Send for CallbackTemplate<K>
where <<K as Blueprint>::Params as Param>::State: Sized,

§

impl<K> Sync for CallbackTemplate<K>
where <<K as Blueprint>::Params as Param>::State: Sized + Sync,

§

impl<K> Unpin for CallbackTemplate<K>
where <<K as Blueprint>::Params as Param>::State: Sized + Unpin,

§

impl<K> UnsafeUnpin for CallbackTemplate<K>
where <<K as Blueprint>::Params as Param>::State: Sized + UnsafeUnpin,

§

impl<K> UnwindSafe for CallbackTemplate<K>
where <<K as Blueprint>::Params as Param>::State: Sized + 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<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.