Make EventLoopRuntime a thin wrapper over &CppEventLoopRuntime
The idea with this is to (in a future CR), pass EventLoopRuntime
as an owned type instead of a reference. This is important
because we can move the CppEventLoopRuntime as part of the
EventLoopRuntimeHolder. Without that, it's pretty much impossible
to create an event loop task that uses the runtime internally
(e.g. to get the time).
Change-Id: I163d94edba8a57191040fef171bb7abad624f7f8
Signed-off-by: James Kuszmaul <james.kuszmaul@bluerivertech.com>
diff --git a/aos/events/event_loop_runtime.rs b/aos/events/event_loop_runtime.rs
index dc1da07..461f650 100644
--- a/aos/events/event_loop_runtime.rs
+++ b/aos/events/event_loop_runtime.rs
@@ -252,9 +252,10 @@
//
// `EventLoopHolder`s safety requirements prevent anybody else from touching the underlying
// `aos::EventLoop`.
- let mut runtime = unsafe { EventLoopRuntime::new(event_loop.into_raw()) };
+ let cpp_runtime = unsafe { CppEventLoopRuntime::new(event_loop.into_raw()).within_box() };
+ let mut runtime = unsafe { EventLoopRuntime::new(&cpp_runtime) };
fun(&mut runtime);
- Self(ManuallyDrop::new(runtime.into_cpp()), PhantomData)
+ Self(ManuallyDrop::new(cpp_runtime), PhantomData)
}
}
@@ -271,7 +272,7 @@
}
pub struct EventLoopRuntime<'event_loop>(
- Pin<Box<CppEventLoopRuntime>>,
+ &'event_loop CppEventLoopRuntime,
// See documentation of [`new`] for details.
InvariantLifetime<'event_loop>,
);
@@ -362,38 +363,8 @@
/// Following these rules is very tricky. Be very cautious calling this function. It exposes an
/// unbound lifetime, which means you should wrap it directly in a function that attaches a
/// correct lifetime.
- pub unsafe fn new(event_loop: *mut CppEventLoop) -> Self {
- Self(
- // SAFETY: We push all the validity requirements for this up to our caller.
- unsafe { CppEventLoopRuntime::new(event_loop) }.within_box(),
- InvariantLifetime::default(),
- )
- }
-
- /// Creates a Rust wrapper from the underlying C++ object, with an unbound lifetime.
- ///
- /// This may never be useful, but it's here for this big scary comment to explain why it's not
- /// useful.
- ///
- /// # Safety
- ///
- /// See [`new`] for safety restrictions on `'event_loop` when calling this. In particular, see
- /// the note about how tricky doing this correctly is, and remember that for this function the
- /// event loop in question isn't even an argument to this function so it's even trickier. Also
- /// note that you cannot call this on the result of [`into_cpp`] without violating those
- /// restrictions.
- pub unsafe fn from_cpp(cpp: Pin<Box<CppEventLoopRuntime>>) -> Self {
- Self(cpp, InvariantLifetime::default())
- }
-
- /// Extracts the underlying C++ object, without the corresponding Rust lifetime. This is useful
- /// to stop the propagation of Rust lifetimes without destroying the underlying object which
- /// contains all the state.
- ///
- /// Note that you *cannot* call [`from_cpp`] on the result of this, because that will violate
- /// [`from_cpp`]'s safety requirements.
- pub fn into_cpp(self) -> Pin<Box<CppEventLoopRuntime>> {
- self.0
+ pub unsafe fn new(event_loop: &'event_loop CppEventLoopRuntime) -> Self {
+ Self(event_loop, InvariantLifetime::default())
}
/// Returns the pointer passed into the constructor.
diff --git a/aos/events/shm_event_loop.rs b/aos/events/shm_event_loop.rs
index bc7cc0a..cd611da 100644
--- a/aos/events/shm_event_loop.rs
+++ b/aos/events/shm_event_loop.rs
@@ -1,6 +1,6 @@
pub use aos_configuration::{Configuration, ConfigurationExt};
pub use aos_events_event_loop_runtime::{
- CppEventLoop, CppExitHandle, EventLoopRuntime, ExitHandle,
+ CppEventLoop, CppEventLoopRuntime, CppExitHandle, EventLoopRuntime, ExitHandle,
};
use aos_configuration_fbs::aos::Configuration as RustConfiguration;
@@ -172,7 +172,9 @@
// SAFETY: The runtime and the event loop (i.e. self) both get destroyed at the end of this
// scope: first the runtime followed by the event loop. The runtime gets exclusive access
// during initialization in `fun` while the event loop remains unused.
- let runtime = unsafe { EventLoopRuntime::new(self.inner.as_mut().event_loop_mut()) };
+ let cpp_runtime =
+ unsafe { CppEventLoopRuntime::new(self.inner.as_mut().event_loop_mut()).within_box() };
+ let runtime = unsafe { EventLoopRuntime::new(&cpp_runtime) };
let mut runtime = Scoped::new(runtime);
fun(&mut runtime);
self.run();