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
//! The [`Task`] contract and shared task types.
use ;
use crateTaskError;
use crateTaskContext;
/// Boxed `Send` future returned by [`Task::spawn`].
pub type BoxTaskFuture = ;
/// Shared task handle (`Arc<dyn Task>`).
pub type TaskRef = ;
/// Async work managed by a [`Supervisor`](crate::Supervisor).
/// > Use [`TaskFn`](crate::TaskFn) unless you need a custom task type.
///
/// ## Attempt Contract
///
/// The supervisor calls [`spawn`](Task::spawn) once for every attempt.
/// > `spawn` **must return a new future on every call**.
///
/// The same task object is reused for every attempt.
/// State stored in its fields can therefore survive retries.
/// Each returned future is attempt-local; its local values do not carry over to the next attempt.
///
/// ```text
/// spawn(ctx) -> attempt 1 -> retry delay -> spawn(ctx) -> attempt 2
/// ```
///
/// ## Cancellation
///
/// Long-running tasks should listen to [`TaskContext::cancelled`] or use [`TaskContext::run_until_cancelled`].
/// Return [`TaskError::Canceled`] when the task stops for that reason.
/// > **This is a normal stop and is never retried.**
///
/// Short tasks may ignore cancellation if they finish quickly.
/// During shutdown, a task that does not stop before the grace period is aborted.
/// Dropping its future does not rollback external side effects.
///
/// | Result | Meaning | Can restart? |
/// |-------------------------|-------------------|-------------------------------------------------------------------|
/// | `Ok(())` | Attempt succeeded | Only with [`RestartPolicy::Always`](crate::RestartPolicy::Always) |
/// | [`TaskError::Fail`] | Retryable failure | If policy and retry limit allow it |
/// | [`TaskError::Timeout`] | Attempt timed out | If policy and retry limit allow it |
/// | [`TaskError::Canceled`] | Cooperative stop | No |
/// | [`TaskError::Fatal`] | Permanent failure | No |
///
/// A panic in `spawn` or in the returned future is caught and treated as a retryable failure.
/// > **Do not use panic for normal error handling.**
///
/// ## See Also
///
/// - For the closure-based implementation and a shared-state example, see [`TaskFn`](crate::TaskFn).
/// - To configure restart, backoff, and timeout see [`TaskSpec`](crate::TaskSpec).