1  
//
1  
//
2  
// Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com)
2  
// Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com)
3  
//
3  
//
4  
// Distributed under the Boost Software License, Version 1.0. (See accompanying
4  
// Distributed under the Boost Software License, Version 1.0. (See accompanying
5  
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5  
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6  
//
6  
//
7  
// Official repository: https://github.com/cppalliance/capy
7  
// Official repository: https://github.com/cppalliance/capy
8  
//
8  
//
9  

9  

10  
#ifndef BOOST_CAPY_IO_ANY_BUFFER_SINK_HPP
10  
#ifndef BOOST_CAPY_IO_ANY_BUFFER_SINK_HPP
11  
#define BOOST_CAPY_IO_ANY_BUFFER_SINK_HPP
11  
#define BOOST_CAPY_IO_ANY_BUFFER_SINK_HPP
12  

12  

13  
#include <boost/capy/detail/config.hpp>
13  
#include <boost/capy/detail/config.hpp>
14  
#include <boost/capy/detail/await_suspend_helper.hpp>
14  
#include <boost/capy/detail/await_suspend_helper.hpp>
15  
#include <boost/capy/buffers.hpp>
15  
#include <boost/capy/buffers.hpp>
16  
#include <boost/capy/buffers/buffer_copy.hpp>
16  
#include <boost/capy/buffers/buffer_copy.hpp>
17  
#include <boost/capy/buffers/buffer_param.hpp>
17  
#include <boost/capy/buffers/buffer_param.hpp>
18  
#include <boost/capy/concept/buffer_sink.hpp>
18  
#include <boost/capy/concept/buffer_sink.hpp>
19  
#include <boost/capy/concept/io_awaitable.hpp>
19  
#include <boost/capy/concept/io_awaitable.hpp>
20  
#include <boost/capy/concept/write_sink.hpp>
20  
#include <boost/capy/concept/write_sink.hpp>
21  
#include <boost/capy/ex/io_env.hpp>
21  
#include <boost/capy/ex/io_env.hpp>
22  
#include <boost/capy/io_result.hpp>
22  
#include <boost/capy/io_result.hpp>
23  
#include <boost/capy/io_task.hpp>
23  
#include <boost/capy/io_task.hpp>
24  

24  

25  
#include <concepts>
25  
#include <concepts>
26  
#include <coroutine>
26  
#include <coroutine>
27  
#include <cstddef>
27  
#include <cstddef>
28  
#include <exception>
28  
#include <exception>
29  
#include <new>
29  
#include <new>
30  
#include <span>
30  
#include <span>
31  
#include <stop_token>
31  
#include <stop_token>
32  
#include <system_error>
32  
#include <system_error>
33  
#include <utility>
33  
#include <utility>
34  

34  

