LCOV - code coverage report
Current view: top level - capy - write.hpp (source / functions) Coverage Total Hit
Test: coverage_remapped.info Lines: 100.0 % 2 2
Test Date: 2026-05-18 20:10:28 Functions: 100.0 % 2 2

           TLA  Line data    Source code
       1                 : //
       2                 : // Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com)
       3                 : //
       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)
       6                 : //
       7                 : // Official repository: https://github.com/cppalliance/capy
       8                 : //
       9                 : 
      10                 : #ifndef BOOST_CAPY_WRITE_HPP
      11                 : #define BOOST_CAPY_WRITE_HPP
      12                 : 
      13                 : #include <boost/capy/detail/config.hpp>
      14                 : #include <boost/capy/io_task.hpp>
      15                 : #include <boost/capy/buffers.hpp>
      16                 : #include <boost/capy/buffers/buffer_slice.hpp>
      17                 : #include <boost/capy/concept/write_stream.hpp>
      18                 : #include <system_error>
      19                 : 
      20                 : #include <cstddef>
      21                 : 
      22                 : namespace boost {
      23                 : namespace capy {
      24                 : 
      25                 : /** Make an awaitable for writing a buffer sequence to a stream.
      26                 : 
      27                 :     @par Await-effects
      28                 : 
      29                 :     Writes the contents of `buffers` to `stream` via awaiting on
      30                 :     `stream.write_some` with consecutive portions of data form `buffers`
      31                 :     until:
      32                 : 
      33                 :     @li either the full content of @c buffers is processed,
      34                 :     @li or a contingency occurs.
      35                 : 
      36                 : 
      37                 :     @par Await-returns
      38                 : 
      39                 :     An object of type `io_result<std::size_t>` destructuring as `[ec, n]`.
      40                 : 
      41                 :     Upon a contingency, `n` represents the number of bytes written
      42                 :     so far.
      43                 : 
      44                 :     Otherwise `n` represents the number of bytes written.
      45                 : 
      46                 :     Contingencies:
      47                 : 
      48                 :     @li Whatever the first contingency is reported from
      49                 :     awaiting on @c stream.write_some .
      50                 : 
      51                 :     Notable conditions:
      52                 : 
      53                 :     @li @c cond::canceled — Operation was cancelled,
      54                 :     @li @c std::errc::broken_pipe — Peer closed connection.
      55                 : 
      56                 : 
      57                 :     @par Await-postcondition
      58                 : 
      59                 :     `ec || n == buffer_size(buffers)`.
      60                 : 
      61                 : 
      62                 :     @par Cancellation
      63                 :     Supports cancellation via `stop_token` propagated through the
      64                 :     `IoAwaitable` protocol. When cancelled, returns with `cond::canceled`.
      65                 : 
      66                 :     @param stream The stream to write to. If the lifetime of `stream` ends
      67                 :     before the coroutine finishes, the behavior is undefined.
      68                 : 
      69                 :     @param buffers The buffer sequence to write. If the lifetime of `buffers` ends
      70                 :     before the coroutine finishes, the behavior is undefined.
      71                 : 
      72                 : 
      73                 :     @par Example
      74                 : 
      75                 :     @code
      76                 :     task<> send_response(capy::WriteStream auto& stream, std::string_view body)
      77                 :     {
      78                 :         auto [ec, n] = co_await capy::write(stream, capy::make_buffer(body));
      79                 :         if (ec)
      80                 :             throw std::system_error(ec);
      81                 : 
      82                 :         // All bytes written successfully
      83                 :     }
      84                 :     @endcode
      85                 : 
      86                 :     @return
      87                 : 
      88                 :     @see write_some, WriteStream, ConstBufferSequence
      89                 : */
      90                 : template <WriteStream S, ConstBufferSequence CB>
      91 HIT          50 : auto write(S& stream, CB buffers) -> io_task<std::size_t>
      92                 : {
      93                 :     auto consuming = buffer_slice(buffers);
      94                 :     std::size_t const total_size = buffer_size(buffers);
      95                 :     std::size_t total_written = 0;
      96                 : 
      97                 :     while(total_written < total_size)
      98                 :     {
      99                 :         auto [ec, n] = co_await stream.write_some(consuming.data());
     100                 :         consuming.remove_prefix(n);
     101                 :         total_written += n;
     102                 :         if(ec)
     103                 :             co_return {ec, total_written};
     104                 :     }
     105                 : 
     106                 :     co_return {{}, total_written};
     107             100 : }
     108                 : 
     109                 : } // namespace capy
     110                 : } // namespace boost
     111                 : 
     112                 : #endif
        

Generated by: LCOV version 2.3