100.00% Lines (2/2) 100.00% Functions (1/1)
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_WRITE_HPP 11   #ifndef BOOST_CAPY_WRITE_HPP
12   #define BOOST_CAPY_WRITE_HPP 12   #define BOOST_CAPY_WRITE_HPP
13   13  
14   #include <boost/capy/detail/config.hpp> 14   #include <boost/capy/detail/config.hpp>
15   #include <boost/capy/results.hpp> 15   #include <boost/capy/results.hpp>
16   #include <boost/capy/task.hpp> 16   #include <boost/capy/task.hpp>
17   #include <boost/capy/buffers.hpp> 17   #include <boost/capy/buffers.hpp>
18   #include <boost/capy/detail/consuming_buffers.hpp> 18   #include <boost/capy/detail/consuming_buffers.hpp>
19   #include <boost/capy/concept/write_stream.hpp> 19   #include <boost/capy/concept/write_stream.hpp>
20   #include <system_error> 20   #include <system_error>
21   21  
22   #include <cstddef> 22   #include <cstddef>
23   23  
24   namespace boost { 24   namespace boost {
25   namespace capy { 25   namespace capy {
26   26  
27 - /** Write an entire buffer sequence to a stream. 27 + /** Asynchronously write an entire buffer sequence to a stream.
28   28  
29   @par Await-effects 29   @par Await-effects
30   30  
31   Writes the contents of `buffers` to `stream` via awaiting 31   Writes the contents of `buffers` to `stream` via awaiting
32   `stream.write_some` with consecutive portions of data from `buffers` 32   `stream.write_some` with consecutive portions of data from `buffers`
33   until: 33   until:
34   34  
35 - @li either the full content of @c buffers is processed, 35 + @li either the full content of `buffers` is processed,
36   @li or a contingency in `stream.write_some` occurs. 36   @li or a contingency in `stream.write_some` occurs.
37   37  
38   If `buffer_size(buffers) == 0` then no awaiting `stream.write_some` 38   If `buffer_size(buffers) == 0` then no awaiting `stream.write_some`
39   is performed. This is not a contingency. 39   is performed. This is not a contingency.
40   40  
41   41  
42   @par Await-returns 42   @par Await-returns
43   43  
44   An object of type `results<std::size_t>` destructuring as `[ec, n]`. 44   An object of type `results<std::size_t>` destructuring as `[ec, n]`.
45   45  
46   Upon a contingency, `n` represents the number of bytes written 46   Upon a contingency, `n` represents the number of bytes written
47   so far. 47   so far.
48 - Otherwise `n` represents the number of bytes written.  
49 -  
50   48  
51   Contingencies: 49   Contingencies:
52   50  
53 - @li The first contingency reported from awaiting @c stream.write_some 51 + @li The first contingency reported from awaiting `stream.write_some`
54   while not all bytes have been written. A contingency that accompanies 52   while not all bytes have been written. A contingency that accompanies
55   the write which transfers the last bytes is not reported: a completed 53   the write which transfers the last bytes is not reported: a completed
56   transfer is a success. 54   transfer is a success.
57   55  
58   Notable conditions: 56   Notable conditions:
59   57  
60 - @li @c cond::canceled — Operation was cancelled, 58 + @li `cond::canceled`: the operation was canceled.
61 - @li @c std::errc::broken_pipe — Peer closed connection. 59 + @li `std::errc::broken_pipe`: the peer closed the connection.
62   60  
63   61  
64   @par Await-postcondition 62   @par Await-postcondition
65   63  
66   If `n == buffer_size(buffers)` the transfer completed and `ec` is 64   If `n == buffer_size(buffers)` the transfer completed and `ec` is
67   success; otherwise `ec` is set. 65   success; otherwise `ec` is set.
68   66  
  67 + @par Remarks
69   68  
70 - @param stream The stream to write to. If the lifetime of `stream` ends 69 + Cancellation follows the stop token of the caller's
71 - before the coroutine finishes, the behavior is undefined. 70 + @ref await_context.
72   71  
73 - @param buffers The buffer sequence to write. If the lifetime of the buffer 72 + @par Exception Safety
74 - sequence represented by `buffers` ends 73 + Calling `write` allocates the coroutine frame, which throws if the
75 - before the coroutine finishes, the behavior is undefined. 74 + frame allocator fails. Awaiting the task rethrows any exception
  75 + thrown by awaiting `stream.write_some`.
76   76  
77 - @return A task yielding `results<std::size_t>` whose second element 77 + @par Example
78 - is the number of bytes written.  
79   78  
80 - @par Remarks 79 + @par !example example
81 - Supports _ContextAwaitable cancellation_.  
82   80  
83   81  
84 - @par Example 82 + @param stream The stream to write to. If the lifetime of `stream` ends
  83 + before the coroutine finishes, the behavior is undefined.
85   84  
86 - @par !example example 85 + @param buffers The buffer sequence to write. If the lifetime of the
  86 + buffer sequence represented by `buffers` ends before the coroutine
  87 + finishes, the behavior is undefined.
87   88  
  89 + @return A task yielding `results<std::size_t>` whose second element
  90 + is the number of bytes written.
88   91  
89   @see @ref WriteStream, @ref ConstBufferSequence, @ref ContextAwaitable, @ref results, @ref cond 92   @see @ref WriteStream, @ref ConstBufferSequence, @ref ContextAwaitable, @ref results, @ref cond
90   */ 93   */
91   template <WriteStream S, ConstBufferSequence CB> 94   template <WriteStream S, ConstBufferSequence CB>
HITCBC 92   56 auto write(S& stream, CB buffers) -> task<results<std::size_t>> 95   92 auto write(S& stream, CB buffers) -> task<results<std::size_t>>
93   { 96   {
94   detail::consuming_buffers consuming(buffers); 97   detail::consuming_buffers consuming(buffers);
95   std::size_t const total_size = buffer_size(buffers); 98   std::size_t const total_size = buffer_size(buffers);
96   std::size_t total_written = 0; 99   std::size_t total_written = 0;
97   100  
98   while(total_written < total_size) 101   while(total_written < total_size)
99   { 102   {
100   auto [ec, n] = co_await stream.write_some(consuming.data()); 103   auto [ec, n] = co_await stream.write_some(consuming.data());
101   consuming.consume(n); 104   consuming.consume(n);
102   total_written += n; 105   total_written += n;
103   // A contingency that still completed the transfer is a success: 106   // A contingency that still completed the transfer is a success:
104   // report it only when not all bytes were written. 107   // report it only when not all bytes were written.
105   if(ec && total_written < total_size) 108   if(ec && total_written < total_size)
106   co_return {ec, total_written}; 109   co_return {ec, total_written};
107   } 110   }
108   111  
109   co_return {std::error_code(), total_written}; 112   co_return {std::error_code(), total_written};
HITCBC 110   112 } 113   184 }
111   114  
112   } // namespace capy 115   } // namespace capy
113   } // namespace boost 116   } // namespace boost
114   117  
115   #endif 118   #endif