35  
namespace boost {
35  
namespace boost {
36  
namespace capy {
36  
namespace capy {
37  

37  

38  
/** Type-erased wrapper for any BufferSink.
38  
/** Type-erased wrapper for any BufferSink.
39  

39  

40  
    This class provides type erasure for any type satisfying the
40  
    This class provides type erasure for any type satisfying the
41  
    @ref BufferSink concept, enabling runtime polymorphism for
41  
    @ref BufferSink concept, enabling runtime polymorphism for
42  
    buffer sink operations. It uses cached awaitable storage to achieve
42  
    buffer sink operations. It uses cached awaitable storage to achieve
43  
    zero steady-state allocation after construction.
43  
    zero steady-state allocation after construction.
44  

44  

45  
    The wrapper exposes two interfaces for producing data:
45  
    The wrapper exposes two interfaces for producing data:
46  
    the @ref BufferSink interface (`prepare`, `commit`, `commit_eof`)
46  
    the @ref BufferSink interface (`prepare`, `commit`, `commit_eof`)
47  
    and the @ref WriteSink interface (`write_some`, `write`,
47  
    and the @ref WriteSink interface (`write_some`, `write`,
48  
    `write_eof`). Choose the interface that matches how your data
48  
    `write_eof`). Choose the interface that matches how your data
49  
    is produced:
49  
    is produced:
50  

50  

51  
    @par Choosing an Interface
51  
    @par Choosing an Interface
52  

52  

53  
    Use the **BufferSink** interface when you are a generator that
53  
    Use the **BufferSink** interface when you are a generator that
54  
    produces data into externally-provided buffers. The sink owns
54  
    produces data into externally-provided buffers. The sink owns
55  
    the memory; you call @ref prepare to obtain writable buffers,
55  
    the memory; you call @ref prepare to obtain writable buffers,
56  
    fill them, then call @ref commit or @ref commit_eof.
56  
    fill them, then call @ref commit or @ref commit_eof.
57  

57  

58  
    Use the **WriteSink** interface when you already have buffers
58  
    Use the **WriteSink** interface when you already have buffers
59  
    containing the data to write:
59  
    containing the data to write:
60  
    - If the entire body is available up front, call
60  
    - If the entire body is available up front, call
61  
      @ref write_eof(buffers) to send everything atomically.
61  
      @ref write_eof(buffers) to send everything atomically.
62  
    - If data arrives incrementally, call @ref write or
62  
    - If data arrives incrementally, call @ref write or
63  
      @ref write_some in a loop, then @ref write_eof() when done.
63  
      @ref write_some in a loop, then @ref write_eof() when done.
64  
      Prefer `write` (complete) unless your streaming pattern
64  
      Prefer `write` (complete) unless your streaming pattern
65  
      benefits from partial writes via `write_some`.
65  
      benefits from partial writes via `write_some`.
66  

66  

67  
    If the wrapped type only satisfies @ref BufferSink, the
67  
    If the wrapped type only satisfies @ref BufferSink, the
68  
    @ref WriteSink operations are provided automatically.
68  
    @ref WriteSink operations are provided automatically.
69  

69  

70  
    @par Construction Modes
70  
    @par Construction Modes
71  

71  

72  
    - **Owning**: Pass by value to transfer ownership. The wrapper
72  
    - **Owning**: Pass by value to transfer ownership. The wrapper
73  
      allocates storage and owns the sink.
73  
      allocates storage and owns the sink.
74  
    - **Reference**: Pass a pointer to wrap without ownership. The
74  
    - **Reference**: Pass a pointer to wrap without ownership. The
75  
      pointed-to sink must outlive this wrapper.
75  
      pointed-to sink must outlive this wrapper.
76  

76  

77  
    @par Awaitable Preallocation
77  
    @par Awaitable Preallocation
78  
    The constructor preallocates storage for the type-erased awaitable.
78  
    The constructor preallocates storage for the type-erased awaitable.
79  
    This reserves all virtual address space at server startup
79  
    This reserves all virtual address space at server startup
80  
    so memory usage can be measured up front, rather than
80  
    so memory usage can be measured up front, rather than
81  
    allocating piecemeal as traffic arrives.
81  
    allocating piecemeal as traffic arrives.
82  

82  

83  
    @par Thread Safety
83  
    @par Thread Safety
84  
    Not thread-safe. Concurrent operations on the same wrapper
84  
    Not thread-safe. Concurrent operations on the same wrapper
85  
    are undefined behavior.
85  
    are undefined behavior.
86  

86  

87  
    @par Example
87  
    @par Example
88  
    @code
88  
    @code
89  
    // Owning - takes ownership of the sink
89  
    // Owning - takes ownership of the sink
90  
    any_buffer_sink abs(some_buffer_sink{args...});
90  
    any_buffer_sink abs(some_buffer_sink{args...});
91  

91  

92  
    // Reference - wraps without ownership
92  
    // Reference - wraps without ownership
93  
    some_buffer_sink sink;
93  
    some_buffer_sink sink;
94  
    any_buffer_sink abs(&sink);
94  
    any_buffer_sink abs(&sink);
95  

95  

96  
    // BufferSink interface: generate into callee-owned buffers
96  
    // BufferSink interface: generate into callee-owned buffers
97  
    mutable_buffer arr[16];
97  
    mutable_buffer arr[16];
98  
    auto bufs = abs.prepare(arr);
98  
    auto bufs = abs.prepare(arr);
99  
    // Write data into bufs[0..bufs.size())
99  
    // Write data into bufs[0..bufs.size())
100  
    auto [ec] = co_await abs.commit(bytes_written);
100  
    auto [ec] = co_await abs.commit(bytes_written);
101  
    auto [ec2] = co_await abs.commit_eof(0);
101  
    auto [ec2] = co_await abs.commit_eof(0);
102  

102  

103  
    // WriteSink interface: send caller-owned buffers
103  
    // WriteSink interface: send caller-owned buffers
104  
    auto [ec3, n] = co_await abs.write(make_buffer("hello", 5));
104  
    auto [ec3, n] = co_await abs.write(make_buffer("hello", 5));
105  
    auto [ec4] = co_await abs.write_eof();
105  
    auto [ec4] = co_await abs.write_eof();
106  

106  

107  
    // Or send everything at once
107  
    // Or send everything at once
108  
    auto [ec5, n2] = co_await abs.write_eof(
108  
    auto [ec5, n2] = co_await abs.write_eof(
109  
        make_buffer(body_data));
109  
        make_buffer(body_data));
110  
    @endcode
110  
    @endcode
111  

111  

112  
    @see any_buffer_source, BufferSink, WriteSink
112  
    @see any_buffer_source, BufferSink, WriteSink
113  
*/
113  
*/
114  
class any_buffer_sink
114  
class any_buffer_sink
115  
{
115  
{
116  
    struct vtable;
116  
    struct vtable;
117  
    struct awaitable_ops;
117  
    struct awaitable_ops;
118  
    struct write_awaitable_ops;
118  
    struct write_awaitable_ops;
119  

119  

120  
    template<BufferSink S>
120  
    template<BufferSink S>
121  
    struct vtable_for_impl;
121  
    struct vtable_for_impl;
122  

122  

123  
    // hot-path members first for cache locality
123  
    // hot-path members first for cache locality
124  
    void* sink_ = nullptr;
124  
    void* sink_ = nullptr;
125  
    vtable const* vt_ = nullptr;
125  
    vtable const* vt_ = nullptr;
126  
    void* cached_awaitable_ = nullptr;
126  
    void* cached_awaitable_ = nullptr;
127  
    awaitable_ops const* active_ops_ = nullptr;
127  
    awaitable_ops const* active_ops_ = nullptr;
128  
    write_awaitable_ops const* active_write_ops_ = nullptr;
128  
    write_awaitable_ops const* active_write_ops_ = nullptr;
129  
    void* storage_ = nullptr;
129  
    void* storage_ = nullptr;
130  

130  

131  
public:
131  
public:
132  
    /** Destructor.
132  
    /** Destructor.
133  

133  

134  
        Destroys the owned sink (if any) and releases the cached
134  
        Destroys the owned sink (if any) and releases the cached
135  
        awaitable storage.
135  
        awaitable storage.
136  
    */
136  
    */
137  
    ~any_buffer_sink();
137  
    ~any_buffer_sink();
138  

138  

139 -
    /** Default constructor.
139 +
    /** Construct a default instance.
140  

140  

141  
        Constructs an empty wrapper. Operations on a default-constructed
141  
        Constructs an empty wrapper. Operations on a default-constructed
142  
        wrapper result in undefined behavior.
142  
        wrapper result in undefined behavior.
143  
    */
143  
    */
144  
    any_buffer_sink() = default;
144  
    any_buffer_sink() = default;
145  

145  

146  
    /** Non-copyable.
146  
    /** Non-copyable.
147  

147  

148  
        The awaitable cache is per-instance and cannot be shared.
148  
        The awaitable cache is per-instance and cannot be shared.
149  
    */
149  
    */
150  
    any_buffer_sink(any_buffer_sink const&) = delete;
150  
    any_buffer_sink(any_buffer_sink const&) = delete;
151  
    any_buffer_sink& operator=(any_buffer_sink const&) = delete;
151  
    any_buffer_sink& operator=(any_buffer_sink const&) = delete;
152  

152  

153 -
    /** Move constructor.
153 +
    /** Construct by moving.
154  

154  

155  
        Transfers ownership of the wrapped sink (if owned) and
155  
        Transfers ownership of the wrapped sink (if owned) and
156  
        cached awaitable storage from `other`. After the move, `other` is
156  
        cached awaitable storage from `other`. After the move, `other` is
157  
        in a default-constructed state.
157  
        in a default-constructed state.
158  

158  

159  
        @param other The wrapper to move from.
159  
        @param other The wrapper to move from.
160  
    */
160  
    */
161  
    any_buffer_sink(any_buffer_sink&& other) noexcept
161  
    any_buffer_sink(any_buffer_sink&& other) noexcept
162  
        : sink_(std::exchange(other.sink_, nullptr))
162  
        : sink_(std::exchange(other.sink_, nullptr))
163  
        , vt_(std::exchange(other.vt_, nullptr))
163  
        , vt_(std::exchange(other.vt_, nullptr))
164  
        , cached_awaitable_(std::exchange(other.cached_awaitable_, nullptr))
164  
        , cached_awaitable_(std::exchange(other.cached_awaitable_, nullptr))
165  
        , active_ops_(std::exchange(other.active_ops_, nullptr))
165  
        , active_ops_(std::exchange(other.active_ops_, nullptr))
166  
        , active_write_ops_(std::exchange(other.active_write_ops_, nullptr))
166  
        , active_write_ops_(std::exchange(other.active_write_ops_, nullptr))
167  
        , storage_(std::exchange(other.storage_, nullptr))
167  
        , storage_(std::exchange(other.storage_, nullptr))
168  
    {
168  
    {
169  
    }
169  
    }
170  

170  

171 -
    /** Move assignment operator.
171 +
    /** Assign by moving.
172  

172  

173  
        Destroys any owned sink and releases existing resources,
173  
        Destroys any owned sink and releases existing resources,
174  
        then transfers ownership from `other`.
174  
        then transfers ownership from `other`.
175  

175  

176  
        @param other The wrapper to move from.
176  
        @param other The wrapper to move from.
177  
        @return Reference to this wrapper.
177  
        @return Reference to this wrapper.
178  
    */
178  
    */
179  
    any_buffer_sink&
179  
    any_buffer_sink&
180  
    operator=(any_buffer_sink&& other) noexcept;
180  
    operator=(any_buffer_sink&& other) noexcept;
181  

181  

182  
    /** Construct by taking ownership of a BufferSink.
182  
    /** Construct by taking ownership of a BufferSink.
183  

183  

184  
        Allocates storage and moves the sink into this wrapper.
184  
        Allocates storage and moves the sink into this wrapper.
185  
        The wrapper owns the sink and will destroy it. If `S` also
185  
        The wrapper owns the sink and will destroy it. If `S` also
186  
        satisfies @ref WriteSink, native write operations are
186  
        satisfies @ref WriteSink, native write operations are
187  
        forwarded through the virtual boundary.
187  
        forwarded through the virtual boundary.
188  

188  

189  
        @param s The sink to take ownership of.
189  
        @param s The sink to take ownership of.
190  
    */
190  
    */
191  
    template<BufferSink S>
191  
    template<BufferSink S>
192  
        requires (!std::same_as<std::decay_t<S>, any_buffer_sink>)
192  
        requires (!std::same_as<std::decay_t<S>, any_buffer_sink>)
193  
    any_buffer_sink(S s);
193  
    any_buffer_sink(S s);
194  

194  

195  
    /** Construct by wrapping a BufferSink without ownership.
195  
    /** Construct by wrapping a BufferSink without ownership.
196  

196  

197  
        Wraps the given sink by pointer. The sink must remain
197  
        Wraps the given sink by pointer. The sink must remain
198  
        valid for the lifetime of this wrapper. If `S` also
198  
        valid for the lifetime of this wrapper. If `S` also
199  
        satisfies @ref WriteSink, native write operations are
199  
        satisfies @ref WriteSink, native write operations are
200  
        forwarded through the virtual boundary.
200  
        forwarded through the virtual boundary.
201  

201  

202  
        @param s Pointer to the sink to wrap.
202  
        @param s Pointer to the sink to wrap.
203  
    */
203  
    */
204  
    template<BufferSink S>
204  
    template<BufferSink S>
205  
    any_buffer_sink(S* s);
205  
    any_buffer_sink(S* s);
206  

206  

207  
    /** Check if the wrapper contains a valid sink.
207  
    /** Check if the wrapper contains a valid sink.
208  

208  

209  
        @return `true` if wrapping a sink, `false` if default-constructed
209  
        @return `true` if wrapping a sink, `false` if default-constructed
210  
            or moved-from.
210  
            or moved-from.
211  
    */
211  
    */
212  
    bool
212  
    bool
213  
    has_value() const noexcept
213  
    has_value() const noexcept
214  
    {
214  
    {
215  
        return sink_ != nullptr;
215  
        return sink_ != nullptr;
216  
    }
216  
    }
217  

217  

218  
    /** Check if the wrapper contains a valid sink.
218  
    /** Check if the wrapper contains a valid sink.
219  

219  

220  
        @return `true` if wrapping a sink, `false` if default-constructed
220  
        @return `true` if wrapping a sink, `false` if default-constructed
221  
            or moved-from.
221  
            or moved-from.
222  
    */
222  
    */
223  
    explicit
223  
    explicit
224  
    operator bool() const noexcept
224  
    operator bool() const noexcept
225  
    {
225  
    {
226  
        return has_value();
226  
        return has_value();
227  
    }
227  
    }
228  

228  

229  
    /** Prepare writable buffers.
229  
    /** Prepare writable buffers.
230  

230  

231  
        Fills the provided span with mutable buffer descriptors
231  
        Fills the provided span with mutable buffer descriptors
232  
        pointing to the underlying sink's internal storage. This
232  
        pointing to the underlying sink's internal storage. This
233  
        operation is synchronous.
233  
        operation is synchronous.
234  

234  

235  
        @param dest Span of mutable_buffer to fill.
235  
        @param dest Span of mutable_buffer to fill.
236  

236  

237  
        @return A span of filled buffers.
237  
        @return A span of filled buffers.
238  

238  

239  
        @par Preconditions
239  
        @par Preconditions
240  
        The wrapper must contain a valid sink (`has_value() == true`).
240  
        The wrapper must contain a valid sink (`has_value() == true`).
241  
    */
241  
    */
242  
    std::span<mutable_buffer>
242  
    std::span<mutable_buffer>
243  
    prepare(std::span<mutable_buffer> dest);
243  
    prepare(std::span<mutable_buffer> dest);
244  

244  

245  
    /** Commit bytes written to the prepared buffers.
245  
    /** Commit bytes written to the prepared buffers.
246  

246  

247  
        Commits `n` bytes written to the buffers returned by the
247  
        Commits `n` bytes written to the buffers returned by the
248  
        most recent call to @ref prepare. The operation may trigger
248  
        most recent call to @ref prepare. The operation may trigger
249  
        underlying I/O.
249  
        underlying I/O.
250  

250  

251  
        @param n The number of bytes to commit.
251  
        @param n The number of bytes to commit.
252  

252  

253  
        @return An awaitable yielding `(error_code)`.
253  
        @return An awaitable yielding `(error_code)`.
254  

254  

255  
        @par Preconditions
255  
        @par Preconditions
256  
        The wrapper must contain a valid sink (`has_value() == true`).
256  
        The wrapper must contain a valid sink (`has_value() == true`).
257  
    */
257  
    */
258  
    auto
258  
    auto
259  
    commit(std::size_t n);
259  
    commit(std::size_t n);
260  

260  

261  
    /** Commit final bytes and signal end-of-stream.
261  
    /** Commit final bytes and signal end-of-stream.
262  

262  

263  
        Commits `n` bytes written to the buffers returned by the
263  
        Commits `n` bytes written to the buffers returned by the
264  
        most recent call to @ref prepare and finalizes the sink.
264  
        most recent call to @ref prepare and finalizes the sink.
265  
        After success, no further operations are permitted.
265  
        After success, no further operations are permitted.
266  

266  

267  
        @param n The number of bytes to commit.
267  
        @param n The number of bytes to commit.
268  

268  

269  
        @return An awaitable yielding `(error_code)`.
269  
        @return An awaitable yielding `(error_code)`.
270  

270  

271  
        @par Preconditions
271  
        @par Preconditions
272  
        The wrapper must contain a valid sink (`has_value() == true`).
272  
        The wrapper must contain a valid sink (`has_value() == true`).
273  
    */
273  
    */
274  
    auto
274  
    auto
275  
    commit_eof(std::size_t n);
275  
    commit_eof(std::size_t n);
276  

276  

277  
    /** Write some data from a buffer sequence.
277  
    /** Write some data from a buffer sequence.
278  

278  

279  
        Writes one or more bytes from the buffer sequence to the
279  
        Writes one or more bytes from the buffer sequence to the
280  
        underlying sink. May consume less than the full sequence.
280  
        underlying sink. May consume less than the full sequence.
281  

281  

282  
        When the wrapped type provides native @ref WriteSink support,
282  
        When the wrapped type provides native @ref WriteSink support,
283  
        the operation forwards directly. Otherwise it is synthesized
283  
        the operation forwards directly. Otherwise it is synthesized
284  
        from @ref prepare and @ref commit with a buffer copy.
284  
        from @ref prepare and @ref commit with a buffer copy.
285  

285  

286  
        @param buffers The buffer sequence to write.
286  
        @param buffers The buffer sequence to write.
287  

287  

288  
        @return An awaitable yielding `(error_code,std::size_t)`.
288  
        @return An awaitable yielding `(error_code,std::size_t)`.
289  

289  

290  
        @par Preconditions
290  
        @par Preconditions
291  
        The wrapper must contain a valid sink (`has_value() == true`).
291  
        The wrapper must contain a valid sink (`has_value() == true`).
292  
    */
292  
    */
293  
    template<ConstBufferSequence CB>
293  
    template<ConstBufferSequence CB>
294  
    io_task<std::size_t>
294  
    io_task<std::size_t>
295  
    write_some(CB buffers);
295  
    write_some(CB buffers);
296  

296  

297  
    /** Write all data from a buffer sequence.
297  
    /** Write all data from a buffer sequence.
298  

298  

299  
        Writes all data from the buffer sequence to the underlying
299  
        Writes all data from the buffer sequence to the underlying
300  
        sink. This method satisfies the @ref WriteSink concept.
300  
        sink. This method satisfies the @ref WriteSink concept.
301  

301  

302  
        When the wrapped type provides native @ref WriteSink support,
302  
        When the wrapped type provides native @ref WriteSink support,
303  
        each window is forwarded directly. Otherwise the data is
303  
        each window is forwarded directly. Otherwise the data is
304  
        copied into the sink via @ref prepare and @ref commit.
304  
        copied into the sink via @ref prepare and @ref commit.
305  

305  

306  
        @param buffers The buffer sequence to write.
306  
        @param buffers The buffer sequence to write.
307  

307  

308  
        @return An awaitable yielding `(error_code,std::size_t)`.
308  
        @return An awaitable yielding `(error_code,std::size_t)`.
309  

309  

310  
        @par Preconditions
310  
        @par Preconditions
311  
        The wrapper must contain a valid sink (`has_value() == true`).
311  
        The wrapper must contain a valid sink (`has_value() == true`).
312  
    */
312  
    */
313  
    template<ConstBufferSequence CB>
313  
    template<ConstBufferSequence CB>
314  
    io_task<std::size_t>
314  
    io_task<std::size_t>
315  
    write(CB buffers);
315  
    write(CB buffers);
316  

316  

317  
    /** Atomically write data and signal end-of-stream.
317  
    /** Atomically write data and signal end-of-stream.
318  

318  

319  
        Writes all data from the buffer sequence to the underlying
319  
        Writes all data from the buffer sequence to the underlying
320  
        sink and then signals end-of-stream.
320  
        sink and then signals end-of-stream.
321  

321  

322  
        When the wrapped type provides native @ref WriteSink support,
322  
        When the wrapped type provides native @ref WriteSink support,
323  
        the final window is sent atomically via the underlying
323  
        the final window is sent atomically via the underlying
324  
        `write_eof(buffers)`. Otherwise the data is synthesized
324  
        `write_eof(buffers)`. Otherwise the data is synthesized
325  
        through @ref prepare, @ref commit, and @ref commit_eof.
325  
        through @ref prepare, @ref commit, and @ref commit_eof.
326  

326  

327  
        @param buffers The buffer sequence to write.
327  
        @param buffers The buffer sequence to write.
328  

328  

329  
        @return An awaitable yielding `(error_code,std::size_t)`.
329  
        @return An awaitable yielding `(error_code,std::size_t)`.
330  

330  

331  
        @par Preconditions
331  
        @par Preconditions
332  
        The wrapper must contain a valid sink (`has_value() == true`).
332  
        The wrapper must contain a valid sink (`has_value() == true`).
333  
    */
333  
    */
334  
    template<ConstBufferSequence CB>
334  
    template<ConstBufferSequence CB>
335  
    io_task<std::size_t>
335  
    io_task<std::size_t>
336  
    write_eof(CB buffers);
336  
    write_eof(CB buffers);
337  

337  

338  
    /** Signal end-of-stream.
338  
    /** Signal end-of-stream.
339  

339  

340  
        Indicates that no more data will be written to the sink.
340  
        Indicates that no more data will be written to the sink.
341  
        This method satisfies the @ref WriteSink concept.
341  
        This method satisfies the @ref WriteSink concept.
342  

342  

343  
        When the wrapped type provides native @ref WriteSink support,
343  
        When the wrapped type provides native @ref WriteSink support,
344  
        the underlying `write_eof()` is called. Otherwise the
344  
        the underlying `write_eof()` is called. Otherwise the
345  
        operation is implemented as `commit_eof(0)`.
345  
        operation is implemented as `commit_eof(0)`.
346  

346  

347  
        @return An awaitable yielding `(error_code)`.
347  
        @return An awaitable yielding `(error_code)`.
348  

348  

349  
        @par Preconditions
349  
        @par Preconditions
350  
        The wrapper must contain a valid sink (`has_value() == true`).
350  
        The wrapper must contain a valid sink (`has_value() == true`).
351  
    */
351  
    */
352  
    auto
352  
    auto
353  
    write_eof();
353  
    write_eof();
354  

354  

355  
protected:
355  
protected:
356  
    /** Rebind to a new sink after move.
356  
    /** Rebind to a new sink after move.
357  

357  

358  
        Updates the internal pointer to reference a new sink object.
358  
        Updates the internal pointer to reference a new sink object.
359  
        Used by owning wrappers after move assignment when the owned
359  
        Used by owning wrappers after move assignment when the owned
360  
        object has moved to a new location.
360  
        object has moved to a new location.
361  

361  

362  
        @param new_sink The new sink to bind to. Must be the same
362  
        @param new_sink The new sink to bind to. Must be the same
363  
            type as the original sink.
363  
            type as the original sink.
364  

364  

365  
        @note Terminates if called with a sink of different type
365  
        @note Terminates if called with a sink of different type
366  
            than the original.
366  
            than the original.
367  
    */
367  
    */
368  
    template<BufferSink S>
368  
    template<BufferSink S>
369  
    void
369  
    void
370  
    rebind(S& new_sink) noexcept
370  
    rebind(S& new_sink) noexcept
371  
    {
371  
    {
372  
        if(vt_ != &vtable_for_impl<S>::value)
372  
        if(vt_ != &vtable_for_impl<S>::value)
373  
            std::terminate();
373  
            std::terminate();
374  
        sink_ = &new_sink;
374  
        sink_ = &new_sink;
375  
    }
375  
    }
376  

376  

377  
private:
377  
private:
378  
    /** Forward a partial write through the vtable.
378  
    /** Forward a partial write through the vtable.
379  

379  

380  
        Constructs the underlying `write_some` awaitable in
380  
        Constructs the underlying `write_some` awaitable in
381  
        cached storage and returns a type-erased awaitable.
381  
        cached storage and returns a type-erased awaitable.
382  
    */
382  
    */
383  
    auto
383  
    auto
384  
    write_some_(std::span<const_buffer const> buffers);
384  
    write_some_(std::span<const_buffer const> buffers);
385  

385  

386  
    /** Forward a complete write through the vtable.
386  
    /** Forward a complete write through the vtable.
387  

387  

388  
        Constructs the underlying `write` awaitable in
388  
        Constructs the underlying `write` awaitable in
389  
        cached storage and returns a type-erased awaitable.
389  
        cached storage and returns a type-erased awaitable.
390  
    */
390  
    */
391  
    auto
391  
    auto
392  
    write_(std::span<const_buffer const> buffers);
392  
    write_(std::span<const_buffer const> buffers);
393  

393  

394  
    /** Forward an atomic write-with-EOF through the vtable.
394  
    /** Forward an atomic write-with-EOF through the vtable.
395  

395  

396  
        Constructs the underlying `write_eof(buffers)` awaitable
396  
        Constructs the underlying `write_eof(buffers)` awaitable
397  
        in cached storage and returns a type-erased awaitable.
397  
        in cached storage and returns a type-erased awaitable.
398  
    */
398  
    */
399  
    auto
399  
    auto
400  
    write_eof_buffers_(std::span<const_buffer const> buffers);
400  
    write_eof_buffers_(std::span<const_buffer const> buffers);
401  
};
401  
};
402 -
//----------------------------------------------------------
 
403 -

 
404  

402  

405  
/** Type-erased ops for awaitables yielding `io_result<>`. */
403  
/** Type-erased ops for awaitables yielding `io_result<>`. */
406  
struct any_buffer_sink::awaitable_ops
404  
struct any_buffer_sink::awaitable_ops
407  
{
405  
{
408  
    bool (*await_ready)(void*);
406  
    bool (*await_ready)(void*);
409  
    std::coroutine_handle<> (*await_suspend)(void*, std::coroutine_handle<>, io_env const*);
407  
    std::coroutine_handle<> (*await_suspend)(void*, std::coroutine_handle<>, io_env const*);
410  
    io_result<> (*await_resume)(void*);
408  
    io_result<> (*await_resume)(void*);
411  
    void (*destroy)(void*) noexcept;
409  
    void (*destroy)(void*) noexcept;
412  
};
410  
};
413  

411  

414  
/** Type-erased ops for awaitables yielding `io_result<std::size_t>`. */
412  
/** Type-erased ops for awaitables yielding `io_result<std::size_t>`. */
415  
struct any_buffer_sink::write_awaitable_ops
413  
struct any_buffer_sink::write_awaitable_ops
416  
{
414  
{
417  
    bool (*await_ready)(void*);
415  
    bool (*await_ready)(void*);
418  
    std::coroutine_handle<> (*await_suspend)(void*, std::coroutine_handle<>, io_env const*);
416  
    std::coroutine_handle<> (*await_suspend)(void*, std::coroutine_handle<>, io_env const*);
419  
    io_result<std::size_t> (*await_resume)(void*);
417  
    io_result<std::size_t> (*await_resume)(void*);
420  
    void (*destroy)(void*) noexcept;
418  
    void (*destroy)(void*) noexcept;
421  
};
419  
};
422  

420  

423  
struct any_buffer_sink::vtable
421  
struct any_buffer_sink::vtable
424  
{
422  
{
425  
    void (*destroy)(void*) noexcept;
423  
    void (*destroy)(void*) noexcept;
426  
    std::span<mutable_buffer> (*do_prepare)(
424  
    std::span<mutable_buffer> (*do_prepare)(
427  
        void* sink,
425  
        void* sink,
428  
        std::span<mutable_buffer> dest);
426  
        std::span<mutable_buffer> dest);
429  
    std::size_t awaitable_size;
427  
    std::size_t awaitable_size;
430  
    std::size_t awaitable_align;
428  
    std::size_t awaitable_align;
431  
    awaitable_ops const* (*construct_commit_awaitable)(
429  
    awaitable_ops const* (*construct_commit_awaitable)(
432  
        void* sink,
430  
        void* sink,
433  
        void* storage,
431  
        void* storage,
434  
        std::size_t n);
432  
        std::size_t n);
435  
    awaitable_ops const* (*construct_commit_eof_awaitable)(
433  
    awaitable_ops const* (*construct_commit_eof_awaitable)(
436  
        void* sink,
434  
        void* sink,
437  
        void* storage,
435  
        void* storage,
438  
        std::size_t n);
436  
        std::size_t n);
439  

437  

440  
    // WriteSink forwarding (null when wrapped type is BufferSink-only)
438  
    // WriteSink forwarding (null when wrapped type is BufferSink-only)
441  
    write_awaitable_ops const* (*construct_write_some_awaitable)(
439  
    write_awaitable_ops const* (*construct_write_some_awaitable)(
442  
        void* sink,
440  
        void* sink,
443  
        void* storage,
441  
        void* storage,
444  
        std::span<const_buffer const> buffers);
442  
        std::span<const_buffer const> buffers);
445  
    write_awaitable_ops const* (*construct_write_awaitable)(
443  
    write_awaitable_ops const* (*construct_write_awaitable)(
446  
        void* sink,
444  
        void* sink,
447  
        void* storage,
445  
        void* storage,
448  
        std::span<const_buffer const> buffers);
446  
        std::span<const_buffer const> buffers);
449  
    write_awaitable_ops const* (*construct_write_eof_buffers_awaitable)(
447  
    write_awaitable_ops const* (*construct_write_eof_buffers_awaitable)(
450  
        void* sink,
448  
        void* sink,
451  
        void* storage,
449  
        void* storage,
452  
        std::span<const_buffer const> buffers);
450  
        std::span<const_buffer const> buffers);
453  
    awaitable_ops const* (*construct_write_eof_awaitable)(
451  
    awaitable_ops const* (*construct_write_eof_awaitable)(
454  
        void* sink,
452  
        void* sink,
455  
        void* storage);
453  
        void* storage);
456  
};
454  
};
457  

455  

458  
template<BufferSink S>
456  
template<BufferSink S>
459  
struct any_buffer_sink::vtable_for_impl
457  
struct any_buffer_sink::vtable_for_impl
460  
{
458  
{
461  
    using CommitAwaitable = decltype(std::declval<S&>().commit(
459  
    using CommitAwaitable = decltype(std::declval<S&>().commit(
462  
        std::size_t{}));
460  
        std::size_t{}));
463  
    using CommitEofAwaitable = decltype(std::declval<S&>().commit_eof(
461  
    using CommitEofAwaitable = decltype(std::declval<S&>().commit_eof(
464  
        std::size_t{}));
462  
        std::size_t{}));
465  

463  

466  
    static void
464  
    static void
467  
    do_destroy_impl(void* sink) noexcept
465  
    do_destroy_impl(void* sink) noexcept
468  
    {
466  
    {
469  
        static_cast<S*>(sink)->~S();
467  
        static_cast<S*>(sink)->~S();
470  
    }
468  
    }
471  

469  

472  
    static std::span<mutable_buffer>
470  
    static std::span<mutable_buffer>
473  
    do_prepare_impl(
471  
    do_prepare_impl(
474  
        void* sink,
472  
        void* sink,
475  
        std::span<mutable_buffer> dest)
473  
        std::span<mutable_buffer> dest)
476  
    {
474  
    {
477  
        auto& s = *static_cast<S*>(sink);
475  
        auto& s = *static_cast<S*>(sink);
478  
        return s.prepare(dest);
476  
        return s.prepare(dest);
479  
    }
477  
    }
480  

478  

481  
    static awaitable_ops const*
479  
    static awaitable_ops const*
482  
    construct_commit_awaitable_impl(
480  
    construct_commit_awaitable_impl(
483  
        void* sink,
481  
        void* sink,
484  
        void* storage,
482  
        void* storage,
485  
        std::size_t n)
483  
        std::size_t n)
486  
    {
484  
    {
487  
        auto& s = *static_cast<S*>(sink);
485  
        auto& s = *static_cast<S*>(sink);
488  
        ::new(storage) CommitAwaitable(s.commit(n));
486  
        ::new(storage) CommitAwaitable(s.commit(n));
489  

487  

490  
        static constexpr awaitable_ops ops = {
488  
        static constexpr awaitable_ops ops = {
491  
            +[](void* p) {
489  
            +[](void* p) {
492  
                return static_cast<CommitAwaitable*>(p)->await_ready();
490  
                return static_cast<CommitAwaitable*>(p)->await_ready();
493  
            },
491  
            },
494  
            +[](void* p, std::coroutine_handle<> h, io_env const* env) {
492  
            +[](void* p, std::coroutine_handle<> h, io_env const* env) {
495  
                return detail::call_await_suspend(
493  
                return detail::call_await_suspend(
496  
                    static_cast<CommitAwaitable*>(p), h, env);
494  
                    static_cast<CommitAwaitable*>(p), h, env);
497  
            },
495  
            },
498  
            +[](void* p) {
496  
            +[](void* p) {
499  
                return static_cast<CommitAwaitable*>(p)->await_resume();
497  
                return static_cast<CommitAwaitable*>(p)->await_resume();
500  
            },
498  
            },
501  
            +[](void* p) noexcept {
499  
            +[](void* p) noexcept {
502  
                static_cast<CommitAwaitable*>(p)->~CommitAwaitable();
500  
                static_cast<CommitAwaitable*>(p)->~CommitAwaitable();
503  
            }
501  
            }
504  
        };
502  
        };
505  
        return &ops;
503  
        return &ops;
506  
    }
504  
    }
507  

505  

508  
    static awaitable_ops const*
506  
    static awaitable_ops const*
509  
    construct_commit_eof_awaitable_impl(
507  
    construct_commit_eof_awaitable_impl(
510  
        void* sink,
508  
        void* sink,
511  
        void* storage,
509  
        void* storage,
512  
        std::size_t n)
510  
        std::size_t n)
513  
    {
511  
    {
514  
        auto& s = *static_cast<S*>(sink);
512  
        auto& s = *static_cast<S*>(sink);
515  
        ::new(storage) CommitEofAwaitable(s.commit_eof(n));
513  
        ::new(storage) CommitEofAwaitable(s.commit_eof(n));
516  

514  

517  
        static constexpr awaitable_ops ops = {
515  
        static constexpr awaitable_ops ops = {
518  
            +[](void* p) {
516  
            +[](void* p) {
519  
                return static_cast<CommitEofAwaitable*>(p)->await_ready();
517  
                return static_cast<CommitEofAwaitable*>(p)->await_ready();
520  
            },
518  
            },
521  
            +[](void* p, std::coroutine_handle<> h, io_env const* env) {
519  
            +[](void* p, std::coroutine_handle<> h, io_env const* env) {
522  
                return detail::call_await_suspend(
520  
                return detail::call_await_suspend(
523  
                    static_cast<CommitEofAwaitable*>(p), h, env);
521  
                    static_cast<CommitEofAwaitable*>(p), h, env);
524  
            },
522  
            },
525  
            +[](void* p) {
523  
            +[](void* p) {
526  
                return static_cast<CommitEofAwaitable*>(p)->await_resume();
524  
                return static_cast<CommitEofAwaitable*>(p)->await_resume();
527  
            },
525  
            },
528  
            +[](void* p) noexcept {
526  
            +[](void* p) noexcept {
529  
                static_cast<CommitEofAwaitable*>(p)->~CommitEofAwaitable();
527  
                static_cast<CommitEofAwaitable*>(p)->~CommitEofAwaitable();
530  
            }
528  
            }
531  
        };
529  
        };
532  
        return &ops;
530  
        return &ops;
533  
    }
531  
    }
534 -
    //------------------------------------------------------
 
535 -
    // WriteSink forwarding (only instantiated when WriteSink<S>)
 
536 -

 
537  

532  

538  
    static write_awaitable_ops const*
533  
    static write_awaitable_ops const*
539  
    construct_write_some_awaitable_impl(
534  
    construct_write_some_awaitable_impl(
540  
        void* sink,
535  
        void* sink,
541  
        void* storage,
536  
        void* storage,
542  
        std::span<const_buffer const> buffers)
537  
        std::span<const_buffer const> buffers)
543  
        requires WriteSink<S>
538  
        requires WriteSink<S>
544  
    {
539  
    {
545  
        using Aw = decltype(std::declval<S&>().write_some(
540  
        using Aw = decltype(std::declval<S&>().write_some(
546  
            std::span<const_buffer const>{}));
541  
            std::span<const_buffer const>{}));
547  
        auto& s = *static_cast<S*>(sink);
542  
        auto& s = *static_cast<S*>(sink);
548  
        ::new(storage) Aw(s.write_some(buffers));
543  
        ::new(storage) Aw(s.write_some(buffers));
549  

544  

550  
        static constexpr write_awaitable_ops ops = {
545  
        static constexpr write_awaitable_ops ops = {
551  
            +[](void* p) {
546  
            +[](void* p) {
552  
                return static_cast<Aw*>(p)->await_ready();
547  
                return static_cast<Aw*>(p)->await_ready();
553  
            },
548  
            },
554  
            +[](void* p, std::coroutine_handle<> h, io_env const* env) {
549  
            +[](void* p, std::coroutine_handle<> h, io_env const* env) {
555  
                return detail::call_await_suspend(
550  
                return detail::call_await_suspend(
556  
                    static_cast<Aw*>(p), h, env);
551  
                    static_cast<Aw*>(p), h, env);
557  
            },
552  
            },
558  
            +[](void* p) {
553  
            +[](void* p) {
559  
                return static_cast<Aw*>(p)->await_resume();
554  
                return static_cast<Aw*>(p)->await_resume();
560  
            },
555  
            },
561  
            +[](void* p) noexcept {
556  
            +[](void* p) noexcept {
562  
                static_cast<Aw*>(p)->~Aw();
557  
                static_cast<Aw*>(p)->~Aw();
563  
            }
558  
            }
564  
        };
559  
        };
565  
        return &ops;
560  
        return &ops;
566  
    }
561  
    }
567  

562  

568  
    static write_awaitable_ops const*
563  
    static write_awaitable_ops const*
569  
    construct_write_awaitable_impl(
564  
    construct_write_awaitable_impl(
570  
        void* sink,
565  
        void* sink,
571  
        void* storage,
566  
        void* storage,
572  
        std::span<const_buffer const> buffers)
567  
        std::span<const_buffer const> buffers)
573  
        requires WriteSink<S>
568  
        requires WriteSink<S>
574  
    {
569  
    {
575  
        using Aw = decltype(std::declval<S&>().write(
570  
        using Aw = decltype(std::declval<S&>().write(
576  
            std::span<const_buffer const>{}));
571  
            std::span<const_buffer const>{}));
577  
        auto& s = *static_cast<S*>(sink);
572  
        auto& s = *static_cast<S*>(sink);
578  
        ::new(storage) Aw(s.write(buffers));
573  
        ::new(storage) Aw(s.write(buffers));
579  

574  

580  
        static constexpr write_awaitable_ops ops = {
575  
        static constexpr write_awaitable_ops ops = {
581  
            +[](void* p) {
576  
            +[](void* p) {
582  
                return static_cast<Aw*>(p)->await_ready();
577  
                return static_cast<Aw*>(p)->await_ready();
583  
            },
578  
            },
584  
            +[](void* p, std::coroutine_handle<> h, io_env const* env) {
579  
            +[](void* p, std::coroutine_handle<> h, io_env const* env) {
585  
                return detail::call_await_suspend(
580  
                return detail::call_await_suspend(
586  
                    static_cast<Aw*>(p), h, env);
581  
                    static_cast<Aw*>(p), h, env);
587  
            },
582  
            },
588  
            +[](void* p) {
583  
            +[](void* p) {
589  
                return static_cast<Aw*>(p)->await_resume();
584  
                return static_cast<Aw*>(p)->await_resume();
590  
            },
585  
            },
591  
            +[](void* p) noexcept {
586  
            +[](void* p) noexcept {
592  
                static_cast<Aw*>(p)->~Aw();
587  
                static_cast<Aw*>(p)->~Aw();
593  
            }
588  
            }
594  
        };
589  
        };
595  
        return &ops;
590  
        return &ops;
596  
    }
591  
    }
597  

592  

598  
    static write_awaitable_ops const*
593  
    static write_awaitable_ops const*
599  
    construct_write_eof_buffers_awaitable_impl(
594  
    construct_write_eof_buffers_awaitable_impl(
600  
        void* sink,
595  
        void* sink,
601  
        void* storage,
596  
        void* storage,
602  
        std::span<const_buffer const> buffers)
597  
        std::span<const_buffer const> buffers)
603  
        requires WriteSink<S>
598  
        requires WriteSink<S>
604  
    {
599  
    {
605  
        using Aw = decltype(std::declval<S&>().write_eof(
600  
        using Aw = decltype(std::declval<S&>().write_eof(
606  
            std::span<const_buffer const>{}));
601  
            std::span<const_buffer const>{}));
607  
        auto& s = *static_cast<S*>(sink);
602  
        auto& s = *static_cast<S*>(sink);
608  
        ::new(storage) Aw(s.write_eof(buffers));
603  
        ::new(storage) Aw(s.write_eof(buffers));
609  

604  

610  
        static constexpr write_awaitable_ops ops = {
605  
        static constexpr write_awaitable_ops ops = {
611  
            +[](void* p) {
606  
            +[](void* p) {
612  
                return static_cast<Aw*>(p)->await_ready();
607  
                return static_cast<Aw*>(p)->await_ready();
613  
            },
608  
            },
614  
            +[](void* p, std::coroutine_handle<> h, io_env const* env) {
609  
            +[](void* p, std::coroutine_handle<> h, io_env const* env) {
615  
                return detail::call_await_suspend(
610  
                return detail::call_await_suspend(
616  
                    static_cast<Aw*>(p), h, env);
611  
                    static_cast<Aw*>(p), h, env);
617  
            },
612  
            },
618  
            +[](void* p) {
613  
            +[](void* p) {
619  
                return static_cast<Aw*>(p)->await_resume();
614  
                return static_cast<Aw*>(p)->await_resume();
620  
            },
615  
            },
621  
            +[](void* p) noexcept {
616  
            +[](void* p) noexcept {
622  
                static_cast<Aw*>(p)->~Aw();
617  
                static_cast<Aw*>(p)->~Aw();
623  
            }
618  
            }
624  
        };
619  
        };
625  
        return &ops;
620  
        return &ops;
626  
    }
621  
    }
627  

622  

628  
    static awaitable_ops const*
623  
    static awaitable_ops const*
629  
    construct_write_eof_awaitable_impl(
624  
    construct_write_eof_awaitable_impl(
630  
        void* sink,
625  
        void* sink,
631  
        void* storage)
626  
        void* storage)
632  
        requires WriteSink<S>
627  
        requires WriteSink<S>
633  
    {
628  
    {
634  
        using Aw = decltype(std::declval<S&>().write_eof());
629  
        using Aw = decltype(std::declval<S&>().write_eof());
635  
        auto& s = *static_cast<S*>(sink);
630  
        auto& s = *static_cast<S*>(sink);
636  
        ::new(storage) Aw(s.write_eof());
631  
        ::new(storage) Aw(s.write_eof());
637  

632  

638  
        static constexpr awaitable_ops ops = {
633  
        static constexpr awaitable_ops ops = {
639  
            +[](void* p) {
634  
            +[](void* p) {
640  
                return static_cast<Aw*>(p)->await_ready();
635  
                return static_cast<Aw*>(p)->await_ready();
641  
            },
636  
            },
642  
            +[](void* p, std::coroutine_handle<> h, io_env const* env) {
637  
            +[](void* p, std::coroutine_handle<> h, io_env const* env) {
643  
                return detail::call_await_suspend(
638  
                return detail::call_await_suspend(
644  
                    static_cast<Aw*>(p), h, env);
639  
                    static_cast<Aw*>(p), h, env);
645  
            },
640  
            },
646  
            +[](void* p) {
641  
            +[](void* p) {
647  
                return static_cast<Aw*>(p)->await_resume();
642  
                return static_cast<Aw*>(p)->await_resume();
648  
            },
643  
            },
649  
            +[](void* p) noexcept {
644  
            +[](void* p) noexcept {
650  
                static_cast<Aw*>(p)->~Aw();
645  
                static_cast<Aw*>(p)->~Aw();
651  
            }
646  
            }
652  
        };
647  
        };
653  
        return &ops;
648  
        return &ops;
654  
    }
649  
    }
655 -
    //------------------------------------------------------
 
656 -

 
657  

650  

658  
    static consteval std::size_t
651  
    static consteval std::size_t
659  
    compute_max_size() noexcept
652  
    compute_max_size() noexcept
660  
    {
653  
    {
661  
        std::size_t s = sizeof(CommitAwaitable) > sizeof(CommitEofAwaitable)
654  
        std::size_t s = sizeof(CommitAwaitable) > sizeof(CommitEofAwaitable)
662  
            ? sizeof(CommitAwaitable)
655  
            ? sizeof(CommitAwaitable)
663  
            : sizeof(CommitEofAwaitable);
656  
            : sizeof(CommitEofAwaitable);
664  
        if constexpr (WriteSink<S>)
657  
        if constexpr (WriteSink<S>)
665  
        {
658  
        {
666  
            using WS = decltype(std::declval<S&>().write_some(
659  
            using WS = decltype(std::declval<S&>().write_some(
667  
                std::span<const_buffer const>{}));
660  
                std::span<const_buffer const>{}));
668  
            using W = decltype(std::declval<S&>().write(
661  
            using W = decltype(std::declval<S&>().write(
669  
                std::span<const_buffer const>{}));
662  
                std::span<const_buffer const>{}));
670  
            using WEB = decltype(std::declval<S&>().write_eof(
663  
            using WEB = decltype(std::declval<S&>().write_eof(
671  
                std::span<const_buffer const>{}));
664  
                std::span<const_buffer const>{}));
672  
            using WE = decltype(std::declval<S&>().write_eof());
665  
            using WE = decltype(std::declval<S&>().write_eof());
673  

666  

674  
            if(sizeof(WS) > s) s = sizeof(WS);
667  
            if(sizeof(WS) > s) s = sizeof(WS);
675  
            if(sizeof(W) > s) s = sizeof(W);
668  
            if(sizeof(W) > s) s = sizeof(W);
676  
            if(sizeof(WEB) > s) s = sizeof(WEB);
669  
            if(sizeof(WEB) > s) s = sizeof(WEB);
677  
            if(sizeof(WE) > s) s = sizeof(WE);
670  
            if(sizeof(WE) > s) s = sizeof(WE);
678  
        }
671  
        }
679  
        return s;
672  
        return s;
680  
    }
673  
    }
681  

674  

682  
    static consteval std::size_t
675  
    static consteval std::size_t
683  
    compute_max_align() noexcept
676  
    compute_max_align() noexcept
684  
    {
677  
    {
685  
        std::size_t a = alignof(CommitAwaitable) > alignof(CommitEofAwaitable)
678  
        std::size_t a = alignof(CommitAwaitable) > alignof(CommitEofAwaitable)
686  
            ? alignof(CommitAwaitable)
679  
            ? alignof(CommitAwaitable)
687  
            : alignof(CommitEofAwaitable);
680  
            : alignof(CommitEofAwaitable);
688  
        if constexpr (WriteSink<S>)
681  
        if constexpr (WriteSink<S>)
689  
        {
682  
        {
690  
            using WS = decltype(std::declval<S&>().write_some(
683  
            using WS = decltype(std::declval<S&>().write_some(
691  
                std::span<const_buffer const>{}));
684  
                std::span<const_buffer const>{}));
692  
            using W = decltype(std::declval<S&>().write(
685  
            using W = decltype(std::declval<S&>().write(
693  
                std::span<const_buffer const>{}));
686  
                std::span<const_buffer const>{}));
694  
            using WEB = decltype(std::declval<S&>().write_eof(
687  
            using WEB = decltype(std::declval<S&>().write_eof(
695  
                std::span<const_buffer const>{}));
688  
                std::span<const_buffer const>{}));
696  
            using WE = decltype(std::declval<S&>().write_eof());
689  
            using WE = decltype(std::declval<S&>().write_eof());
697  

690  

698  
            if(alignof(WS) > a) a = alignof(WS);
691  
            if(alignof(WS) > a) a = alignof(WS);
699  
            if(alignof(W) > a) a = alignof(W);
692  
            if(alignof(W) > a) a = alignof(W);
700  
            if(alignof(WEB) > a) a = alignof(WEB);
693  
            if(alignof(WEB) > a) a = alignof(WEB);
701  
            if(alignof(WE) > a) a = alignof(WE);
694  
            if(alignof(WE) > a) a = alignof(WE);
702  
        }
695  
        }
703  
        return a;
696  
        return a;
704  
    }
697  
    }
705  

698  

706  
    static consteval vtable
699  
    static consteval vtable
707  
    make_vtable() noexcept
700  
    make_vtable() noexcept
708  
    {
701  
    {
709  
        vtable v{};
702  
        vtable v{};
710  
        v.destroy = &do_destroy_impl;
703  
        v.destroy = &do_destroy_impl;
711  
        v.do_prepare = &do_prepare_impl;
704  
        v.do_prepare = &do_prepare_impl;
712  
        v.awaitable_size = compute_max_size();
705  
        v.awaitable_size = compute_max_size();
713  
        v.awaitable_align = compute_max_align();
706  
        v.awaitable_align = compute_max_align();
714  
        v.construct_commit_awaitable = &construct_commit_awaitable_impl;
707  
        v.construct_commit_awaitable = &construct_commit_awaitable_impl;
715  
        v.construct_commit_eof_awaitable = &construct_commit_eof_awaitable_impl;
708  
        v.construct_commit_eof_awaitable = &construct_commit_eof_awaitable_impl;
716  
        v.construct_write_some_awaitable = nullptr;
709  
        v.construct_write_some_awaitable = nullptr;
717  
        v.construct_write_awaitable = nullptr;
710  
        v.construct_write_awaitable = nullptr;
718  
        v.construct_write_eof_buffers_awaitable = nullptr;
711  
        v.construct_write_eof_buffers_awaitable = nullptr;
719  
        v.construct_write_eof_awaitable = nullptr;
712  
        v.construct_write_eof_awaitable = nullptr;
720  

713  

721  
        if constexpr (WriteSink<S>)
714  
        if constexpr (WriteSink<S>)
722  
        {
715  
        {
723  
            v.construct_write_some_awaitable =
716  
            v.construct_write_some_awaitable =
724  
                &construct_write_some_awaitable_impl;
717  
                &construct_write_some_awaitable_impl;
725  
            v.construct_write_awaitable =
718  
            v.construct_write_awaitable =
726  
                &construct_write_awaitable_impl;
719  
                &construct_write_awaitable_impl;
727  
            v.construct_write_eof_buffers_awaitable =
720  
            v.construct_write_eof_buffers_awaitable =
728  
                &construct_write_eof_buffers_awaitable_impl;
721  
                &construct_write_eof_buffers_awaitable_impl;
729  
            v.construct_write_eof_awaitable =
722  
            v.construct_write_eof_awaitable =
730  
                &construct_write_eof_awaitable_impl;
723  
                &construct_write_eof_awaitable_impl;
731  
        }
724  
        }
732  
        return v;
725  
        return v;
733  
    }
726  
    }
734  

727  

735  
    static constexpr vtable value = make_vtable();
728  
    static constexpr vtable value = make_vtable();
736  
};
729  
};
737 -
//----------------------------------------------------------
 
738 -

 
739  

730  

740  
inline
731  
inline
741  
any_buffer_sink::~any_buffer_sink()
732  
any_buffer_sink::~any_buffer_sink()
742  
{
733  
{
743  
    if(storage_)
734  
    if(storage_)
744  
    {
735  
    {
745  
        vt_->destroy(sink_);
736  
        vt_->destroy(sink_);
746  
        ::operator delete(storage_);
737  
        ::operator delete(storage_);
747  
    }
738  
    }
748  
    if(cached_awaitable_)
739  
    if(cached_awaitable_)
749  
        ::operator delete(cached_awaitable_);
740  
        ::operator delete(cached_awaitable_);
750  
}
741  
}
751  

742  

752  
inline any_buffer_sink&
743  
inline any_buffer_sink&
753  
any_buffer_sink::operator=(any_buffer_sink&& other) noexcept
744  
any_buffer_sink::operator=(any_buffer_sink&& other) noexcept
754  
{
745  
{
755  
    if(this != &other)
746  
    if(this != &other)
756  
    {
747  
    {
757  
        if(storage_)
748  
        if(storage_)
758  
        {
749  
        {
759  
            vt_->destroy(sink_);
750  
            vt_->destroy(sink_);
760  
            ::operator delete(storage_);
751  
            ::operator delete(storage_);
761  
        }
752  
        }
762  
        if(cached_awaitable_)
753  
        if(cached_awaitable_)
763  
            ::operator delete(cached_awaitable_);
754  
            ::operator delete(cached_awaitable_);
764  
        sink_ = std::exchange(other.sink_, nullptr);
755  
        sink_ = std::exchange(other.sink_, nullptr);
765  
        vt_ = std::exchange(other.vt_, nullptr);
756  
        vt_ = std::exchange(other.vt_, nullptr);
766  
        cached_awaitable_ = std::exchange(other.cached_awaitable_, nullptr);
757  
        cached_awaitable_ = std::exchange(other.cached_awaitable_, nullptr);
767  
        storage_ = std::exchange(other.storage_, nullptr);
758  
        storage_ = std::exchange(other.storage_, nullptr);
768  
        active_ops_ = std::exchange(other.active_ops_, nullptr);
759  
        active_ops_ = std::exchange(other.active_ops_, nullptr);
769  
        active_write_ops_ = std::exchange(other.active_write_ops_, nullptr);
760  
        active_write_ops_ = std::exchange(other.active_write_ops_, nullptr);
770  
    }
761  
    }
771  
    return *this;
762  
    return *this;
772  
}
763  
}
773  

764  

774  
template<BufferSink S>
765  
template<BufferSink S>
775  
    requires (!std::same_as<std::decay_t<S>, any_buffer_sink>)
766  
    requires (!std::same_as<std::decay_t<S>, any_buffer_sink>)
776  
any_buffer_sink::any_buffer_sink(S s)
767  
any_buffer_sink::any_buffer_sink(S s)
777  
    : vt_(&vtable_for_impl<S>::value)
768  
    : vt_(&vtable_for_impl<S>::value)
778  
{
769  
{
779  
    struct guard {
770  
    struct guard {
780  
        any_buffer_sink* self;
771  
        any_buffer_sink* self;
781  
        bool committed = false;
772  
        bool committed = false;
782  
        ~guard() {
773  
        ~guard() {
783  
            if(!committed && self->storage_) {
774  
            if(!committed && self->storage_) {
784  
                self->vt_->destroy(self->sink_);
775  
                self->vt_->destroy(self->sink_);
785  
                ::operator delete(self->storage_);
776  
                ::operator delete(self->storage_);
786  
                self->storage_ = nullptr;
777  
                self->storage_ = nullptr;
787  
                self->sink_ = nullptr;
778  
                self->sink_ = nullptr;
788  
            }
779  
            }
789  
        }
780  
        }
790  
    } g{this};
781  
    } g{this};
791  

782  

792  
    storage_ = ::operator new(sizeof(S));
783  
    storage_ = ::operator new(sizeof(S));
793  
    sink_ = ::new(storage_) S(std::move(s));
784  
    sink_ = ::new(storage_) S(std::move(s));
794  

785  

795  
    cached_awaitable_ = ::operator new(vt_->awaitable_size);
786  
    cached_awaitable_ = ::operator new(vt_->awaitable_size);
796  

787  

797  
    g.committed = true;
788  
    g.committed = true;
798  
}
789  
}
799  

790  

800  
template<BufferSink S>
791  
template<BufferSink S>
801  
any_buffer_sink::any_buffer_sink(S* s)
792  
any_buffer_sink::any_buffer_sink(S* s)
802  
    : sink_(s)
793  
    : sink_(s)
803  
    , vt_(&vtable_for_impl<S>::value)
794  
    , vt_(&vtable_for_impl<S>::value)
804  
{
795  
{
805  
    cached_awaitable_ = ::operator new(vt_->awaitable_size);
796  
    cached_awaitable_ = ::operator new(vt_->awaitable_size);
806  
}
797  
}
807 -
//----------------------------------------------------------
 
808 -

 
809  

798  

810  
inline std::span<mutable_buffer>
799  
inline std::span<mutable_buffer>
811  
any_buffer_sink::prepare(std::span<mutable_buffer> dest)
800  
any_buffer_sink::prepare(std::span<mutable_buffer> dest)
812  
{
801  
{
813  
    return vt_->do_prepare(sink_, dest);
802  
    return vt_->do_prepare(sink_, dest);
814  
}
803  
}
815  

804  

816  
inline auto
805  
inline auto
817  
any_buffer_sink::commit(std::size_t n)
806  
any_buffer_sink::commit(std::size_t n)
818  
{
807  
{
819  
    struct awaitable
808  
    struct awaitable
820  
    {
809  
    {
821  
        any_buffer_sink* self_;
810  
        any_buffer_sink* self_;
822  
        std::size_t n_;
811  
        std::size_t n_;
823  

812  

824  
        bool
813  
        bool
825  
        await_ready()
814  
        await_ready()
826  
        {
815  
        {
827  
            self_->active_ops_ = self_->vt_->construct_commit_awaitable(
816  
            self_->active_ops_ = self_->vt_->construct_commit_awaitable(
828  
                self_->sink_,
817  
                self_->sink_,
829  
                self_->cached_awaitable_,
818  
                self_->cached_awaitable_,
830  
                n_);
819  
                n_);
831  
            return self_->active_ops_->await_ready(self_->cached_awaitable_);
820  
            return self_->active_ops_->await_ready(self_->cached_awaitable_);
832  
        }
821  
        }
833  

822  

834  
        std::coroutine_handle<>
823  
        std::coroutine_handle<>
835  
        await_suspend(std::coroutine_handle<> h, io_env const* env)
824  
        await_suspend(std::coroutine_handle<> h, io_env const* env)
836  
        {
825  
        {
837  
            return self_->active_ops_->await_suspend(
826  
            return self_->active_ops_->await_suspend(
838  
                self_->cached_awaitable_, h, env);
827  
                self_->cached_awaitable_, h, env);
839  
        }
828  
        }
840  

829  

841  
        io_result<>
830  
        io_result<>
842  
        await_resume()
831  
        await_resume()
843  
        {
832  
        {
844  
            struct guard {
833  
            struct guard {
845  
                any_buffer_sink* self;
834  
                any_buffer_sink* self;
846  
                ~guard() {
835  
                ~guard() {
847  
                    self->active_ops_->destroy(self->cached_awaitable_);
836  
                    self->active_ops_->destroy(self->cached_awaitable_);
848  
                    self->active_ops_ = nullptr;
837  
                    self->active_ops_ = nullptr;
849  
                }
838  
                }
850  
            } g{self_};
839  
            } g{self_};
851  
            return self_->active_ops_->await_resume(
840  
            return self_->active_ops_->await_resume(
852  
                self_->cached_awaitable_);
841  
                self_->cached_awaitable_);
853  
        }
842  
        }
854  
    };
843  
    };
855  
    return awaitable{this, n};
844  
    return awaitable{this, n};
856  
}
845  
}
857  

846  

858  
inline auto
847  
inline auto
859  
any_buffer_sink::commit_eof(std::size_t n)
848  
any_buffer_sink::commit_eof(std::size_t n)
860  
{
849  
{
861  
    struct awaitable
850  
    struct awaitable
862  
    {
851  
    {
863  
        any_buffer_sink* self_;
852  
        any_buffer_sink* self_;
864  
        std::size_t n_;
853  
        std::size_t n_;
865  

854  

866  
        bool
855  
        bool
867  
        await_ready()
856  
        await_ready()
868  
        {
857  
        {
869  
            self_->active_ops_ = self_->vt_->construct_commit_eof_awaitable(
858  
            self_->active_ops_ = self_->vt_->construct_commit_eof_awaitable(
870  
                self_->sink_,
859  
                self_->sink_,
871  
                self_->cached_awaitable_,
860  
                self_->cached_awaitable_,
872  
                n_);
861  
                n_);
873  
            return self_->active_ops_->await_ready(self_->cached_awaitable_);
862  
            return self_->active_ops_->await_ready(self_->cached_awaitable_);
874  
        }
863  
        }
875  

864  

876  
        std::coroutine_handle<>
865  
        std::coroutine_handle<>
877  
        await_suspend(std::coroutine_handle<> h, io_env const* env)
866  
        await_suspend(std::coroutine_handle<> h, io_env const* env)
878  
        {
867  
        {
879  
            return self_->active_ops_->await_suspend(
868  
            return self_->active_ops_->await_suspend(
880  
                self_->cached_awaitable_, h, env);
869  
                self_->cached_awaitable_, h, env);
881  
        }
870  
        }
882  

871  

883  
        io_result<>
872  
        io_result<>
884  
        await_resume()
873  
        await_resume()
885  
        {
874  
        {
886  
            struct guard {
875  
            struct guard {
887  
                any_buffer_sink* self;
876  
                any_buffer_sink* self;
888  
                ~guard() {
877  
                ~guard() {
889  
                    self->active_ops_->destroy(self->cached_awaitable_);
878  
                    self->active_ops_->destroy(self->cached_awaitable_);
890  
                    self->active_ops_ = nullptr;
879  
                    self->active_ops_ = nullptr;
891  
                }
880  
                }
892  
            } g{self_};
881  
            } g{self_};
893  
            return self_->active_ops_->await_resume(
882  
            return self_->active_ops_->await_resume(
894  
                self_->cached_awaitable_);
883  
                self_->cached_awaitable_);
895  
        }
884  
        }
896  
    };
885  
    };
897  
    return awaitable{this, n};
886  
    return awaitable{this, n};
898  
}
887  
}
899 -
//----------------------------------------------------------
 
900 -
// Private helpers for native WriteSink forwarding
 
901 -

 
902  

888  

903  
inline auto
889  
inline auto
904  
any_buffer_sink::write_some_(
890  
any_buffer_sink::write_some_(
905  
    std::span<const_buffer const> buffers)
891  
    std::span<const_buffer const> buffers)
906  
{
892  
{
907  
    struct awaitable
893  
    struct awaitable
908  
    {
894  
    {
909  
        any_buffer_sink* self_;
895  
        any_buffer_sink* self_;
910  
        std::span<const_buffer const> buffers_;
896  
        std::span<const_buffer const> buffers_;
911  

897  

912  
        bool
898  
        bool
913  
        await_ready() const noexcept
899  
        await_ready() const noexcept
914  
        {
900  
        {
915  
            return false;
901  
            return false;
916  
        }
902  
        }
917  

903  

918  
        std::coroutine_handle<>
904  
        std::coroutine_handle<>
919  
        await_suspend(std::coroutine_handle<> h, io_env const* env)
905  
        await_suspend(std::coroutine_handle<> h, io_env const* env)
920  
        {
906  
        {
921  
            self_->active_write_ops_ =
907  
            self_->active_write_ops_ =
922  
                self_->vt_->construct_write_some_awaitable(
908  
                self_->vt_->construct_write_some_awaitable(
923  
                    self_->sink_,
909  
                    self_->sink_,
924  
                    self_->cached_awaitable_,
910  
                    self_->cached_awaitable_,
925  
                    buffers_);
911  
                    buffers_);
926  

912  

927  
            if(self_->active_write_ops_->await_ready(
913  
            if(self_->active_write_ops_->await_ready(
928  
                self_->cached_awaitable_))
914  
                self_->cached_awaitable_))
929  
                return h;
915  
                return h;
930  

916  

931  
            return self_->active_write_ops_->await_suspend(
917  
            return self_->active_write_ops_->await_suspend(
932  
                self_->cached_awaitable_, h, env);
918  
                self_->cached_awaitable_, h, env);
933  
        }
919  
        }
934  

920  

935  
        io_result<std::size_t>
921  
        io_result<std::size_t>
936  
        await_resume()
922  
        await_resume()
937  
        {
923  
        {
938  
            struct guard {
924  
            struct guard {
939  
                any_buffer_sink* self;
925  
                any_buffer_sink* self;
940  
                ~guard() {
926  
                ~guard() {
941  
                    self->active_write_ops_->destroy(
927  
                    self->active_write_ops_->destroy(
942  
                        self->cached_awaitable_);
928  
                        self->cached_awaitable_);
943  
                    self->active_write_ops_ = nullptr;
929  
                    self->active_write_ops_ = nullptr;
944  
                }
930  
                }
945  
            } g{self_};
931  
            } g{self_};
946  
            return self_->active_write_ops_->await_resume(
932  
            return self_->active_write_ops_->await_resume(
947  
                self_->cached_awaitable_);
933  
                self_->cached_awaitable_);
948  
        }
934  
        }
949  
    };
935  
    };
950  
    return awaitable{this, buffers};
936  
    return awaitable{this, buffers};
951  
}
937  
}
952  

938  

953  
inline auto
939  
inline auto
954  
any_buffer_sink::write_(
940  
any_buffer_sink::write_(
955  
    std::span<const_buffer const> buffers)
941  
    std::span<const_buffer const> buffers)
956  
{
942  
{
957  
    struct awaitable
943  
    struct awaitable
958  
    {
944  
    {
959  
        any_buffer_sink* self_;
945  
        any_buffer_sink* self_;
960  
        std::span<const_buffer const> buffers_;
946  
        std::span<const_buffer const> buffers_;
961  

947  

962  
        bool
948  
        bool
963  
        await_ready() const noexcept
949  
        await_ready() const noexcept
964  
        {
950  
        {
965  
            return false;
951  
            return false;
966  
        }
952  
        }
967  

953  

968  
        std::coroutine_handle<>
954  
        std::coroutine_handle<>
969  
        await_suspend(std::coroutine_handle<> h, io_env const* env)
955  
        await_suspend(std::coroutine_handle<> h, io_env const* env)
970  
        {
956  
        {
971  
            self_->active_write_ops_ =
957  
            self_->active_write_ops_ =
972  
                self_->vt_->construct_write_awaitable(
958  
                self_->vt_->construct_write_awaitable(
973  
                    self_->sink_,
959  
                    self_->sink_,
974  
                    self_->cached_awaitable_,
960  
                    self_->cached_awaitable_,
975  
                    buffers_);
961  
                    buffers_);
976  

962  

977  
            if(self_->active_write_ops_->await_ready(
963  
            if(self_->active_write_ops_->await_ready(
978  
                self_->cached_awaitable_))
964  
                self_->cached_awaitable_))
979  
                return h;
965  
                return h;
980  

966  

981  
            return self_->active_write_ops_->await_suspend(
967  
            return self_->active_write_ops_->await_suspend(
982  
                self_->cached_awaitable_, h, env);
968  
                self_->cached_awaitable_, h, env);
983  
        }
969  
        }
984  

970  

985  
        io_result<std::size_t>
971  
        io_result<std::size_t>
986  
        await_resume()
972  
        await_resume()
987  
        {
973  
        {
988  
            struct guard {
974  
            struct guard {
989  
                any_buffer_sink* self;
975  
                any_buffer_sink* self;
990  
                ~guard() {
976  
                ~guard() {
991  
                    self->active_write_ops_->destroy(
977  
                    self->active_write_ops_->destroy(
992  
                        self->cached_awaitable_);
978  
                        self->cached_awaitable_);
993  
                    self->active_write_ops_ = nullptr;
979  
                    self->active_write_ops_ = nullptr;
994  
                }
980  
                }
995  
            } g{self_};
981  
            } g{self_};
996  
            return self_->active_write_ops_->await_resume(
982  
            return self_->active_write_ops_->await_resume(
997  
                self_->cached_awaitable_);
983  
                self_->cached_awaitable_);
998  
        }
984  
        }
999  
    };
985  
    };
1000  
    return awaitable{this, buffers};
986  
    return awaitable{this, buffers};
1001  
}
987  
}
1002  

988  

1003  
inline auto
989  
inline auto
1004  
any_buffer_sink::write_eof_buffers_(
990  
any_buffer_sink::write_eof_buffers_(
1005  
    std::span<const_buffer const> buffers)
991  
    std::span<const_buffer const> buffers)
1006  
{
992  
{
1007  
    struct awaitable
993  
    struct awaitable
1008  
    {
994  
    {
1009  
        any_buffer_sink* self_;
995  
        any_buffer_sink* self_;
1010  
        std::span<const_buffer const> buffers_;
996  
        std::span<const_buffer const> buffers_;
1011  

997  

1012  
        bool
998  
        bool
1013  
        await_ready() const noexcept
999  
        await_ready() const noexcept
1014  
        {
1000  
        {
1015  
            return false;
1001  
            return false;
1016  
        }
1002  
        }
1017  

1003  

1018  
        std::coroutine_handle<>
1004  
        std::coroutine_handle<>
1019  
        await_suspend(std::coroutine_handle<> h, io_env const* env)
1005  
        await_suspend(std::coroutine_handle<> h, io_env const* env)
1020  
        {
1006  
        {
1021  
            self_->active_write_ops_ =
1007  
            self_->active_write_ops_ =
1022  
                self_->vt_->construct_write_eof_buffers_awaitable(
1008  
                self_->vt_->construct_write_eof_buffers_awaitable(
1023  
                    self_->sink_,
1009  
                    self_->sink_,
1024  
                    self_->cached_awaitable_,
1010  
                    self_->cached_awaitable_,
1025  
                    buffers_);
1011  
                    buffers_);
1026  

1012  

1027  
            if(self_->active_write_ops_->await_ready(
1013  
            if(self_->active_write_ops_->await_ready(
1028  
                self_->cached_awaitable_))
1014  
                self_->cached_awaitable_))
1029  
                return h;
1015  
                return h;
1030  

1016  

1031  
            return self_->active_write_ops_->await_suspend(
1017  
            return self_->active_write_ops_->await_suspend(
1032  
                self_->cached_awaitable_, h, env);
1018  
                self_->cached_awaitable_, h, env);
1033  
        }
1019  
        }
1034  

1020  

1035  
        io_result<std::size_t>
1021  
        io_result<std::size_t>
1036  
        await_resume()
1022  
        await_resume()
1037  
        {
1023  
        {
1038  
            struct guard {
1024  
            struct guard {
1039  
                any_buffer_sink* self;
1025  
                any_buffer_sink* self;
1040  
                ~guard() {
1026  
                ~guard() {
1041  
                    self->active_write_ops_->destroy(
1027  
                    self->active_write_ops_->destroy(
1042  
                        self->cached_awaitable_);
1028  
                        self->cached_awaitable_);
1043  
                    self->active_write_ops_ = nullptr;
1029  
                    self->active_write_ops_ = nullptr;
1044  
                }
1030  
                }
1045  
            } g{self_};
1031  
            } g{self_};
1046  
            return self_->active_write_ops_->await_resume(
1032  
            return self_->active_write_ops_->await_resume(
1047  
                self_->cached_awaitable_);
1033  
                self_->cached_awaitable_);
1048  
        }
1034  
        }
1049  
    };
1035  
    };
1050  
    return awaitable{this, buffers};
1036  
    return awaitable{this, buffers};
1051  
}
1037  
}
1052 -
//----------------------------------------------------------
 
1053 -
// Public WriteSink methods
 
1054 -

 
1055  

1038  

1056  
template<ConstBufferSequence CB>
1039  
template<ConstBufferSequence CB>
1057  
io_task<std::size_t>
1040  
io_task<std::size_t>
1058  
any_buffer_sink::write_some(CB buffers)
1041  
any_buffer_sink::write_some(CB buffers)
1059  
{
1042  
{
1060  
    buffer_param<CB> bp(buffers);
1043  
    buffer_param<CB> bp(buffers);
1061  
    auto src = bp.data();
1044  
    auto src = bp.data();
1062  
    if(src.empty())
1045  
    if(src.empty())
1063  
        co_return {{}, 0};
1046  
        co_return {{}, 0};
1064  

1047  

1065  
    // Native WriteSink path
1048  
    // Native WriteSink path
1066  
    if(vt_->construct_write_some_awaitable)
1049  
    if(vt_->construct_write_some_awaitable)
1067  
        co_return co_await write_some_(src);
1050  
        co_return co_await write_some_(src);
1068  

1051  

1069  
    // Synthesized path: prepare + buffer_copy + commit
1052  
    // Synthesized path: prepare + buffer_copy + commit
1070  
    mutable_buffer arr[detail::max_iovec_];
1053  
    mutable_buffer arr[detail::max_iovec_];
1071  
    auto dst_bufs = prepare(arr);
1054  
    auto dst_bufs = prepare(arr);
1072  
    if(dst_bufs.empty())
1055  
    if(dst_bufs.empty())
1073  
    {
1056  
    {
1074  
        auto [ec] = co_await commit(0);
1057  
        auto [ec] = co_await commit(0);
1075  
        if(ec)
1058  
        if(ec)
1076  
            co_return {ec, 0};
1059  
            co_return {ec, 0};
1077  
        dst_bufs = prepare(arr);
1060  
        dst_bufs = prepare(arr);
1078  
        if(dst_bufs.empty())
1061  
        if(dst_bufs.empty())
1079  
            co_return {{}, 0};
1062  
            co_return {{}, 0};
1080  
    }
1063  
    }
1081  

1064  

1082  
    auto n = buffer_copy(dst_bufs, src);
1065  
    auto n = buffer_copy(dst_bufs, src);
1083  
    auto [ec] = co_await commit(n);
1066  
    auto [ec] = co_await commit(n);
1084  
    if(ec)
1067  
    if(ec)
1085  
        co_return {ec, 0};
1068  
        co_return {ec, 0};
1086  
    co_return {{}, n};
1069  
    co_return {{}, n};
1087  
}
1070  
}
1088  

1071  

1089  
template<ConstBufferSequence CB>
1072  
template<ConstBufferSequence CB>
1090  
io_task<std::size_t>
1073  
io_task<std::size_t>
1091  
any_buffer_sink::write(CB buffers)
1074  
any_buffer_sink::write(CB buffers)
1092  
{
1075  
{
1093  
    buffer_param<CB> bp(buffers);
1076  
    buffer_param<CB> bp(buffers);
1094  
    std::size_t total = 0;
1077  
    std::size_t total = 0;
1095  

1078  

1096  
    // Native WriteSink path
1079  
    // Native WriteSink path
1097  
    if(vt_->construct_write_awaitable)
1080  
    if(vt_->construct_write_awaitable)
1098  
    {
1081  
    {
1099  
        for(;;)
1082  
        for(;;)
1100  
        {
1083  
        {
1101  
            auto bufs = bp.data();
1084  
            auto bufs = bp.data();
1102  
            if(bufs.empty())
1085  
            if(bufs.empty())
1103  
                break;
1086  
                break;
1104  

1087  

1105  
            auto [ec, n] = co_await write_(bufs);
1088  
            auto [ec, n] = co_await write_(bufs);
1106  
            total += n;
1089  
            total += n;
1107  
            if(ec)
1090  
            if(ec)
1108  
                co_return {ec, total};
1091  
                co_return {ec, total};
1109  
            bp.consume(n);
1092  
            bp.consume(n);
1110  
        }
1093  
        }
1111  
        co_return {{}, total};
1094  
        co_return {{}, total};
1112  
    }
1095  
    }
1113  

1096  

1114  
    // Synthesized path: prepare + buffer_copy + commit
1097  
    // Synthesized path: prepare + buffer_copy + commit
1115  
    for(;;)
1098  
    for(;;)
1116  
    {
1099  
    {
1117  
        auto src = bp.data();
1100  
        auto src = bp.data();
1118  
        if(src.empty())
1101  
        if(src.empty())
1119  
            break;
1102  
            break;
1120  

1103  

1121  
        mutable_buffer arr[detail::max_iovec_];
1104  
        mutable_buffer arr[detail::max_iovec_];
1122  
        auto dst_bufs = prepare(arr);
1105  
        auto dst_bufs = prepare(arr);
1123  
        if(dst_bufs.empty())
1106  
        if(dst_bufs.empty())
1124  
        {
1107  
        {
1125  
            auto [ec] = co_await commit(0);
1108  
            auto [ec] = co_await commit(0);
1126  
            if(ec)
1109  
            if(ec)
1127  
                co_return {ec, total};
1110  
                co_return {ec, total};
1128  
            continue;
1111  
            continue;
1129  
        }
1112  
        }
1130  

1113  

1131  
        auto n = buffer_copy(dst_bufs, src);
1114  
        auto n = buffer_copy(dst_bufs, src);
1132  
        auto [ec] = co_await commit(n);
1115  
        auto [ec] = co_await commit(n);
1133  
        if(ec)
1116  
        if(ec)
1134  
            co_return {ec, total};
1117  
            co_return {ec, total};
1135  
        bp.consume(n);
1118  
        bp.consume(n);
1136  
        total += n;
1119  
        total += n;
1137  
    }
1120  
    }
1138  

1121  

1139  
    co_return {{}, total};
1122  
    co_return {{}, total};
1140  
}
1123  
}
1141  

1124  

1142  
inline auto
1125  
inline auto
1143  
any_buffer_sink::write_eof()
1126  
any_buffer_sink::write_eof()
1144  
{
1127  
{
1145  
    struct awaitable
1128  
    struct awaitable
1146  
    {
1129  
    {
1147  
        any_buffer_sink* self_;
1130  
        any_buffer_sink* self_;
1148  

1131  

1149  
        bool
1132  
        bool
1150  
        await_ready()
1133  
        await_ready()
1151  
        {
1134  
        {
1152  
            if(self_->vt_->construct_write_eof_awaitable)
1135  
            if(self_->vt_->construct_write_eof_awaitable)
1153  
            {
1136  
            {
1154  
                // Native WriteSink: forward to underlying write_eof()
1137  
                // Native WriteSink: forward to underlying write_eof()
1155  
                self_->active_ops_ =
1138  
                self_->active_ops_ =
1156  
                    self_->vt_->construct_write_eof_awaitable(
1139  
                    self_->vt_->construct_write_eof_awaitable(
1157  
                        self_->sink_,
1140  
                        self_->sink_,
1158  
                        self_->cached_awaitable_);
1141  
                        self_->cached_awaitable_);
1159  
            }
1142  
            }
1160  
            else
1143  
            else
1161  
            {
1144  
            {
1162  
                // Synthesized: commit_eof(0)
1145  
                // Synthesized: commit_eof(0)
1163  
                self_->active_ops_ =
1146  
                self_->active_ops_ =
1164  
                    self_->vt_->construct_commit_eof_awaitable(
1147  
                    self_->vt_->construct_commit_eof_awaitable(
1165  
                        self_->sink_,
1148  
                        self_->sink_,
1166  
                        self_->cached_awaitable_,
1149  
                        self_->cached_awaitable_,
1167  
                        0);
1150  
                        0);
1168  
            }
1151  
            }
1169  
            return self_->active_ops_->await_ready(
1152  
            return self_->active_ops_->await_ready(
1170  
                self_->cached_awaitable_);
1153  
                self_->cached_awaitable_);
1171  
        }
1154  
        }
1172  

1155  

1173  
        std::coroutine_handle<>
1156  
        std::coroutine_handle<>
1174  
        await_suspend(std::coroutine_handle<> h, io_env const* env)
1157  
        await_suspend(std::coroutine_handle<> h, io_env const* env)
1175  
        {
1158  
        {
1176  
            return self_->active_ops_->await_suspend(
1159  
            return self_->active_ops_->await_suspend(
1177  
                self_->cached_awaitable_, h, env);
1160  
                self_->cached_awaitable_, h, env);
1178  
        }
1161  
        }
1179  

1162  

1180  
        io_result<>
1163  
        io_result<>
1181  
        await_resume()
1164  
        await_resume()
1182  
        {
1165  
        {
1183  
            struct guard {
1166  
            struct guard {
1184  
                any_buffer_sink* self;
1167  
                any_buffer_sink* self;
1185  
                ~guard() {
1168  
                ~guard() {
1186  
                    self->active_ops_->destroy(self->cached_awaitable_);
1169  
                    self->active_ops_->destroy(self->cached_awaitable_);
1187  
                    self->active_ops_ = nullptr;
1170  
                    self->active_ops_ = nullptr;
1188  
                }
1171  
                }
1189  
            } g{self_};
1172  
            } g{self_};
1190  
            return self_->active_ops_->await_resume(
1173  
            return self_->active_ops_->await_resume(
1191  
                self_->cached_awaitable_);
1174  
                self_->cached_awaitable_);
1192  
        }
1175  
        }
1193  
    };
1176  
    };
1194  
    return awaitable{this};
1177  
    return awaitable{this};
1195  
}
1178  
}
1196  

1179  

1197  
template<ConstBufferSequence CB>
1180  
template<ConstBufferSequence CB>
1198  
io_task<std::size_t>
1181  
io_task<std::size_t>
1199  
any_buffer_sink::write_eof(CB buffers)
1182  
any_buffer_sink::write_eof(CB buffers)
1200  
{
1183  
{
1201  
    // Native WriteSink path
1184  
    // Native WriteSink path
1202  
    if(vt_->construct_write_eof_buffers_awaitable)
1185  
    if(vt_->construct_write_eof_buffers_awaitable)
1203  
    {
1186  
    {
1204  
        const_buffer_param<CB> bp(buffers);
1187  
        const_buffer_param<CB> bp(buffers);
1205  
        std::size_t total = 0;
1188  
        std::size_t total = 0;
1206  

1189  

1207  
        for(;;)
1190  
        for(;;)
1208  
        {
1191  
        {
1209  
            auto bufs = bp.data();
1192  
            auto bufs = bp.data();
1210  
            if(bufs.empty())
1193  
            if(bufs.empty())
1211  
            {
1194  
            {
1212  
                auto [ec] = co_await write_eof();
1195  
                auto [ec] = co_await write_eof();
1213  
                co_return {ec, total};
1196  
                co_return {ec, total};
1214  
            }
1197  
            }
1215  

1198  

1216  
            if(!bp.more())
1199  
            if(!bp.more())
1217  
            {
1200  
            {
1218  
                // Last window: send atomically with EOF
1201  
                // Last window: send atomically with EOF
1219  
                auto [ec, n] = co_await write_eof_buffers_(bufs);
1202  
                auto [ec, n] = co_await write_eof_buffers_(bufs);
1220  
                total += n;
1203  
                total += n;
1221  
                co_return {ec, total};
1204  
                co_return {ec, total};
1222  
            }
1205  
            }
1223  

1206  

1224  
            auto [ec, n] = co_await write_(bufs);
1207  
            auto [ec, n] = co_await write_(bufs);
1225  
            total += n;
1208  
            total += n;
1226  
            if(ec)
1209  
            if(ec)
1227  
                co_return {ec, total};
1210  
                co_return {ec, total};
1228  
            bp.consume(n);
1211  
            bp.consume(n);
1229  
        }
1212  
        }
1230  
    }
1213  
    }
1231  

1214  

1232  
    // Synthesized path: prepare + buffer_copy + commit + commit_eof
1215  
    // Synthesized path: prepare + buffer_copy + commit + commit_eof
1233  
    buffer_param<CB> bp(buffers);
1216  
    buffer_param<CB> bp(buffers);
1234  
    std::size_t total = 0;
1217  
    std::size_t total = 0;
1235  

1218  

1236  
    for(;;)
1219  
    for(;;)
1237  
    {
1220  
    {
1238  
        auto src = bp.data();
1221  
        auto src = bp.data();
1239  
        if(src.empty())
1222  
        if(src.empty())
1240  
            break;
1223  
            break;
1241  

1224  

1242  
        mutable_buffer arr[detail::max_iovec_];
1225  
        mutable_buffer arr[detail::max_iovec_];
1243  
        auto dst_bufs = prepare(arr);
1226  
        auto dst_bufs = prepare(arr);
1244  
        if(dst_bufs.empty())
1227  
        if(dst_bufs.empty())
1245  
        {
1228  
        {
1246  
            auto [ec] = co_await commit(0);
1229  
            auto [ec] = co_await commit(0);
1247  
            if(ec)
1230  
            if(ec)
1248  
                co_return {ec, total};
1231  
                co_return {ec, total};
1249  
            continue;
1232  
            continue;
1250  
        }
1233  
        }
1251  

1234  

1252  
        auto n = buffer_copy(dst_bufs, src);
1235  
        auto n = buffer_copy(dst_bufs, src);
1253  
        auto [ec] = co_await commit(n);
1236  
        auto [ec] = co_await commit(n);
1254  
        if(ec)
1237  
        if(ec)
1255  
            co_return {ec, total};
1238  
            co_return {ec, total};
1256  
        bp.consume(n);
1239  
        bp.consume(n);
1257  
        total += n;
1240  
        total += n;
1258  
    }
1241  
    }
1259  

1242  

1260  
    auto [ec] = co_await commit_eof(0);
1243  
    auto [ec] = co_await commit_eof(0);
1261  
    if(ec)
1244  
    if(ec)
1262  
        co_return {ec, total};
1245  
        co_return {ec, total};
1263  

1246  

1264  
    co_return {{}, total};
1247  
    co_return {{}, total};
1265 -

 
1266 -
//----------------------------------------------------------
 
1267  
}
1248  
}
1268  

1249  

1269  
static_assert(BufferSink<any_buffer_sink>);
1250  
static_assert(BufferSink<any_buffer_sink>);
1270  
static_assert(WriteSink<any_buffer_sink>);
1251  
static_assert(WriteSink<any_buffer_sink>);
1271  

1252  

1272  
} // namespace capy
1253  
} // namespace capy
1273  
} // namespace boost
1254  
} // namespace boost
1274  

1255  

1275  
#endif
1256  
#endif