use std::future::Future;
use dportable::{CancellationToken, Cancelled, CancelledOwned};
use serde::{Deserialize, Serialize};
pub type Result<T> = std::result::Result<T, Aborted>;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Aborted;
pub type AborterToken = CancellationToken;
pub type AbortionChildToken = CancellationToken;
pub type AbortedFuture<'a> = Cancelled<'a>;
pub type AbortedFutureOwned = CancelledOwned;
#[derive(Debug)]
pub struct AbortionToken(AborterToken);
impl AbortionToken {
pub fn new(token: AborterToken) -> Self {
Self(token)
}
pub fn child_token(&self) -> AbortionChildToken {
self.0.child_token()
}
pub fn is_aborted(&self) -> bool {
self.0.is_cancelled()
}
pub fn aborted(&self) -> AbortedFuture<'_> {
self.0.cancelled()
}
pub fn aborted_owned(self) -> AbortedFutureOwned {
self.0.cancelled_owned()
}
pub async fn run_until_aborted<F>(&self, fut: F) -> Option<F::Output>
where
F: Future,
{
self.0.run_until_cancelled(fut).await
}
pub async fn run_until_aborted_owned<F>(self, fut: F) -> Option<F::Output>
where
F: Future,
{
self.0.run_until_cancelled_owned(fut).await
}
}