mod iterators;
mod message_cursor;
mod message_mutator;
mod message_reader;
mod message_registry;
mod message_writer;
mod messages;
mod mut_iterators;
mod update;
pub use iterators::*;
pub use message_cursor::*;
pub use message_mutator::*;
pub use message_reader::*;
pub use message_registry::*;
pub use message_writer::*;
pub use messages::*;
pub use mut_iterators::*;
pub use update::*;
pub use bevy_ecs_macros::Message;
use crate::change_detection::MaybeLocation;
#[cfg(feature = "bevy_reflect")]
use bevy_reflect::Reflect;
use core::{
cmp::Ordering,
fmt,
hash::{Hash, Hasher},
marker::PhantomData,
};
#[diagnostic::on_unimplemented(
message = "`{Self}` is not an `Message`",
label = "invalid `Message`",
note = "consider annotating `{Self}` with `#[derive(Message)]`"
)]
pub trait Message: Send + Sync + 'static {}
#[derive(Debug)]
#[cfg_attr(feature = "bevy_reflect", derive(Reflect))]
pub(crate) struct MessageInstance<M: Message> {
pub message_id: MessageId<M>,
pub message: M,
}
#[cfg_attr(
feature = "bevy_reflect",
derive(Reflect),
reflect(Clone, Debug, PartialEq, Hash)
)]
pub struct MessageId<M: Message> {
pub id: usize,
pub caller: MaybeLocation,
#[cfg_attr(feature = "bevy_reflect", reflect(ignore, clone))]
pub(super) _marker: PhantomData<M>,
}
impl<M: Message> Copy for MessageId<M> {}
impl<M: Message> Clone for MessageId<M> {
fn clone(&self) -> Self {
*self
}
}
impl<M: Message> fmt::Display for MessageId<M> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
<Self as fmt::Debug>::fmt(self, f)
}
}
impl<M: Message> fmt::Debug for MessageId<M> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"message<{}>#{}",
core::any::type_name::<M>().split("::").last().unwrap(),
self.id,
)
}
}
impl<M: Message> PartialEq for MessageId<M> {
fn eq(&self, other: &Self) -> bool {
self.id == other.id
}
}
impl<M: Message> Eq for MessageId<M> {}
impl<M: Message> PartialOrd for MessageId<M> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl<M: Message> Ord for MessageId<M> {
fn cmp(&self, other: &Self) -> Ordering {
self.id.cmp(&other.id)
}
}
impl<M: Message> Hash for MessageId<M> {
fn hash<H: Hasher>(&self, state: &mut H) {
Hash::hash(&self.id, state);
}
}