Skip to main content

moirai_core/communication/
message.rs

1use std::sync::Arc;
2
3/// Zero-copy message for efficient communication
4pub struct Message<T> {
5    /// The shared data
6    data: Arc<T>,
7}
8
9impl<T> Message<T> {
10    /// Create a new message
11    pub fn new(data: T) -> Self {
12        Self {
13            data: Arc::new(data),
14        }
15    }
16
17    /// Get a reference to the data
18    pub fn data(&self) -> &T {
19        &self.data
20    }
21
22    /// Take ownership of the data if this is the only reference
23    pub fn try_unwrap(self) -> Result<T, Self> {
24        Arc::try_unwrap(self.data).map_err(|data| Self { data })
25    }
26}
27
28impl<T> Clone for Message<T> {
29    fn clone(&self) -> Self {
30        Self {
31            data: Arc::clone(&self.data),
32        }
33    }
34}
35
36impl<T> std::fmt::Debug for Message<T> {
37    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
38        f.debug_struct("Message")
39            .field("data", &format!("Arc<{}>", std::any::type_name::<T>()))
40            .finish()
41    }
42}