use std::pin::Pin;
use std::task::{Context, Poll};
use super::{ArrayOps, UnsafeArray};
use crate::scheduler::LamellarTask;
use crate::warnings::RuntimeWarning;
use crate::{Darc, Dist, LamellarTeamRT};
use futures_util::{ready, Future};
use pin_project::{pin_project, pinned_drop};
#[must_use = " UnsafeArray 'new' handles do nothing unless polled or awaited, or 'spawn()' or 'block()' are called"]
#[pin_project(PinnedDrop)]
#[doc(alias = "Collective")]
pub struct UnsafeArrayHandle<T: Dist + ArrayOps + 'static> {
pub(crate) team: Darc<LamellarTeamRT>,
pub(crate) launched: bool,
#[pin]
pub(crate) creation_future: Pin<Box<dyn Future<Output = UnsafeArray<T>> + Send>>,
}
#[pinned_drop]
impl<T: Dist + ArrayOps + 'static> PinnedDrop for UnsafeArrayHandle<T> {
fn drop(self: Pin<&mut Self>) {
if !self.launched {
RuntimeWarning::DroppedHandle("a UnsafeArrayHandle").print();
}
}
}
impl<T: Dist + ArrayOps + 'static> UnsafeArrayHandle<T> {
pub fn block(mut self) -> UnsafeArray<T> {
self.launched = true;
RuntimeWarning::BlockingCall(
"UnsafeArrayHandle::block",
"<handle>.spawn() or<handle>.await",
)
.print();
self.team.clone().block_on(self)
}
#[must_use = "this function returns a future [LamellarTask] used to poll for completion. Call '.await' on the returned future in an async context or '.block()' in a non async context. Alternatively it may be acceptable to call '.block()' instead of 'spawn()' on this handle"]
pub fn spawn(mut self) -> LamellarTask<UnsafeArray<T>> {
self.launched = true;
self.team.clone().spawn(self)
}
}
impl<T: Dist + ArrayOps + 'static> Future for UnsafeArrayHandle<T> {
type Output = UnsafeArray<T>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
self.launched = true;
let mut this = self.project();
let array = ready!(this.creation_future.as_mut().poll(cx));
Poll::Ready(array)
}
}