use std::{
marker::{PhantomData, PhantomPinned},
pin::Pin,
};
use jlrs_sys::jlrs_task_gc_state;
use super::IsActive;
use crate::{error::RuntimeError, prelude::JlrsResult, runtime::state::GC_UNSAFE};
#[macro_export]
macro_rules! weak_handle {
() => {
$crate::error::project_jlrs_result(::std::pin::pin!(unsafe {
$crate::runtime::handle::weak_handle::WeakHandle::new()
}))
};
}
#[macro_export]
macro_rules! weak_handle_unchecked {
() => {
::std::pin::pin!($crate::runtime::handle::weak_handle::WeakHandle::new_unchecked())
};
}
pub struct WeakHandle {
_marker: PhantomData<*mut ()>,
_ph: PhantomPinned,
}
impl WeakHandle {
#[inline]
pub unsafe fn new() -> JlrsResult<Self> {
unsafe {
if jlrs_task_gc_state() != GC_UNSAFE {
Err(RuntimeError::IncorrectState)?;
}
Ok(Self::new_unchecked())
}
}
#[inline(always)]
pub const unsafe fn new_unchecked() -> Self {
WeakHandle {
_marker: PhantomData,
_ph: PhantomPinned,
}
}
}
impl IsActive for Pin<&mut WeakHandle> {}