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 <sean@xobs.io>
This commit is contained in:
Sean Cross 2022-04-25 14:19:48 +08:00
parent 04e91b8c44
commit d8a413b7c2

View file

@ -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<crate::Result, (Envelope, crate::Error)> {
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"))]