#![deny(
missing_docs,
missing_debug_implementations,
missing_copy_implementations,
elided_lifetimes_in_paths,
rust_2018_idioms,
clippy::fallible_impl_from,
clippy::missing_const_for_fn,
intra_doc_link_resolution_failure
)]
#![doc(html_logo_url = "https://avatars0.githubusercontent.com/u/55122894")]
use std::future::Future;
mod dart_array;
mod into_dart;
#[cfg(feature = "catch-unwind")]
mod catch_unwind;
pub mod ffi;
pub use into_dart::IntoDart;
static mut POST_COBJECT: Option<ffi::DartPostCObjectFnType> = None;
#[no_mangle]
pub unsafe extern "C" fn store_dart_post_cobject(ptr: ffi::DartPostCObjectFnType) {
POST_COBJECT = Some(ptr);
}
#[derive(Copy, Clone, Debug)]
pub struct Isolate {
port: i64,
}
impl Isolate {
pub const fn new(port: i64) -> Self {
Self { port }
}
pub fn post(&self, msg: impl IntoDart) -> bool {
unsafe {
if let Some(func) = POST_COBJECT {
let boxed_msg = Box::new(msg.into_dart());
let ptr = Box::into_raw(boxed_msg);
let result = func(self.port, ptr);
let boxed_obj = Box::from_raw(ptr);
drop(boxed_obj);
result
} else {
false
}
}
}
pub async fn task<T, R>(self, t: T) -> bool
where
T: Future<Output = R> + Send + 'static,
R: Send + IntoDart + 'static,
{
self.post(t.await)
}
#[cfg(feature = "catch-unwind")]
pub async fn catch_unwind<T, R>(self, t: T) -> Result<bool, Box<dyn std::any::Any + Send>>
where
T: Future<Output = R> + Send + 'static,
R: Send + IntoDart + 'static,
{
catch_unwind::CatchUnwind::new(t)
.await
.map(|msg| Ok(self.post(msg)))?
}
}