executor work
This commit is contained in:
@@ -1,21 +1,40 @@
|
||||
use async_executors::JoinHandle;
|
||||
use super::*;
|
||||
use core::future::Future;
|
||||
use core::pin::Pin;
|
||||
use core::task::{Context, Poll};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct MustJoinHandle<T> {
|
||||
join_handle: JoinHandle<T>,
|
||||
join_handle: Option<LowLevelJoinHandle<T>>,
|
||||
completed: bool,
|
||||
}
|
||||
|
||||
impl<T> MustJoinHandle<T> {
|
||||
pub fn new(join_handle: JoinHandle<T>) -> Self {
|
||||
pub fn new(join_handle: LowLevelJoinHandle<T>) -> Self {
|
||||
Self {
|
||||
join_handle,
|
||||
join_handle: Some(join_handle),
|
||||
completed: false,
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn abort(mut self) {
|
||||
if !self.completed {
|
||||
cfg_if! {
|
||||
if #[cfg(feature="rt-async-std")] {
|
||||
if let Some(jh) = self.join_handle.take() {
|
||||
jh.cancel().await;
|
||||
self.completed = true;
|
||||
}
|
||||
} else if #[cfg(feature="rt-tokio")] {
|
||||
if let Some(jh) = self.join_handle.take() {
|
||||
jh.abort();
|
||||
let _ = jh.await;
|
||||
self.completed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Drop for MustJoinHandle<T> {
|
||||
@@ -31,10 +50,16 @@ impl<T: 'static> Future for MustJoinHandle<T> {
|
||||
type Output = T;
|
||||
|
||||
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||
match Pin::new(&mut self.join_handle).poll(cx) {
|
||||
match Pin::new(self.join_handle.as_mut().unwrap()).poll(cx) {
|
||||
Poll::Ready(t) => {
|
||||
self.completed = true;
|
||||
Poll::Ready(t)
|
||||
cfg_if! {
|
||||
if #[cfg(feature="rt-async-std")] {
|
||||
Poll::Ready(t)
|
||||
} else if #[cfg(feature="rt-tokio")] {
|
||||
Poll::Ready(t.unwrap())
|
||||
}
|
||||
}
|
||||
}
|
||||
Poll::Pending => Poll::Pending,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user