From d8a413b7c2d57a9dd627e1eb2c03946b5110b841 Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Mon, 25 Apr 2022 14:19:48 +0800 Subject: [PATCH] xous: add `take_message()` and `send()` calls to the envelope Add a function to enable "regifting" a message that was sent to you. Signed-off-by: Sean Cross --- xous-rs/src/definitions/messages/envelope.rs | 37 ++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/xous-rs/src/definitions/messages/envelope.rs b/xous-rs/src/definitions/messages/envelope.rs index 3f8f8326f..885e85048 100644 --- a/xous-rs/src/definitions/messages/envelope.rs +++ b/xous-rs/src/definitions/messages/envelope.rs @@ -1,4 +1,5 @@ use super::{Message, MessageSender}; +use crate::CID; #[repr(C)] #[derive(Debug, PartialEq)] @@ -26,6 +27,42 @@ impl Envelope { ret.1[4], ] } + + pub fn take_message(self) -> Message { + use core::mem::ManuallyDrop; + // Convert `Self` into something that won't have its "Drop" method called + let manual_self = ManuallyDrop::new(self); + + // This function only works on memory messages + unsafe { core::ptr::read(&manual_self.body) } + } + + pub fn send(self, connection: CID) -> Result { + use core::mem::ManuallyDrop; + // Convert `Self` into something that won't have its "Drop" method called + let manual_self = ManuallyDrop::new(self); + + // This function only works on memory messages + let body = unsafe { core::ptr::read(&manual_self.body) }; + match body { + Message::Move(_) => { + let result = crate::send_message(connection, body); + if let Ok(crate::Result::Ok) = result { + return Ok(crate::Result::Ok); + } + return Err(( + ManuallyDrop::into_inner(manual_self), + crate::Error::MemoryInUse, + )); + } + _ => { + return Err(( + ManuallyDrop::into_inner(manual_self), + crate::Error::MemoryInUse, + )) + } + } + } } #[cfg(not(feature = "forget-memory-messages"))]