1use std::cell::Cell;
2use winit::event_loop::ActiveEventLoop;
34thread_local! {
5static CURRENT_EVENT_LOOP: Cell<Option<*const ActiveEventLoop>> = const { Cell::new(None) };
6}
78struct EventLoopGuard;
910impl EventLoopGuard {
11fn new(event_loop: &ActiveEventLoop) -> Self {
12 CURRENT_EVENT_LOOP.with(|cell| {
13assert!(
14 cell.get().is_none(),
15"Attempted to set a new event loop while one is already set"
16);
17 cell.set(Some(std::ptr::from_ref::<ActiveEventLoop>(event_loop)));
18 });
19Self
20}
21}
2223impl Drop for EventLoopGuard {
24fn drop(&mut self) {
25 CURRENT_EVENT_LOOP.with(|cell| cell.set(None));
26 }
27}
2829// Helper function to safely use the current event loop
30#[allow(unsafe_code)]
31pub fn with_current_event_loop<F, R>(f: F) -> Option<R>
32where
33F: FnOnce(&ActiveEventLoop) -> R,
34{
35 CURRENT_EVENT_LOOP.with(|cell| {
36 cell.get().map(|ptr| {
37// SAFETY:
38 // 1. The pointer is guaranteed to be valid when it's Some, as the EventLoopGuard that created it
39 // lives at least as long as the reference, and clears it when it's dropped. Only run_with_event_loop creates
40 // a new EventLoopGuard, and does not leak it.
41 // 2. Since the pointer was created from a borrow which lives at least as long as this pointer there are
42 // no mutable references to the ActiveEventLoop.
43let event_loop = unsafe { &*ptr };
44 f(event_loop)
45 })
46 })
47}
4849// The only public interface to use the event loop
50pub fn with_event_loop_context(event_loop: &ActiveEventLoop, f: impl FnOnce()) {
51// NOTE: For safety, this guard must NOT be leaked.
52let _guard = EventLoopGuard::new(event_loop);
53 f();
54}