diff --git a/bevy_matchbox/src/socket.rs b/bevy_matchbox/src/socket.rs index 75e091ca..db8eb355 100644 --- a/bevy_matchbox/src/socket.rs +++ b/bevy_matchbox/src/socket.rs @@ -1,13 +1,17 @@ use bevy::{ ecs::system::Command, - prelude::{Commands, Component, Deref, DerefMut, Resource, World}, + prelude::{Commands, Component, Resource, World}, tasks::IoTaskPool, }; pub use matchbox_socket; use matchbox_socket::{ BuildablePlurality, MessageLoopFuture, SingleChannel, WebRtcSocket, WebRtcSocketBuilder, }; -use std::marker::PhantomData; +use std::{ + fmt::Debug, + marker::PhantomData, + ops::{Deref, DerefMut}, +}; /// A [`WebRtcSocket`] as a [`Component`] or [`Resource`]. /// @@ -65,8 +69,22 @@ use std::marker::PhantomData; /// commands.remove_resource::>(); /// } /// ``` -#[derive(Resource, Component, Debug, Deref, DerefMut)] -pub struct MatchboxSocket(WebRtcSocket); +#[derive(Resource, Component, Debug)] +pub struct MatchboxSocket(WebRtcSocket, Box); + +impl Deref for MatchboxSocket { + type Target = WebRtcSocket; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +impl DerefMut for MatchboxSocket { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.0 + } +} impl From> for MatchboxSocket { fn from(builder: WebRtcSocketBuilder) -> Self { @@ -77,8 +95,8 @@ impl From> for MatchboxSocket { impl From<(WebRtcSocket, MessageLoopFuture)> for MatchboxSocket { fn from((socket, message_loop_fut): (WebRtcSocket, MessageLoopFuture)) -> Self { let task_pool = IoTaskPool::get(); - task_pool.spawn(message_loop_fut).detach(); - MatchboxSocket(socket) + let task = task_pool.spawn(message_loop_fut); + MatchboxSocket(socket, Box::new(task)) } }