Skip to main content

Message

Trait Message 

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.

A MessageReader system parameter tracks the consumption of these events on a per-system basis using a Local<MessageCursor>, which will guarantee each system an opportunity to read the event once.

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}");
    }
}

Implementations on Foreign Types§

Source§

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

Source§

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

Source§

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

Source§

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

Source§

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

Source§

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

Source§

impl Message for CreativeInventoryActionMessage

Source§

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

Source§

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

Source§

impl<T> Message for CommandResultMessage<T>
where T: Command + Send + Sync + 'static, CommandResultMessage<T>: Send + Sync + 'static,

Implementors§

§

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

Source§

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

Source§

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

Source§

impl Message for LoadEntityForClientMessage

Source§

impl Message for UnloadEntityForClientMessage

Source§

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

§

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

Source§

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

Source§

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

Source§

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

Source§

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

Source§

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

Source§

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

Source§

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

Source§

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

Source§

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

Source§

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

Source§

impl Message for StatusEffectRemovedMessage

Source§

impl Message for AdvancementTabChangeMessage

Source§

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

Source§

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

Source§

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

Source§

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

Source§

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

Source§

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