Skip to main content

Broadcast

Struct Broadcast 

Source
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§

Source§

impl<E> Broadcast<E>

Source

pub fn new() -> Self

Create an empty broadcast with no handlers.

Source

pub fn add<H: for<'e> Handler<&'e E> + Send + 'static>(&mut self, handler: H)

Add a handler to the broadcast.

Source

pub fn len(&self) -> usize

Returns the number of handlers.

Source

pub fn is_empty(&self) -> bool

Returns true if there are no handlers.

Trait Implementations§

Source§

impl<E> Default for Broadcast<E>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<E> Handler<E> for Broadcast<E>

Source§

fn run(&mut self, world: &mut World, event: E)

Run this handler with the given event.
Source§

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

Returns the handler’s name. Read more

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> 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.