From 6e4dbf08cc1bcf3dcc50e30cfba292b52763a0c6 Mon Sep 17 00:00:00 2001 From: Jacob-JB Date: Wed, 26 Jun 2024 14:41:47 +1000 Subject: [PATCH] changed MessageParams api to MessageId resource --- crates/nevy_messaging/src/lib.rs | 4 +--- crates/nevy_messaging/src/serialize.rs | 15 ++++++--------- 2 files changed, 7 insertions(+), 12 deletions(-) diff --git a/crates/nevy_messaging/src/lib.rs b/crates/nevy_messaging/src/lib.rs index 6691ffa..88b1fd7 100644 --- a/crates/nevy_messaging/src/lib.rs +++ b/crates/nevy_messaging/src/lib.rs @@ -9,9 +9,7 @@ pub mod deserialize; pub mod serialize; pub mod prelude { - pub use crate::serialize::{ - MessageSerializationPlugin, MessageStreamParams, MessageStreamState, - }; + pub use crate::serialize::{MessageId, MessageSerializationPlugin, MessageStreamState}; pub use crate::deserialize::{ EndpointMessagingHeader, MessageDeserializationPlugin, ReceivedMessages, diff --git a/crates/nevy_messaging/src/serialize.rs b/crates/nevy_messaging/src/serialize.rs index b532533..bd6b921 100644 --- a/crates/nevy_messaging/src/serialize.rs +++ b/crates/nevy_messaging/src/serialize.rs @@ -58,8 +58,10 @@ impl MessageIdBuilder } } -#[derive(Resource)] -struct MessageId { +/// the message id for a message `T`, +/// assigned by [MessageSerializationPlugin] and stored as a resource +#[derive(Clone, Copy, Resource)] +pub struct MessageId { _p: PhantomData<(C, T)>, message_id: u16, } @@ -71,11 +73,6 @@ pub struct MessageStreamState { buffer: Vec, } -#[derive(bevy::ecs::system::SystemParam)] -pub struct MessageStreamParams<'w, C: Send + Sync + 'static, T: Send + Sync + 'static> { - message_id: Res<'w, MessageId>, -} - #[derive(Debug)] pub enum MessageStreamSendError { StreamClosed, @@ -174,7 +171,7 @@ impl MessageStreamState { pub fn send( &mut self, connection: &mut BevyConnectionMut, - params: &MessageStreamParams, + message_id: MessageId, message: &T, ) -> Result { if !self.ready() { @@ -183,7 +180,7 @@ impl MessageStreamState { } } - let message_id = params.message_id.message_id.to_be_bytes(); + let message_id = message_id.message_id.to_be_bytes(); let bytes = bincode::serialize(message).expect("Failed to serialize message"); let message_length = (bytes.len() as u16).to_be_bytes();