2023-12-26 01:22:10 +01:00
|
|
|
use super::super::main_thread::MainThreadMarker;
|
2023-10-16 15:50:22 +02:00
|
|
|
use std::cell::{Ref, RefCell};
|
|
|
|
|
use std::future::Future;
|
|
|
|
|
use std::marker::PhantomData;
|
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
|
|
// Unsafe wrapper type that allows us to use `T` when it's not `Send` from other threads.
|
|
|
|
|
// `value` **must** only be accessed on the main thread.
|
2024-02-01 14:27:44 +01:00
|
|
|
pub struct Wrapper<V: 'static, S: Clone + Send, E> {
|
|
|
|
|
value: Value<V>,
|
2023-10-16 15:50:22 +02:00
|
|
|
handler: fn(&RefCell<Option<V>>, E),
|
|
|
|
|
sender_data: S,
|
|
|
|
|
sender_handler: fn(&S, E),
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-01 14:27:44 +01:00
|
|
|
struct Value<V> {
|
2023-10-16 15:50:22 +02:00
|
|
|
// SAFETY:
|
|
|
|
|
// This value must not be accessed if not on the main thread.
|
|
|
|
|
//
|
|
|
|
|
// - We wrap this in an `Arc` to allow it to be safely cloned without
|
|
|
|
|
// accessing the value.
|
|
|
|
|
// - The `RefCell` lets us mutably access in the main thread but is safe to
|
|
|
|
|
// drop in any thread because it has no `Drop` behavior.
|
|
|
|
|
// - The `Option` lets us safely drop `T` only in the main thread.
|
|
|
|
|
value: Arc<RefCell<Option<V>>>,
|
|
|
|
|
// Prevent's `Send` or `Sync` to be automatically implemented.
|
|
|
|
|
local: PhantomData<*const ()>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SAFETY: See `Self::value`.
|
2024-02-01 14:27:44 +01:00
|
|
|
unsafe impl<V> Send for Value<V> {}
|
2023-10-16 15:50:22 +02:00
|
|
|
// SAFETY: See `Self::value`.
|
2024-02-01 14:27:44 +01:00
|
|
|
unsafe impl<V> Sync for Value<V> {}
|
2023-10-16 15:50:22 +02:00
|
|
|
|
2024-02-01 14:27:44 +01:00
|
|
|
impl<V, S: Clone + Send, E> Wrapper<V, S, E> {
|
2023-10-16 15:50:22 +02:00
|
|
|
#[track_caller]
|
|
|
|
|
pub fn new<R: Future<Output = ()>>(
|
2023-12-26 01:22:10 +01:00
|
|
|
_: MainThreadMarker,
|
2023-10-16 15:50:22 +02:00
|
|
|
value: V,
|
|
|
|
|
handler: fn(&RefCell<Option<V>>, E),
|
|
|
|
|
receiver: impl 'static + FnOnce(Arc<RefCell<Option<V>>>) -> R,
|
|
|
|
|
sender_data: S,
|
|
|
|
|
sender_handler: fn(&S, E),
|
|
|
|
|
) -> Option<Self> {
|
|
|
|
|
let value = Arc::new(RefCell::new(Some(value)));
|
|
|
|
|
|
|
|
|
|
wasm_bindgen_futures::spawn_local({
|
|
|
|
|
let value = Arc::clone(&value);
|
|
|
|
|
async move {
|
|
|
|
|
receiver(Arc::clone(&value)).await;
|
|
|
|
|
drop(value.borrow_mut().take().unwrap());
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Some(Self {
|
|
|
|
|
value: Value {
|
|
|
|
|
value,
|
|
|
|
|
local: PhantomData,
|
|
|
|
|
},
|
|
|
|
|
handler,
|
|
|
|
|
sender_data,
|
|
|
|
|
sender_handler,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn send(&self, event: E) {
|
2023-12-26 01:22:10 +01:00
|
|
|
if MainThreadMarker::new().is_some() {
|
|
|
|
|
(self.handler)(&self.value.value, event)
|
|
|
|
|
} else {
|
|
|
|
|
(self.sender_handler)(&self.sender_data, event)
|
|
|
|
|
}
|
2023-10-16 15:50:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn value(&self) -> Option<Ref<'_, V>> {
|
2023-12-26 01:22:10 +01:00
|
|
|
MainThreadMarker::new()
|
|
|
|
|
.map(|_| Ref::map(self.value.value.borrow(), |value| value.as_ref().unwrap()))
|
2023-10-16 15:50:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn with_sender_data<T>(&self, f: impl FnOnce(&S) -> T) -> T {
|
|
|
|
|
f(&self.sender_data)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-01 14:27:44 +01:00
|
|
|
impl<V, S: Clone + Send, E> Clone for Wrapper<V, S, E> {
|
2023-10-16 15:50:22 +02:00
|
|
|
fn clone(&self) -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
value: Value {
|
|
|
|
|
value: self.value.value.clone(),
|
|
|
|
|
local: PhantomData,
|
|
|
|
|
},
|
|
|
|
|
handler: self.handler,
|
|
|
|
|
sender_data: self.sender_data.clone(),
|
|
|
|
|
sender_handler: self.sender_handler,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|