pub struct CallbackTemplate<K: CallbackBlueprint>{ /* 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>
impl<K: CallbackBlueprint> CallbackTemplate<K>
Sourcepub fn new<F>(f: F, registry: &Registry) -> Self
pub fn new<F>(f: F, registry: &Registry) -> Self
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.
Sourcepub fn generate(&self, ctx: K::Context) -> TemplatedCallback<K>
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>
impl<K> RefUnwindSafe for CallbackTemplate<K>
impl<K> Send for CallbackTemplate<K>
impl<K> Sync for CallbackTemplate<K>
impl<K> Unpin for CallbackTemplate<K>
impl<K> UnsafeUnpin for CallbackTemplate<K>
impl<K> UnwindSafe for CallbackTemplate<K>
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