100.00% Lines (22/22) 100.00% Functions (11/11)
TLA Baseline Branch
Line Hits Code Line Hits Code
1   // 1   //
2   // Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com) 2   // Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com)
3   // Copyright (c) 2026 Michael Vandeberg 3   // Copyright (c) 2026 Michael Vandeberg
4   // 4   //
5   // Distributed under the Boost Software License, Version 1.0. (See accompanying 5   // Distributed under the Boost Software License, Version 1.0. (See accompanying
6   // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6   // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7   // 7   //
8   // Official repository: https://github.com/cppalliance/capy 8   // Official repository: https://github.com/cppalliance/capy
9   // 9   //
10   10  
11   #ifndef BOOST_CAPY_EX_IMMEDIATE_HPP 11   #ifndef BOOST_CAPY_EX_IMMEDIATE_HPP
12   #define BOOST_CAPY_EX_IMMEDIATE_HPP 12   #define BOOST_CAPY_EX_IMMEDIATE_HPP
13   13  
14   #include <boost/capy/detail/config.hpp> 14   #include <boost/capy/detail/config.hpp>
15   #include <boost/capy/ex/await_context.hpp> 15   #include <boost/capy/ex/await_context.hpp>
16   #include <boost/capy/results.hpp> 16   #include <boost/capy/results.hpp>
17   17  
18   #include <coroutine> 18   #include <coroutine>
19   #include <stop_token> 19   #include <stop_token>
20   #include <utility> 20   #include <utility>
21   21  
22   namespace boost { 22   namespace boost {
23   namespace capy { 23   namespace capy {
24   24  
25 - /** An awaitable that completes immediately with a value. 25 + /** Complete a `co_await` at once with a stored value.
26   26  
27   This awaitable wraps a synchronous result so it can be used in 27   This awaitable wraps a synchronous result so it can be used in
28 - contexts that require an awaitable type. It never suspends - 28 + contexts that require an awaitable type. `await_ready()` always
29 - `await_ready()` always returns `true`, so the coroutine machinery 29 + returns `true`, so the awaiting coroutine never suspends.
30 - is optimized away by the compiler.  
31   30  
32   Use this to adapt synchronous operations to satisfy async concepts 31   Use this to adapt synchronous operations to satisfy async concepts
33   like @ref ContextAwaitable without the overhead of a full coroutine frame. 32   like @ref ContextAwaitable without the overhead of a full coroutine frame.
34 - @tparam T The result type to wrap.  
35 -  
36   33  
37   @par Example 34   @par Example
38   @par !example example_1 35   @par !example example_1
39   36  
40   37  
41   @par Building synchronous I/O operations 38   @par Building synchronous I/O operations
42   @par !example example_2 39   @par !example example_2
43   40  
44   41  
  42 + @tparam T The result type to wrap.
  43 +
45   @see @ref ready, @ref results 44   @see @ref ready, @ref results
46   */ 45   */
47   template<class T> 46   template<class T>
48   struct immediate 47   struct immediate
49   { 48   {
50   /** The wrapped value. */ 49   /** The wrapped value. */
51   T value_; 50   T value_;
52   51  
53 - /** Always returns true - this awaitable never suspends. 52 + /** Check readiness; always `true`.
54   53  
55 - @return Always `true`, so the awaiting coroutine does not suspend 54 + The awaiting coroutine does not suspend, and `await_suspend` is
56 - and `await_suspend` is never called. 55 + never called.
  56 +
  57 + @return Always `true`.
57   */ 58   */
58   constexpr bool 59   constexpr bool
HITCBC 59   21 await_ready() const noexcept 60   21 await_ready() const noexcept
60   { 61   {
HITCBC 61   21 return true; 62   21 return true;
62   } 63   }
63   64  
64 - /** ContextAwaitable protocol overload. 65 + /** Satisfy @ref ContextAwaitable; never called, since `await_ready` is `true`.
65   66  
66   This overload allows `immediate` to satisfy the @ref ContextAwaitable 67   This overload allows `immediate` to satisfy the @ref ContextAwaitable
67   concept. Since the result is already available, the environment 68   concept. Since the result is already available, the environment
68 - is unused. 69 + is unused. If it were called, the returned `std::noop_coroutine()`
  70 + would leave the awaiting coroutine suspended with nothing to
  71 + resume it.
69   72  
70   @param h The coroutine handle (unused). 73   @param h The coroutine handle (unused).
71   @param env The execution environment (unused). 74   @param env The execution environment (unused).
72   75  
73 - @return `std::noop_coroutine()` to indicate no suspension. 76 + @return `std::noop_coroutine()`.
74   */ 77   */
75   std::coroutine_handle<> 78   std::coroutine_handle<>
HITCBC 76   1 await_suspend( 79   1 await_suspend(
77   std::coroutine_handle<> h, 80   std::coroutine_handle<> h,
78   await_context const* env) const noexcept 81   await_context const* env) const noexcept
79   { 82   {
80   (void)h; 83   (void)h;
81   (void)env; 84   (void)env;
HITCBC 82   1 return std::noop_coroutine(); 85   1 return std::noop_coroutine();
83   } 86   }
84   87  
85 - /** Returns the wrapped value. 88 + /** Return the wrapped value.
86   89  
87 - @return The stored value, moved if non-const. 90 + @note A move constructor of `T` that throws calls `std::terminate`.
  91 +
  92 + @return The stored value, moved out.
88   */ 93   */
89   [[nodiscard]] constexpr T 94   [[nodiscard]] constexpr T
HITCBC 90   24 await_resume() noexcept 95   24 await_resume() noexcept
91   { 96   {
HITCBC 92   24 return std::move(value_); 97   24 return std::move(value_);
93   } 98   }
94   99  
95 - /** Returns the wrapped value (const overload). 100 + /** Return the wrapped value (const overload).
96   101  
97   @return A reference to the stored value. Nothing is moved, so the 102   @return A reference to the stored value. Nothing is moved, so the
98   reference is valid only while the `immediate` is alive. 103   reference is valid only while the `immediate` is alive.
99   */ 104   */
100   [[nodiscard]] constexpr T const& 105   [[nodiscard]] constexpr T const&
101   await_resume() const noexcept 106   await_resume() const noexcept
102   { 107   {
103   return value_; 108   return value_;
104   } 109   }
105   }; 110   };
106   111  
107   /** Create an immediate awaitable for a successful `results`. 112   /** Create an immediate awaitable for a successful `results`.
108   113  
109 - This helper creates an @ref immediate wrapping an @ref results 114 + This helper creates an @ref immediate wrapping a @ref results
110   with no error and the provided values. 115   with no error and the provided values.
111   116  
112   @par Example 117   @par Example
113   @par !example example_1 118   @par !example example_1
114   119  
115   120  
116   @return An immediate awaitable containing a successful `results`. 121   @return An immediate awaitable containing a successful `results`.
117   122  
118   @see @ref immediate, @ref results 123   @see @ref immediate, @ref results
119   */ 124   */
120   inline 125   inline
121   immediate<results<>> 126   immediate<results<>>
HITCBC 122   3 ready() noexcept 127   3 ready() noexcept
123   { 128   {
HITCBC 124   3 return {{}}; 129   3 return {{}};
125   } 130   }
126   131  
127   /** Create an immediate awaitable for a successful `results` with one value. 132   /** Create an immediate awaitable for a successful `results` with one value.
128   133  
  134 + @par Exception Safety
  135 + Propagates any exception from copying or moving the arguments.
  136 +
129   @param t1 The result value. 137   @param t1 The result value.
130   138  
131   @return An immediate awaitable containing `results<T1>{std::error_code(), t1}`. 139   @return An immediate awaitable containing `results<T1>{std::error_code(), t1}`.
132   */ 140   */
133   template<class T1> 141   template<class T1>
134   immediate<results<T1>> 142   immediate<results<T1>>
HITCBC 135   4 ready(T1 t1) 143   4 ready(T1 t1)
136   { 144   {
HITCBC 137   4 return {{std::error_code(), std::move(t1)}}; 145   4 return {{std::error_code(), std::move(t1)}};
138   } 146   }
139   147  
140   /** Create an immediate awaitable for a successful `results` with two values. 148   /** Create an immediate awaitable for a successful `results` with two values.
141   149  
  150 + @par Exception Safety
  151 + Propagates any exception from copying or moving the arguments.
  152 +
142   @param t1 The first result value. 153   @param t1 The first result value.
143   @param t2 The second result value. 154   @param t2 The second result value.
144   155  
145   @return An immediate awaitable containing `results<T1,T2>{std::error_code(), t1, t2}`. 156   @return An immediate awaitable containing `results<T1,T2>{std::error_code(), t1, t2}`.
146   */ 157   */
147   template<class T1, class T2> 158   template<class T1, class T2>
148   immediate<results<T1, T2>> 159   immediate<results<T1, T2>>
HITCBC 149   2 ready(T1 t1, T2 t2) 160   2 ready(T1 t1, T2 t2)
150   { 161   {
HITCBC 151   2 return {{std::error_code(), std::move(t1), std::move(t2)}}; 162   2 return {{std::error_code(), std::move(t1), std::move(t2)}};
152   } 163   }
153   164  
154   /** Create an immediate awaitable for a successful `results` with three values. 165   /** Create an immediate awaitable for a successful `results` with three values.
155   166  
  167 + @par Exception Safety
  168 + Propagates any exception from copying or moving the arguments.
  169 +
156   @param t1 The first result value. 170   @param t1 The first result value.
157   @param t2 The second result value. 171   @param t2 The second result value.
158   @param t3 The third result value. 172   @param t3 The third result value.
159   173  
160   @return An immediate awaitable containing `results<T1,T2,T3>{std::error_code(), t1, t2, t3}`. 174   @return An immediate awaitable containing `results<T1,T2,T3>{std::error_code(), t1, t2, t3}`.
161   */ 175   */
162   template<class T1, class T2, class T3> 176   template<class T1, class T2, class T3>
163   immediate<results<T1, T2, T3>> 177   immediate<results<T1, T2, T3>>
HITCBC 164   2 ready(T1 t1, T2 t2, T3 t3) 178   2 ready(T1 t1, T2 t2, T3 t3)
165   { 179   {
HITCBC 166   2 return {{std::error_code(), std::move(t1), std::move(t2), std::move(t3)}}; 180   2 return {{std::error_code(), std::move(t1), std::move(t2), std::move(t3)}};
167   } 181   }
168   182  
169   /** Create an immediate awaitable for a failed `results`. 183   /** Create an immediate awaitable for a failed `results`.
170   184  
171 - This helper creates an @ref immediate wrapping an @ref results 185 + This helper creates an @ref immediate wrapping a @ref results
172   with an error code. 186   with an error code.
173   187  
174   @par Example 188   @par Example
175   @par !example example_2 189   @par !example example_2
176   190  
177   191  
178   @param ec The error code. 192   @param ec The error code.
179   193  
180   @return An immediate awaitable containing a failed `results`. 194   @return An immediate awaitable containing a failed `results`.
181   195  
182   @see @ref immediate, @ref results 196   @see @ref immediate, @ref results
183   */ 197   */
184   inline 198   inline
185   immediate<results<>> 199   immediate<results<>>
HITCBC 186   1 ready(std::error_code ec) noexcept 200   1 ready(std::error_code ec) noexcept
187   { 201   {
HITCBC 188   1 return {{ec}}; 202   1 return {{ec}};
189   } 203   }
190   204  
191   /** Create an immediate awaitable for a `results` with error and one value. 205   /** Create an immediate awaitable for a `results` with error and one value.
192   206  
  207 + @par Exception Safety
  208 + Propagates any exception from copying or moving the arguments.
  209 +
193   @param ec The error code. 210   @param ec The error code.
194   @param t1 The result value. 211   @param t1 The result value.
195   212  
196   @return An immediate awaitable containing `results<T1>{ec, t1}`. 213   @return An immediate awaitable containing `results<T1>{ec, t1}`.
197   */ 214   */
198   template<class T1> 215   template<class T1>
199   immediate<results<T1>> 216   immediate<results<T1>>
HITCBC 200   2 ready(std::error_code ec, T1 t1) 217   2 ready(std::error_code ec, T1 t1)
201   { 218   {
HITCBC 202   2 return {{ec, std::move(t1)}}; 219   2 return {{ec, std::move(t1)}};
203   } 220   }
204   221  
205   /** Create an immediate awaitable for a `results` with error and two values. 222   /** Create an immediate awaitable for a `results` with error and two values.
206   223  
  224 + @par Exception Safety
  225 + Propagates any exception from copying or moving the arguments.
  226 +
207   @param ec The error code. 227   @param ec The error code.
208   @param t1 The first result value. 228   @param t1 The first result value.
209   @param t2 The second result value. 229   @param t2 The second result value.
210   230  
211   @return An immediate awaitable containing `results<T1,T2>{ec, t1, t2}`. 231   @return An immediate awaitable containing `results<T1,T2>{ec, t1, t2}`.
212   */ 232   */
213   template<class T1, class T2> 233   template<class T1, class T2>
214   immediate<results<T1, T2>> 234   immediate<results<T1, T2>>
HITCBC 215   1 ready(std::error_code ec, T1 t1, T2 t2) 235   1 ready(std::error_code ec, T1 t1, T2 t2)
216   { 236   {
HITCBC 217   1 return {{ec, std::move(t1), std::move(t2)}}; 237   1 return {{ec, std::move(t1), std::move(t2)}};
218   } 238   }
219   239  
220   /** Create an immediate awaitable for a `results` with error and three values. 240   /** Create an immediate awaitable for a `results` with error and three values.
  241 +
  242 + @par Exception Safety
  243 + Propagates any exception from copying or moving the arguments.
221   244  
222   @param ec The error code. 245   @param ec The error code.
223   @param t1 The first result value. 246   @param t1 The first result value.
224   @param t2 The second result value. 247   @param t2 The second result value.
225   @param t3 The third result value. 248   @param t3 The third result value.
226   249  
227   @return An immediate awaitable containing `results<T1,T2,T3>{ec, t1, t2, t3}`. 250   @return An immediate awaitable containing `results<T1,T2,T3>{ec, t1, t2, t3}`.
228   */ 251   */
229   template<class T1, class T2, class T3> 252   template<class T1, class T2, class T3>
230   immediate<results<T1, T2, T3>> 253   immediate<results<T1, T2, T3>>
HITCBC 231   1 ready(std::error_code ec, T1 t1, T2 t2, T3 t3) 254   1 ready(std::error_code ec, T1 t1, T2 t2, T3 t3)
232   { 255   {
HITCBC 233   1 return {{ec, std::move(t1), std::move(t2), std::move(t3)}}; 256   1 return {{ec, std::move(t1), std::move(t2), std::move(t3)}};
234   } 257   }
235   258  
236   } // namespace capy 259   } // namespace capy
237   } // namespace boost 260   } // namespace boost
238   261  
239   #endif 262   #endif