context_async/context.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
use std::future::Future;
use std::time;
#[cfg(feature = "name")]
use crate::name::Name;
use crate::{Error, Timer};
/// The [`Context`] trait defines the required methods for `Context`.
/// It can define a duration, be cancellable, and immediately cancel
/// async functions if the processing time exceeds the allowed
/// duration without being cancelled. Additionally, the `Context`
/// can spawn child contexts.
///
/// # Examples
/// ```
/// use context_async::{Context, Timer};
/// async fn my_function() {}
///
/// # tokio_test::block_on(async {
/// let timer = Timer::background(); // a new context without duration limit///
/// timer.handle(my_function()).await.unwrap();
/// # });
/// ```
///
/// In addition to using `handle`, you can also handle it
/// with the `future.with()` method by simply importing the `With` trait.
///
/// ```
/// use context_async::{With, Context, Timer};
/// async fn my_function() {}
///
/// let timer = Timer::todo(); // same as background().
/// # tokio_test::block_on(async {
/// my_function()
/// .with(timer)
/// .await
/// .unwrap();
/// # });
/// ```
#[async_trait::async_trait]
pub trait Context: Clone + Send + Sync {
type SubContext: Context;
/// return the basic [`Timer`].
fn timer(&self) -> Timer;
/// return the name of this context
#[cfg(feature = "name")]
async fn name(&self) -> Name {
self.timer().name().await
}
/// return the deadline [`time::Instant`] of this context.
/// return [None] when this context doesn't have deadline.
async fn deadline(&self) -> Option<time::Instant> {
self.timer().deadline().await
}
/// cancel this context, then cancel all its childs.
async fn cancel(&self) {
self.timer().cancel().await
}
/// check whether this context is cancelled or not.
async fn is_cancelled(&self) -> bool {
self.timer().is_cancelled().await
}
/// check whether this context is timeout or not.
async fn is_timeout(&self) -> bool {
self.timer().is_timeout().await
}
/// check whether there is an [`Error`] in context.
async fn error(&self) -> Option<Error> {
if self.is_cancelled().await {
Some(Error::ContextCancelled)
} else if self.is_timeout().await {
Some(Error::ContextTimeout)
} else {
None
}
}
/// spawn a new child context.
///
/// When the parent (self) is cancelled (call by [`Self::cancel`]),
/// the child context will be cancelled too.
///
/// # Example
/// ```rust
///
/// use context_async::{Context, Timer};
///
/// tokio_test::block_on(async {
/// let ctx = Timer::background();
/// let child = ctx.spawn().await;
/// let child_of_child = child.spawn().await;
///
/// ctx.cancel().await; // <- parent is cancelled.
///
/// assert!(child.is_cancelled().await); // the child is cancelled too.
/// assert!(child_of_child.is_cancelled().await); // also cancelled.
/// });
///
/// ```
async fn spawn(&self) -> Self::SubContext;
/// spawn a new child context, with a new timeout parameter.
///
/// Same as [`Self::spawn`], when the parent (self) is cancelled (call by [`Self::cancel`]),
/// the child context will be cancelled too.
///
/// The expire_at instant should not be longer than the parent's expire_at.
///
/// # Note
/// see [`Self::spawn`] for more examples.
async fn spawn_with_timeout(&self, timeout: time::Duration) -> Self::SubContext;
/// spawn a new child context, with a new timeout parameter in seconds.
async fn spawn_in_seconds(&self, secs: u64) -> Self::SubContext {
self.spawn_with_timeout(time::Duration::from_secs(secs)).await
}
/// spawn a new child context, with a new timeout parameter in milliseconds.
async fn spawn_in_milliseconds(&self, millis: u64) -> Self::SubContext {
self.spawn_with_timeout(time::Duration::from_millis(millis)).await
}
/// handle a future
///
/// # Examples
/// ```rust
/// use context_async::{Context, Timer};
///
/// # tokio_test::block_on(async {
/// let ctx = Timer::background();
/// let task = tokio::time::sleep(tokio::time::Duration::from_secs(1));
///
/// ctx.handle(task).await.unwrap();
/// # });
///
/// ```
async fn handle<'a, Fut, T>(&self, fut: Fut) -> crate::Result<T>
where
Fut: Future<Output = T> + Send + 'a
{
self.timer().handle(fut).await
}
/// handle a future that returns Result<T, E>.
///
/// # Note
/// `E` is user-defined error, which implements `From<Error>`.
///
/// # Examples
///
/// ```rust
/// use context_async::{Context, Timer};
///
/// #[derive(Debug, Clone)]
/// struct MyError;
///
/// impl From<context_async::Error> for MyError {
/// fn from(value: context_async::Error) -> Self {
/// MyError
/// }
/// }
///
/// async fn my_func() -> Result<u8, MyError> {
/// Ok(42)
/// }
///
/// # tokio_test::block_on(async {
/// let ctx = Timer::background();
/// let task = my_func();
/// let _ = ctx.handle_result(task).await;
/// # });
/// ```
async fn handle_result<'a, Fut, T, E>(&self, fut: Fut) -> Result<T, E>
where
Fut: Future<Output = Result<T, E>> + Send + 'a,
E: From<Error>,
{
self.timer().handle(fut).await?
}
}
#[async_trait::async_trait]
impl<T: Context> Context for &T {
type SubContext = T::SubContext;
fn timer(&self) -> Timer {
(*self).timer()
}
async fn spawn(&self) -> T::SubContext {
(*self).spawn().await
}
async fn spawn_with_timeout(&self, timeout: time::Duration) -> T::SubContext {
(*self).spawn_with_timeout(timeout).await
}
}