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
//! A future adapter that turns a future that never resolves (i.e., returns `Infallible`)
//! into a future that can resolve to any type.
//!
//! This is useful in scenarios where you have a task that runs forever (like a background
//! service) but need to integrate it into an API that expects a specific return type,
//! such as `tokio::task::JoinSet`.
//!
//! The core of this crate is the [`AbsurdFuture`] struct and the convenient
//! [`absurd_future`] function.
//!
//! For a detailed explanation of the motivation behind this crate and the concept of
//! uninhabited types in Rust async code, see the blog post:
//! [How to use Rust's never type (!) to write cleaner async code](https://academy.fpblock.com/blog/rust-never-type-async-code).
//!
//! # Example
//!
//! ```
//! use std::convert::Infallible;
//! use std::future;
//! use absurd_future::absurd_future;
//!
//! // A future that never completes.
//! async fn task_that_never_returns() -> Infallible {
//! loop {
//! // In a real scenario, this might be `tokio::time::sleep` or another
//! // future that never resolves. For this example, we'll just pend forever.
//! future::pending::<()>().await;
//! }
//! }
//!
//! async fn main() {
//! // We have a task that never returns, but we want to use it in a
//! // context that expects a `Result<(), &str>`.
//! let future = task_that_never_returns();
//!
//! // Wrap it with `absurd_future` to change its output type.
//! let adapted_future: _ = absurd_future::<_, Result<(), &str>>(future);
//!
//! // This adapted future will now pend forever, just like the original,
//! // but its type signature satisfies the requirement.
//! }
//! ```
use ;
/// Turn a never-returning future into a future yielding any desired type.
///
/// This struct is created by the [`absurd_future`] function.
///
/// Useful for async tasks that logically don't complete but need to satisfy an
/// interface expecting a concrete output type. Because the inner future never
/// resolves, this future will also never resolve, so the output type `T` is
/// never actually produced.
/// Wraps a future that never returns and gives it an arbitrary output type.
///
/// This function makes it easier to create an [`AbsurdFuture`].
///
/// # Type Parameters
///
/// - `F`: The type of the inner future, which must return `Infallible`.
/// - `T`: The desired output type for the wrapped future. This is often inferred.