Trait Message

Source
pub trait Message:
    Send
    + Sync
    + 'static { }
Expand description

A buffered message for pull-based event handling.

Messages can be written with MessageWriter and read using the MessageReader system parameter. Messages are stored in the Messages<M> resource, and require periodically polling the world for new messages, typically in a system that runs as part of a schedule.

While the polling imposes a small overhead, messages are useful for efficiently batch processing a large number of messages at once. For cases like these, messages can be more efficient than Events (which are handled via Observers).

Unlike Events triggered for observers, messages are evaluated at fixed points in the schedule rather than immediately when they are sent. This allows for more predictable scheduling, and deferring message processing to a later point in time.

Messages must be thread-safe.

§Usage

The Message trait can be derived:

#[derive(Message)]
struct Greeting(String);

The message can then be written to the message buffer using a MessageWriter:

fn write_hello(mut writer: MessageWriter<Greeting>) {
    writer.write(Greeting("Hello!".to_string()));
}

Messages can be efficiently read using a MessageReader:

fn read_messages(mut reader: MessageReader<Greeting>) {
    // Process all messages of type `Greeting`.
    for Greeting(greeting) in reader.read() {
        println!("{greeting}");
    }
}

Implementors§

Source§

impl Message for GamepadEvent
where GamepadEvent: Send + Sync + 'static,

Source§

impl Message for GamepadRumbleRequest
where GamepadRumbleRequest: Send + Sync + 'static,

Source§

impl Message for RawGamepadEvent
where RawGamepadEvent: Send + Sync + 'static,

Source§

impl Message for AppExit
where AppExit: Send + Sync + 'static,

Source§

impl Message for GamepadAxisChangedEvent
where GamepadAxisChangedEvent: Send + Sync + 'static,

Source§

impl Message for GamepadButtonChangedEvent
where GamepadButtonChangedEvent: Send + Sync + 'static,

Source§

impl Message for GamepadButtonStateChangedEvent

Source§

impl Message for GamepadConnectionEvent
where GamepadConnectionEvent: Send + Sync + 'static,

Source§

impl Message for RawGamepadAxisChangedEvent

Source§

impl Message for RawGamepadButtonChangedEvent

Source§

impl Message for DoubleTapGesture
where DoubleTapGesture: Send + Sync + 'static,

Source§

impl Message for PanGesture
where PanGesture: Send + Sync + 'static,

Source§

impl Message for PinchGesture
where PinchGesture: Send + Sync + 'static,

Source§

impl Message for RotationGesture
where RotationGesture: Send + Sync + 'static,

Source§

impl Message for KeyboardFocusLost
where KeyboardFocusLost: Send + Sync + 'static,

Source§

impl Message for KeyboardInput
where KeyboardInput: Send + Sync + 'static,

Source§

impl Message for MouseButtonInput
where MouseButtonInput: Send + Sync + 'static,

Source§

impl Message for MouseMotion
where MouseMotion: Send + Sync + 'static,

Source§

impl Message for MouseWheel
where MouseWheel: Send + Sync + 'static,

Source§

impl Message for TouchInput
where TouchInput: Send + Sync + 'static,

Source§

impl Message for RemovedComponentEntity
where RemovedComponentEntity: Send + Sync + 'static,