100.00% Lines (84/84) 100.00% Functions (18/18)
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_IO_ANY_READ_STREAM_HPP 11   #ifndef BOOST_CAPY_IO_ANY_READ_STREAM_HPP
12   #define BOOST_CAPY_IO_ANY_READ_STREAM_HPP 12   #define BOOST_CAPY_IO_ANY_READ_STREAM_HPP
13   13  
14   #include <boost/capy/detail/config.hpp> 14   #include <boost/capy/detail/config.hpp>
15   #include <boost/capy/detail/await_suspend_helper.hpp> 15   #include <boost/capy/detail/await_suspend_helper.hpp>
16   #include <boost/capy/buffers.hpp> 16   #include <boost/capy/buffers.hpp>
17   #include <boost/capy/detail/buffer_array.hpp> 17   #include <boost/capy/detail/buffer_array.hpp>
18   #include <boost/capy/concept/context_awaitable.hpp> 18   #include <boost/capy/concept/context_awaitable.hpp>
19   #include <boost/capy/concept/read_stream.hpp> 19   #include <boost/capy/concept/read_stream.hpp>
20   #include <boost/capy/ex/await_context.hpp> 20   #include <boost/capy/ex/await_context.hpp>
21   #include <boost/capy/results.hpp> 21   #include <boost/capy/results.hpp>
22   22  
23   #include <concepts> 23   #include <concepts>
24   #include <coroutine> 24   #include <coroutine>
25   #include <cstddef> 25   #include <cstddef>
26   #include <exception> 26   #include <exception>
27   #include <new> 27   #include <new>
28   #include <span> 28   #include <span>
29   #include <stop_token> 29   #include <stop_token>
30   #include <system_error> 30   #include <system_error>
31   #include <utility> 31   #include <utility>
32   32  
33   namespace boost { 33   namespace boost {
34   namespace capy { 34   namespace capy {
35   35  
36   /** Dispatches `read_some` through a type-erased vtable, using preallocated awaitable storage. 36   /** Dispatches `read_some` through a type-erased vtable, using preallocated awaitable storage.
37   37  
38   This class provides type erasure for any type satisfying the 38   This class provides type erasure for any type satisfying the
39   @ref ReadStream concept, enabling runtime polymorphism for 39   @ref ReadStream concept, enabling runtime polymorphism for
40   read operations. It uses cached awaitable storage to achieve 40   read operations. It uses cached awaitable storage to achieve
41   zero steady-state allocation after construction. 41   zero steady-state allocation after construction.
42   42  
43   The wrapper supports two construction modes: 43   The wrapper supports two construction modes:
44   - **Owning**: Pass by value to transfer ownership. The wrapper 44   - **Owning**: Pass by value to transfer ownership. The wrapper
45   allocates storage and owns the stream. 45   allocates storage and owns the stream.
46   - **Reference**: Pass a pointer to wrap without ownership. The 46   - **Reference**: Pass a pointer to wrap without ownership. The
47   pointed-to stream must outlive this wrapper. 47   pointed-to stream must outlive this wrapper.
48   48  
49   @par Awaitable Preallocation 49   @par Awaitable Preallocation
50 - The constructor preallocates storage for the type-erased awaitable. 50 + The constructor allocates the storage for the type-erased awaitable
51 - This reserves all virtual address space at server startup 51 + once, so operations do not allocate it piecemeal as traffic
52 - so memory usage can be measured up front, rather than 52 + arrives.
53 - allocating piecemeal as traffic arrives.  
54   53  
55   @par Immediate Completion 54   @par Immediate Completion
56   When the underlying stream's awaitable reports ready immediately 55   When the underlying stream's awaitable reports ready immediately
57   (e.g. buffered data already available), the wrapper skips 56   (e.g. buffered data already available), the wrapper skips
58   coroutine suspension entirely and returns the result inline. 57   coroutine suspension entirely and returns the result inline.
59   58  
60   @par Thread Safety 59   @par Thread Safety
61   Not thread-safe. Concurrent operations on the same wrapper 60   Not thread-safe. Concurrent operations on the same wrapper
62   are undefined behavior. 61   are undefined behavior.
63   62  
64   @par Example 63   @par Example
65   @par !example example 64   @par !example example
66   65  
67   66  
68   @see @ref any_write_stream, @ref any_stream, @ref ReadStream 67   @see @ref any_write_stream, @ref any_stream, @ref ReadStream
69   */ 68   */
70   class any_read_stream 69   class any_read_stream
71   { 70   {
72   struct vtable; 71   struct vtable;
73   72  
74   template<ReadStream S> 73   template<ReadStream S>
75   struct vtable_for_impl; 74   struct vtable_for_impl;
76   75  
77   // ordered for cache line coherence 76   // ordered for cache line coherence
78   void* stream_ = nullptr; 77   void* stream_ = nullptr;
79   vtable const* vt_ = nullptr; 78   vtable const* vt_ = nullptr;
80   void* cached_awaitable_ = nullptr; 79   void* cached_awaitable_ = nullptr;
81   void* storage_ = nullptr; 80   void* storage_ = nullptr;
82   bool awaitable_active_ = false; 81   bool awaitable_active_ = false;
83   // The flag of the awaitable whose operation is active, cleared 82   // The flag of the awaitable whose operation is active, cleared
84   // when the stream ends that operation itself. 83   // when the stream ends that operation itself.
85   bool* owner_ = nullptr; 84   bool* owner_ = nullptr;
86   85  
87   void 86   void
HITCBC 88   220 disown() noexcept 87   220 disown() noexcept
89   { 88   {
HITCBC 90   220 if(owner_) 89   220 if(owner_)
HITCBC 91   8 *std::exchange(owner_, nullptr) = false; 90   8 *std::exchange(owner_, nullptr) = false;
HITCBC 92   220 } 91   220 }
93   92  
94   public: 93   public:
95 - /** Destructor. 94 + /** Destroy the wrapper.
96   95  
97   Ends any operation in flight, then destroys the owned stream 96   Ends any operation in flight, then destroys the owned stream
98   (if any) and releases the cached awaitable storage. 97   (if any) and releases the cached awaitable storage.
99   */ 98   */
100   ~any_read_stream(); 99   ~any_read_stream();
101   100  
102   /** Construct a default instance. 101   /** Construct a default instance.
103   102  
104   Constructs an empty wrapper. @ref has_value and `operator bool` 103   Constructs an empty wrapper. @ref has_value and `operator bool`
105   report the empty state; calling @ref read_some before the 104   report the empty state; calling @ref read_some before the
106   wrapper holds a stream is undefined behavior. 105   wrapper holds a stream is undefined behavior.
107   */ 106   */
HITCBC 108   10 any_read_stream() = default; 107   10 any_read_stream() = default;
109   108  
110 - /** Non-copyable. 109 + /** Copy construction is disabled.
111   110  
112   The awaitable cache is per-instance and cannot be shared. 111   The awaitable cache is per-instance and cannot be shared.
113   112  
114   @param other The wrapper that would be copied. 113   @param other The wrapper that would be copied.
115   */ 114   */
116   any_read_stream(any_read_stream const& other) = delete; 115   any_read_stream(any_read_stream const& other) = delete;
117   116  
118   /** Copy assignment is disabled. 117   /** Copy assignment is disabled.
119   118  
120   The awaitable cache is per-instance and cannot be shared. 119   The awaitable cache is per-instance and cannot be shared.
121   120  
122   @param other The wrapper that would be assigned from. 121   @param other The wrapper that would be assigned from.
123   122  
124   @return A reference to `*this`. 123   @return A reference to `*this`.
125   */ 124   */
126   any_read_stream& operator=(any_read_stream const& other) = delete; 125   any_read_stream& operator=(any_read_stream const& other) = delete;
127   126  
128   /** Construct by moving. 127   /** Construct by moving.
129   128  
130   Transfers ownership of the wrapped stream (if owned) and 129   Transfers ownership of the wrapped stream (if owned) and
131   cached awaitable storage from `other`. After the move, `other` is 130   cached awaitable storage from `other`. After the move, `other` is
132   in a default-constructed state. 131   in a default-constructed state.
133   132  
134   @param other The wrapper to move from. 133   @param other The wrapper to move from.
135   */ 134   */
HITCBC 136   2 any_read_stream(any_read_stream&& other) noexcept 135   2 any_read_stream(any_read_stream&& other) noexcept
HITCBC 137   2 : stream_(std::exchange(other.stream_, nullptr)) 136   2 : stream_(std::exchange(other.stream_, nullptr))
HITCBC 138   2 , vt_(std::exchange(other.vt_, nullptr)) 137   2 , vt_(std::exchange(other.vt_, nullptr))
HITCBC 139   2 , cached_awaitable_(std::exchange(other.cached_awaitable_, nullptr)) 138   2 , cached_awaitable_(std::exchange(other.cached_awaitable_, nullptr))
HITCBC 140   2 , storage_(std::exchange(other.storage_, nullptr)) 139   2 , storage_(std::exchange(other.storage_, nullptr))
HITCBC 141   2 , awaitable_active_(std::exchange(other.awaitable_active_, false)) 140   2 , awaitable_active_(std::exchange(other.awaitable_active_, false))
142   { 141   {
HITCBC 143   2 other.disown(); 142   2 other.disown();
HITCBC 144   2 } 143   2 }
145   144  
146   /** Assign by moving. 145   /** Assign by moving.
147   146  
148   Destroys any owned stream and releases existing resources, 147   Destroys any owned stream and releases existing resources,
149   then transfers ownership from `other`. 148   then transfers ownership from `other`.
150   149  
151   @param other The wrapper to move from. 150   @param other The wrapper to move from.
152   @return Reference to this wrapper. 151   @return Reference to this wrapper.
153   */ 152   */
154   any_read_stream& 153   any_read_stream&
155   operator=(any_read_stream&& other) noexcept; 154   operator=(any_read_stream&& other) noexcept;
156   155  
157   /** Construct by taking ownership of a ReadStream. 156   /** Construct by taking ownership of a ReadStream.
158   157  
159   Allocates storage and moves the stream into this wrapper. 158   Allocates storage and moves the stream into this wrapper.
160   The wrapper owns the stream and destroys it. 159   The wrapper owns the stream and destroys it.
161   160  
  161 + @par Exception Safety
  162 + Strong guarantee. If an allocation throws, nothing is leaked.
  163 +
  164 + @throws std::bad_alloc If allocation fails.
  165 + @throws Any exception thrown by moving `s`.
  166 +
162   @param s The stream to take ownership of. 167   @param s The stream to take ownership of.
163   */ 168   */
164   template<ReadStream S> 169   template<ReadStream S>
165   requires (!std::same_as<std::decay_t<S>, any_read_stream>) 170   requires (!std::same_as<std::decay_t<S>, any_read_stream>)
166   any_read_stream(S s); 171   any_read_stream(S s);
167   172  
168   /** Construct by wrapping a ReadStream without ownership. 173   /** Construct by wrapping a ReadStream without ownership.
169   174  
170   Wraps the given stream by pointer. The stream must remain 175   Wraps the given stream by pointer. The stream must remain
171   valid for the lifetime of this wrapper. 176   valid for the lifetime of this wrapper.
172   177  
  178 + @throws std::bad_alloc If allocating the awaitable storage fails.
  179 +
173   @param s Pointer to the stream to wrap. 180   @param s Pointer to the stream to wrap.
174   */ 181   */
175   template<ReadStream S> 182   template<ReadStream S>
176   any_read_stream(S* s); 183   any_read_stream(S* s);
177   184  
178   /** Check if the wrapper contains a valid stream. 185   /** Check if the wrapper contains a valid stream.
179   186  
180   @return `true` if wrapping a stream, `false` if default-constructed 187   @return `true` if wrapping a stream, `false` if default-constructed
181   or moved-from. 188   or moved-from.
182   */ 189   */
183   bool 190   bool
HITCBC 184   31 has_value() const noexcept 191   31 has_value() const noexcept
185   { 192   {
HITCBC 186   31 return stream_ != nullptr; 193   31 return stream_ != nullptr;
187   } 194   }
188   195  
189   /** Check if the wrapper contains a valid stream. 196   /** Check if the wrapper contains a valid stream.
190   197  
191   @return `true` if wrapping a stream, `false` if default-constructed 198   @return `true` if wrapping a stream, `false` if default-constructed
192   or moved-from. 199   or moved-from.
193   */ 200   */
194   explicit 201   explicit
HITCBC 195   3 operator bool() const noexcept 202   3 operator bool() const noexcept
196   { 203   {
HITCBC 197   3 return has_value(); 204   3 return has_value();
198   } 205   }
199   206  
200 - /** Initiate an asynchronous read operation. 207 + /** Asynchronously read some data into the buffer sequence.
201   208  
202   Reads data into the provided buffer sequence. The operation 209   Reads data into the provided buffer sequence. The operation
203   completes when at least one byte is read, or an error 210   completes when at least one byte is read, or an error
204   occurs. 211   occurs.
205   212  
206 - @param buffers The buffer sequence to read into. Passed by 213 + @pre The wrapper must contain a valid stream (`has_value() == true`).
207 - value to ensure the sequence lives in the coroutine frame  
208 - across suspension points.  
209 -  
210 - @return An awaitable that await-returns `(error_code,std::size_t)`.  
211   214  
212   @par Immediate Completion 215   @par Immediate Completion
213   The operation completes immediately without suspending 216   The operation completes immediately without suspending
214   the calling coroutine when the underlying stream's 217   the calling coroutine when the underlying stream's
215   awaitable reports immediate readiness via `await_ready`. 218   awaitable reports immediate readiness via `await_ready`.
216   219  
  220 + @par After an Error
  221 + A subsequent call is permitted. The wrapper forwards directly
  222 + to the underlying stream, imposing no stricter rule than
  223 + @ref ReadStream.
  224 +
217   @note This is a partial operation and may not process the 225   @note This is a partial operation and may not process the
218   entire buffer sequence. Use the composed @ref read algorithm 226   entire buffer sequence. Use the composed @ref read algorithm
219   for guaranteed complete transfer. 227   for guaranteed complete transfer.
220   228  
221 - @par Preconditions 229 + @param buffers The buffer sequence to read into. Passed by
222 - The wrapper must contain a valid stream (`has_value() == true`). 230 + value to ensure the sequence lives in the coroutine frame
  231 + across suspension points.
223   232  
224 - @par After an Error 233 + @return An awaitable that await-returns `(error_code,std::size_t)`.
225 - A subsequent call is permitted. The wrapper forwards directly  
226 - to the underlying stream, imposing no stricter rule than  
227 - @ref ReadStream.  
228   */ 234   */
229   template<MutableBufferSequence MB> 235   template<MutableBufferSequence MB>
230   auto 236   auto
231   read_some(MB buffers); 237   read_some(MB buffers);
232   238  
233   protected: 239   protected:
234   /** End the operation in flight, if any. 240   /** End the operation in flight, if any.
235   241  
236   Destroys the wrapped stream's active operation, which may refer 242   Destroys the wrapped stream's active operation, which may refer
237   to the stream, so an owner must call this before destroying the 243   to the stream, so an owner must call this before destroying the
238   stream. The awaitable that started the operation no longer ends 244   stream. The awaitable that started the operation no longer ends
239   it. 245   it.
240   */ 246   */
241   void 247   void
242   end_operation() noexcept; 248   end_operation() noexcept;
243   249  
244   /** Rebind to a new stream after move. 250   /** Rebind to a new stream after move.
245   251  
246 - Updates the internal pointer to reference a new stream object. 252 + Updates the internal pointer to reference a new stream object,
247 - Used by owning wrappers after move assignment when the owned 253 + for use when the stream object has moved to a new location.
248 - object has moved to a new location.  
249   254  
250   @param new_stream The new stream to bind to. Must be the same 255   @param new_stream The new stream to bind to. Must be the same
251   type as the original stream. 256   type as the original stream.
252   257  
253   @note Terminates if called with a stream of different type 258   @note Terminates if called with a stream of different type
254   than the original. 259   than the original.
255   */ 260   */
256   template<ReadStream S> 261   template<ReadStream S>
257   void 262   void
258   rebind(S& new_stream) noexcept 263   rebind(S& new_stream) noexcept
259   { 264   {
260   if(vt_ != &vtable_for_impl<S>::value) 265   if(vt_ != &vtable_for_impl<S>::value)
261   std::terminate(); 266   std::terminate();
262   stream_ = &new_stream; 267   stream_ = &new_stream;
263   } 268   }
264   }; 269   };
265   270  
266   struct any_read_stream::vtable 271   struct any_read_stream::vtable
267   { 272   {
268   // ordered by call frequency for cache line coherence 273   // ordered by call frequency for cache line coherence
269   void (*construct_awaitable)( 274   void (*construct_awaitable)(
270   void* stream, 275   void* stream,
271   void* storage, 276   void* storage,
272   std::span<mutable_buffer const> buffers); 277   std::span<mutable_buffer const> buffers);
273   bool (*await_ready)(void*); 278   bool (*await_ready)(void*);
274   std::coroutine_handle<> (*await_suspend)(void*, std::coroutine_handle<>, await_context const*); 279   std::coroutine_handle<> (*await_suspend)(void*, std::coroutine_handle<>, await_context const*);
275   results<std::size_t> (*await_resume)(void*); 280   results<std::size_t> (*await_resume)(void*);
276   void (*destroy_awaitable)(void*) noexcept; 281   void (*destroy_awaitable)(void*) noexcept;
277   std::size_t awaitable_size; 282   std::size_t awaitable_size;
278   std::size_t awaitable_align; 283   std::size_t awaitable_align;
279   void (*destroy)(void*) noexcept; 284   void (*destroy)(void*) noexcept;
280   }; 285   };
281   286  
282   template<ReadStream S> 287   template<ReadStream S>
283   struct any_read_stream::vtable_for_impl 288   struct any_read_stream::vtable_for_impl
284   { 289   {
285   using Awaitable = decltype(std::declval<S&>().read_some( 290   using Awaitable = decltype(std::declval<S&>().read_some(
286   std::span<mutable_buffer const>{})); 291   std::span<mutable_buffer const>{}));
287   292  
288   static void 293   static void
HITCBC 289   6 do_destroy_impl(void* stream) noexcept 294   6 do_destroy_impl(void* stream) noexcept
290   { 295   {
HITCBC 291   6 static_cast<S*>(stream)->~S(); 296   6 static_cast<S*>(stream)->~S();
HITCBC 292   6 } 297   6 }
293   298  
294   static void 299   static void
HITCBC 295   108 construct_awaitable_impl( 300   108 construct_awaitable_impl(
296   void* stream, 301   void* stream,
297   void* storage, 302   void* storage,
298   std::span<mutable_buffer const> buffers) 303   std::span<mutable_buffer const> buffers)
299   { 304   {
HITCBC 300   108 auto& s = *static_cast<S*>(stream); 305   108 auto& s = *static_cast<S*>(stream);
HITCBC 301   108 ::new(storage) Awaitable(s.read_some(buffers)); 306   108 ::new(storage) Awaitable(s.read_some(buffers));
HITCBC 302   108 } 307   108 }
303   308  
304   static constexpr vtable value = { 309   static constexpr vtable value = {
305   &construct_awaitable_impl, 310   &construct_awaitable_impl,
HITCBC 306   108 +[](void* p) { 311   108 +[](void* p) {
HITCBC 307   108 return static_cast<Awaitable*>(p)->await_ready(); 312   108 return static_cast<Awaitable*>(p)->await_ready();
308   }, 313   },
HITCBC 309   84 +[](void* p, std::coroutine_handle<> h, await_context const* env) { 314   84 +[](void* p, std::coroutine_handle<> h, await_context const* env) {
HITCBC 310   84 return detail::call_await_suspend( 315   84 return detail::call_await_suspend(
HITCBC 311   84 static_cast<Awaitable*>(p), h, env); 316   84 static_cast<Awaitable*>(p), h, env);
312   }, 317   },
HITCBC 313   97 +[](void* p) { 318   97 +[](void* p) {
HITCBC 314   97 return static_cast<Awaitable*>(p)->await_resume(); 319   97 return static_cast<Awaitable*>(p)->await_resume();
315   }, 320   },
HITCBC 316   125 +[](void* p) noexcept { 321   125 +[](void* p) noexcept {
HITCBC 317   17 static_cast<Awaitable*>(p)->~Awaitable(); 322   17 static_cast<Awaitable*>(p)->~Awaitable();
318   }, 323   },
319   sizeof(Awaitable), 324   sizeof(Awaitable),
320   alignof(Awaitable), 325   alignof(Awaitable),
321   &do_destroy_impl 326   &do_destroy_impl
322   }; 327   };
323   }; 328   };
324   329  
325   inline void 330   inline void
HITCBC 326   198 any_read_stream::end_operation() noexcept 331   198 any_read_stream::end_operation() noexcept
327   { 332   {
HITCBC 328   198 disown(); 333   198 disown();
HITCBC 329   198 if(awaitable_active_) 334   198 if(awaitable_active_)
330   { 335   {
HITCBC 331   8 vt_->destroy_awaitable(cached_awaitable_); 336   8 vt_->destroy_awaitable(cached_awaitable_);
HITCBC 332   8 awaitable_active_ = false; 337   8 awaitable_active_ = false;
333   } 338   }
HITCBC 334   198 } 339   198 }
335   340  
336   inline 341   inline
HITCBC 337   135 any_read_stream::~any_read_stream() 342   135 any_read_stream::~any_read_stream()
338   { 343   {
HITCBC 339   135 end_operation(); 344   135 end_operation();
HITCBC 340   135 if(cached_awaitable_) 345   135 if(cached_awaitable_)
HITCBC 341   110 ::operator delete(cached_awaitable_); 346   110 ::operator delete(cached_awaitable_);
HITCBC 342   135 if(storage_) 347   135 if(storage_)
343   { 348   {
HITCBC 344   4 vt_->destroy(stream_); 349   4 vt_->destroy(stream_);
HITCBC 345   4 ::operator delete(storage_); 350   4 ::operator delete(storage_);
346   } 351   }
HITCBC 347   135 } 352   135 }
348   353  
349   inline any_read_stream& 354   inline any_read_stream&
HITCBC 350   20 any_read_stream::operator=(any_read_stream&& other) noexcept 355   20 any_read_stream::operator=(any_read_stream&& other) noexcept
351   { 356   {
HITCBC 352   20 if(this != &other) 357   20 if(this != &other)
353   { 358   {
HITCBC 354   20 end_operation(); 359   20 end_operation();
HITCBC 355   20 other.disown(); 360   20 other.disown();
HITCBC 356   20 if(cached_awaitable_) 361   20 if(cached_awaitable_)
HITCBC 357   8 ::operator delete(cached_awaitable_); 362   8 ::operator delete(cached_awaitable_);
HITCBC 358   20 if(storage_) 363   20 if(storage_)
359   { 364   {
HITCBC 360   2 vt_->destroy(stream_); 365   2 vt_->destroy(stream_);
HITCBC 361   2 ::operator delete(storage_); 366   2 ::operator delete(storage_);
362   } 367   }
HITCBC 363   20 stream_ = std::exchange(other.stream_, nullptr); 368   20 stream_ = std::exchange(other.stream_, nullptr);
HITCBC 364   20 vt_ = std::exchange(other.vt_, nullptr); 369   20 vt_ = std::exchange(other.vt_, nullptr);
HITCBC 365   20 cached_awaitable_ = std::exchange(other.cached_awaitable_, nullptr); 370   20 cached_awaitable_ = std::exchange(other.cached_awaitable_, nullptr);
HITCBC 366   20 storage_ = std::exchange(other.storage_, nullptr); 371   20 storage_ = std::exchange(other.storage_, nullptr);
HITCBC 367   20 awaitable_active_ = std::exchange(other.awaitable_active_, false); 372   20 awaitable_active_ = std::exchange(other.awaitable_active_, false);
368   } 373   }
HITCBC 369   20 return *this; 374   20 return *this;
370   } 375   }
371   376  
372   template<ReadStream S> 377   template<ReadStream S>
373   requires (!std::same_as<std::decay_t<S>, any_read_stream>) 378   requires (!std::same_as<std::decay_t<S>, any_read_stream>)
HITCBC 374   7 any_read_stream::any_read_stream(S s) 379   7 any_read_stream::any_read_stream(S s)
HITCBC 375   7 : vt_(&vtable_for_impl<S>::value) 380   7 : vt_(&vtable_for_impl<S>::value)
376   { 381   {
377   struct guard { 382   struct guard {
378   any_read_stream* self; 383   any_read_stream* self;
379   bool committed = false; 384   bool committed = false;
HITCBC 380   7 ~guard() { 385   7 ~guard() {
HITCBC 381   7 if(!committed && self->storage_) { 386   7 if(!committed && self->storage_) {
HITCBC 382   1 if(self->stream_) 387   1 if(self->stream_)
383   self->vt_->destroy(self->stream_); // LCOV_EXCL_LINE OOM rollback: only when the cached-awaitable allocation throws 388   self->vt_->destroy(self->stream_); // LCOV_EXCL_LINE OOM rollback: only when the cached-awaitable allocation throws
HITCBC 384   1 ::operator delete(self->storage_); 389   1 ::operator delete(self->storage_);
HITCBC 385   1 self->storage_ = nullptr; 390   1 self->storage_ = nullptr;
HITCBC 386   1 self->stream_ = nullptr; 391   1 self->stream_ = nullptr;
387   } 392   }
HITCBC 388   7 } 393   7 }
HITCBC 389   7 } g{this}; 394   7 } g{this};
390   395  
HITCBC 391   7 storage_ = ::operator new(sizeof(S)); 396   7 storage_ = ::operator new(sizeof(S));
HITCBC 392   7 stream_ = ::new(storage_) S(std::move(s)); 397   7 stream_ = ::new(storage_) S(std::move(s));
393   398  
394   // Preallocate the awaitable storage 399   // Preallocate the awaitable storage
HITCBC 395   6 cached_awaitable_ = ::operator new(vt_->awaitable_size); 400   6 cached_awaitable_ = ::operator new(vt_->awaitable_size);
396   401  
HITCBC 397   6 g.committed = true; 402   6 g.committed = true;
HITCBC 398   7 } 403   7 }
399   404  
400   template<ReadStream S> 405   template<ReadStream S>
HITCBC 401   112 any_read_stream::any_read_stream(S* s) 406   112 any_read_stream::any_read_stream(S* s)
HITCBC 402   112 : stream_(s) 407   112 : stream_(s)
HITCBC 403   112 , vt_(&vtable_for_impl<S>::value) 408   112 , vt_(&vtable_for_impl<S>::value)
404   { 409   {
405   // Preallocate the awaitable storage 410   // Preallocate the awaitable storage
HITCBC 406   112 cached_awaitable_ = ::operator new(vt_->awaitable_size); 411   112 cached_awaitable_ = ::operator new(vt_->awaitable_size);
HITCBC 407   112 } 412   112 }
408   413  
409   template<MutableBufferSequence MB> 414   template<MutableBufferSequence MB>
410   auto 415   auto
HITCBC 411   109 any_read_stream::read_some(MB buffers) 416   109 any_read_stream::read_some(MB buffers)
412   { 417   {
413   // VFALCO in theory, we could use if constexpr to detect a 418   // VFALCO in theory, we could use if constexpr to detect a
414   // span and then pass that through to read_some without the array 419   // span and then pass that through to read_some without the array
415   // LCOV_EXCL_START read_some awaitable: exercised by tests, but the 420   // LCOV_EXCL_START read_some awaitable: exercised by tests, but the
416   // coverage tooling reports its templated body uncovered per-instantiation 421   // coverage tooling reports its templated body uncovered per-instantiation
417   struct awaitable 422   struct awaitable
418   { 423   {
419   any_read_stream* self_; 424   any_read_stream* self_;
420   detail::mutable_buffer_array<detail::max_iovec_> ba_; 425   detail::mutable_buffer_array<detail::max_iovec_> ba_;
421   // Set while this awaitable, not another on the same stream, 426   // Set while this awaitable, not another on the same stream,
422   // holds the stream's inner awaitable. 427   // holds the stream's inner awaitable.
423   bool owns_ = false; 428   bool owns_ = false;
424   429  
425   // A chain unwound while suspended here never reaches 430   // A chain unwound while suspended here never reaches
426   // await_resume, so the inner awaitable is destroyed here. 431   // await_resume, so the inner awaitable is destroyed here.
427   ~awaitable() 432   ~awaitable()
428   { 433   {
429   if(owns_) 434   if(owns_)
430   { 435   {
431   self_->vt_->destroy_awaitable(self_->cached_awaitable_); 436   self_->vt_->destroy_awaitable(self_->cached_awaitable_);
432   self_->awaitable_active_ = false; 437   self_->awaitable_active_ = false;
433   self_->owner_ = nullptr; 438   self_->owner_ = nullptr;
434   } 439   }
435   } 440   }
436   441  
437   awaitable( 442   awaitable(
438   any_read_stream* self, 443   any_read_stream* self,
439   detail::mutable_buffer_array<detail::max_iovec_> ba) noexcept 444   detail::mutable_buffer_array<detail::max_iovec_> ba) noexcept
440   : self_(self) 445   : self_(self)
441   , ba_(ba) 446   , ba_(ba)
442   { 447   {
443   } 448   }
444   449  
445   awaitable(awaitable&& other) noexcept 450   awaitable(awaitable&& other) noexcept
446   : self_(std::exchange(other.self_, nullptr)) 451   : self_(std::exchange(other.self_, nullptr))
447   , ba_(other.ba_) 452   , ba_(other.ba_)
448   , owns_(std::exchange(other.owns_, false)) 453   , owns_(std::exchange(other.owns_, false))
449   { 454   {
450   if(owns_) 455   if(owns_)
451   self_->owner_ = &owns_; 456   self_->owner_ = &owns_;
452   } 457   }
453   458  
454   bool 459   bool
455   await_ready() 460   await_ready()
456   { 461   {
457   self_->vt_->construct_awaitable( 462   self_->vt_->construct_awaitable(
458   self_->stream_, 463   self_->stream_,
459   self_->cached_awaitable_, 464   self_->cached_awaitable_,
460   ba_.to_span()); 465   ba_.to_span());
461   self_->awaitable_active_ = true; 466   self_->awaitable_active_ = true;
462   owns_ = true; 467   owns_ = true;
463   self_->owner_ = &owns_; 468   self_->owner_ = &owns_;
464   469  
465   return self_->vt_->await_ready( 470   return self_->vt_->await_ready(
466   self_->cached_awaitable_); 471   self_->cached_awaitable_);
467   } 472   }
468   473  
469   std::coroutine_handle<> 474   std::coroutine_handle<>
470   await_suspend(std::coroutine_handle<> h, await_context const* env) 475   await_suspend(std::coroutine_handle<> h, await_context const* env)
471   { 476   {
472   return self_->vt_->await_suspend( 477   return self_->vt_->await_suspend(
473   self_->cached_awaitable_, h, env); 478   self_->cached_awaitable_, h, env);
474   } 479   }
475   480  
476   [[nodiscard]] results<std::size_t> 481   [[nodiscard]] results<std::size_t>
477   await_resume() 482   await_resume()
478   { 483   {
479   struct guard { 484   struct guard {
480   awaitable* a; 485   awaitable* a;
481   ~guard() { 486   ~guard() {
482   a->self_->vt_->destroy_awaitable( 487   a->self_->vt_->destroy_awaitable(
483   a->self_->cached_awaitable_); 488   a->self_->cached_awaitable_);
484   a->self_->awaitable_active_ = false; 489   a->self_->awaitable_active_ = false;
485   a->self_->owner_ = nullptr; 490   a->self_->owner_ = nullptr;
486   a->owns_ = false; 491   a->owns_ = false;
487   } 492   }
488   } g{this}; 493   } g{this};
489   return self_->vt_->await_resume( 494   return self_->vt_->await_resume(
490   self_->cached_awaitable_); 495   self_->cached_awaitable_);
491   } 496   }
492   }; 497   };
493   // LCOV_EXCL_STOP 498   // LCOV_EXCL_STOP
494   return awaitable{this, 499   return awaitable{this,
HITCBC 495   109 detail::mutable_buffer_array<detail::max_iovec_>(buffers)}; 500   109 detail::mutable_buffer_array<detail::max_iovec_>(buffers)};
496   } 501   }
497   502  
498   } // namespace capy 503   } // namespace capy
499   } // namespace boost 504   } // namespace boost
500   505  
501   #endif 506   #endif