boost::capy::ReadStream

Requires read_some to return a ContextAwaitable decomposing to an error code and a size.

Synopsis

template<typename T>
concept ReadStream = requires(T& stream, mutable_buffer_archetype buffers)
    {
        { stream.read_some(buffers) } ‐> ContextAwaitable;
        requires awaitable_decomposes_to<
            decltype(stream.read_some(buffers)),
            std::error_code, std::size_t>;
    };

Description

A type satisfies ReadStream if it provides a read_some member function template that accepts any MutableBufferSequence and await‐returns (error_code, std::size_t).

Syntactic Requirements

  • T must provide a read_some member function template accepting any MutableBufferSequence.

  • The return type of read_some must satisfy ContextAwaitable.

  • The awaitable's result must decompose to (error_code,std::size_t) via structured bindings.

Semantic Requirements

Attempts to read up to buffer_size( buffers ) bytes from the stream into the buffer sequence.

If buffer_size( buffers ) > 0:

  • If !ec, then n >= 1 && n <= buffer_size( buffers ). n bytes were read into the buffer sequence.

  • If ec, then n >= 0 && n < buffer_size( buffers ). n is the number of bytes read before the I/O contingency arose.

Equivalently, n == buffer_size( buffers ) implies !ec. A completion that fills the buffer sequence is a success, even when the underlying operation also signals a contingency such as end‐of‐stream. That contingency is reported on a subsequent read. This lets generic composition algorithms such as when_all and when_any distinguish a completed transfer from a failure.

If buffers_empty( buffers ) is true, n is 0. The empty buffer is not itself a cause for error, but ec may reflect the state of the stream.

Buffers in the sequence are filled in order.

After an Error

A subsequent read_some call is permitted. A conforming stream may report the same contingency, report a different one, or resume delivering data.

Error Reporting

I/O contingencies arising from the underlying I/O system are reported via the error_code component of the return value. Examples are EOF, connection reset, and broken pipe. Failures in the library wrapper itself (such as memory allocation failure) are reported via exceptions.

Buffer Lifetime

The caller must ensure that the memory referenced by buffers remains valid until the co_await expression returns.

Conforming Signatures

template< capy::MutableBufferSequence MB >
capy::ContextAwaitable auto read_some( MB buffers );

Pass buffer sequences by value. A by‐value parameter is copied into the coroutine frame, or into the awaitable's state. The returned awaitable is therefore self‐contained. A caller may store it, move it across threads, or wrap it into a sender without lifetime concerns. A by‐const‐reference parameter binds to caller storage. It is safe only when co_await consumes the awaitable immediately, in the same scope. Storing such an awaitable produces a dangling reference.

Callers who want to avoid copying an expensive buffer sequence (for example, a std::vector<mutable_buffer> with many entries) can pass std::views::all(seq) at the call site. The resulting ref_view satisfies the buffer‐sequence concepts and copies in O(1). See doc/buffers‐passing‐rationale.md.

Example

template< capy::ReadStream Stream >
capy::task<> read_all( Stream& s, char* buf, std::size_t size )
{
    std::size_t total = 0;
    while( total < size )
    {
        auto [ec, n] = co_await s.read_some(
            capy::mutable_buffer( buf + total, size - total ) );
        total += n;
        if( ec )
            co_return;
    }
}

Exceptions

Name

Thrown on

std::bad_alloc

If coroutine frame allocation fails.