#![no_std]
#![cfg_attr(nightly, feature(core_intrinsics, negative_impls))]
extern crate alloc;
use core::cell::{Cell, UnsafeCell};
use crate::queue::Queue;
mod queue;
#[cfg(target_arch = "wasm32")]
#[path = "wasm.rs"]
mod inner;
#[cfg(not(target_arch = "wasm32"))]
compile_error!("only compile in wasm32");
pub use inner::*;
pub struct Context<A: App> {
app: UnsafeCell<A>,
q: Queue<A::Message>,
ready: Cell<bool>,
}
impl<A: App> Context<A> {
pub(crate) fn new(app: A) -> Self {
Self {
app: UnsafeCell::new(app),
q: Queue::new(),
ready: Cell::new(false),
}
}
#[inline]
#[allow(clippy::mut_from_ref)]
pub(crate) unsafe fn app(&self) -> &mut A {
&mut *self.app.get()
}
#[inline]
pub(crate) fn ready(&self, r: bool) {
self.ready.replace(r);
}
#[inline]
pub(crate) fn is_ready(&self) -> bool {
self.ready.get()
}
#[inline]
pub(crate) fn push(&self, msg: A::Message) {
self.q.push(msg);
}
#[inline]
pub(crate) unsafe fn pop(&self) -> Option<A::Message> {
self.q.pop()
}
}
const unsafe fn stc_to_ptr<T>(t: &'static T) -> *mut T {
t as *const T as *mut T
}
#[inline]
#[cfg(not(nightly))]
unsafe fn unwrap<T>(o: Option<T>) -> T {
o.unwrap()
}
#[inline]
#[cfg(nightly)]
unsafe fn unwrap<T>(o: Option<T>) -> T {
o.unwrap_or_else(|| core::intrinsics::unreachable())
}