pub struct Broadcast<E> { /* private fields */ }Expand description
Dynamic fan-out combinator. Takes ownership of an event, borrows
it, and dispatches &E to N handlers, where N is determined at
runtime.
One heap allocation per handler (boxing). Zero clones at dispatch
— each handler receives &E.
Handlers must implement for<'e> Handler<&'e E>. Use
Cloned or Owned to adapt
owned-event handlers.
For static fan-out (known handler count, zero allocation), use
FanOut.
§Examples
use nexus_rt::{WorldBuilder, ResMut, IntoHandler, Handler, Broadcast, Resource};
#[derive(Resource)]
struct Counter(u64);
fn write_a(mut sink: ResMut<Counter>, event: &u32) {
sink.0 += *event as u64;
}
let mut builder = WorldBuilder::new();
builder.register(Counter(0));
let mut world = builder.build();
let mut broadcast: Broadcast<u32> = Broadcast::new();
broadcast.add(write_a.into_handler(world.registry()));
broadcast.add(write_a.into_handler(world.registry()));
broadcast.run(&mut world, 5u32);
assert_eq!(world.resource::<Counter>().0, 10);Implementations§
Trait Implementations§
Auto Trait Implementations§
impl<E> Freeze for Broadcast<E>
impl<E> !RefUnwindSafe for Broadcast<E>
impl<E> Send for Broadcast<E>
impl<E> !Sync for Broadcast<E>
impl<E> Unpin for Broadcast<E>
impl<E> UnsafeUnpin for Broadcast<E>
impl<E> !UnwindSafe for Broadcast<E>
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