fix cooperative cancellation
This commit is contained in:
@@ -1,20 +1,19 @@
|
||||
use async_executors::JoinHandle;
|
||||
use core::future::Future;
|
||||
use core::pin::Pin;
|
||||
use core::sync::atomic::{AtomicBool, Ordering};
|
||||
use core::task::{Context, Poll};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct MustJoinHandle<T> {
|
||||
join_handle: JoinHandle<T>,
|
||||
completed: AtomicBool,
|
||||
completed: bool,
|
||||
}
|
||||
|
||||
impl<T> MustJoinHandle<T> {
|
||||
pub fn new(join_handle: JoinHandle<T>) -> Self {
|
||||
Self {
|
||||
join_handle,
|
||||
completed: AtomicBool::new(false),
|
||||
completed: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -22,7 +21,7 @@ impl<T> MustJoinHandle<T> {
|
||||
impl<T> Drop for MustJoinHandle<T> {
|
||||
fn drop(&mut self) {
|
||||
// panic if we haven't completed
|
||||
if !self.completed.load(Ordering::Relaxed) {
|
||||
if !self.completed {
|
||||
panic!("MustJoinHandle was not completed upon drop. Add cooperative cancellation where appropriate to ensure this is completed before drop.")
|
||||
}
|
||||
}
|
||||
@@ -34,7 +33,7 @@ impl<T: 'static> Future for MustJoinHandle<T> {
|
||||
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||
match Pin::new(&mut self.join_handle).poll(cx) {
|
||||
Poll::Ready(t) => {
|
||||
self.completed.store(true, Ordering::Relaxed);
|
||||
self.completed = true;
|
||||
Poll::Ready(t)
|
||||
}
|
||||
Poll::Pending => Poll::Pending,
|
||||
|
||||
Reference in New Issue
Block a user