From 2393d06127161a7a1bc20251225ad6f041f1fa90 Mon Sep 17 00:00:00 2001 From: Dmitry Dodzin Date: Tue, 28 Jan 2025 14:01:52 +0200 Subject: [PATCH] Docs and bus -> message_bus --- mirrord/intproxy/src/background_tasks.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/mirrord/intproxy/src/background_tasks.rs b/mirrord/intproxy/src/background_tasks.rs index 315bbc7af15..f4987ea0e85 100644 --- a/mirrord/intproxy/src/background_tasks.rs +++ b/mirrord/intproxy/src/background_tasks.rs @@ -20,6 +20,7 @@ use tokio_stream::{wrappers::ReceiverStream, StreamExt, StreamMap, StreamNotifyC pub struct MessageBus { tx: Sender, rx: Receiver, + // Note if adding any new fields do look at `MessageBus::cast`'s unsafe block. } impl MessageBus { @@ -37,10 +38,13 @@ impl MessageBus { } } + /// Cast `&mut MessageBus` as `&mut MessageBus` only if they share the same message types pub fn cast(&mut self) -> &mut MessageBus where R: BackgroundTask, { + // SAFETY: since MessageBus consits of only the `Sender` and `Receiver` and both should + // match. unsafe { &mut *(self as *mut MessageBus as *mut MessageBus) } } @@ -148,9 +152,9 @@ where type MessageIn = T::MessageIn; type MessageOut = T::MessageOut; - async fn run(&mut self, bus: &mut MessageBus) -> Result<(), Self::Error> { + async fn run(&mut self, message_bus: &mut MessageBus) -> Result<(), Self::Error> { let RestartableBackgroundTaskWrapper { task } = self; - let task_bus = bus.cast(); + let task_bus = message_bus.cast(); match task.run(task_bus).await { Err(run_error) => {