include/boost/capy/write.hpp

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