Contingencies
A contingency is any situation occurring during an operation on a stream that prevents this operation from reading or writing the requested number of bytes. The stream’s state or an external request, such as cancellation, can cause it.
These situations do not violate the postconditions of the corresponding operations, as their postconditions never say that the operation indeed processes the requested number of bytes.
Each of this library’s stream operations that may encounter a contingency
await-returns a type which is a specialization of capy::results.
A user-defined stream’s read_some and write_some need only return a ContextAwaitable whose result destructures to [ec, n]. The results objects can be destructured
using a structured binding. The first binding of such destructuring is of type
std::error_code. This binding, call it ec, is used to signal if and which
contingency occurred:
-
If
ec == std::error_code{}, no contingency occurred. -
Otherwise a contingency occurred. To determine which contingency occurred, compare
ecto error conditions, in particular tocapy::cond.
| Reaching the end of stream is also a contingency. The stream has no more bytes to deliver, so the read cannot transfer the requested number of bytes. |
| The stream operations can still throw exceptions to indicate conditions, unrelated to stream state, that prevent these operations from satisfying their postconditions, such as failures to grow a buffer, or failure to allocate a coroutine frame. |
Operations on streams often await-return capy::results<std::size_t>
destructuring to [ec, n], where n represents the number of processed bytes.
Upon a reported contingency, a non-zero n reports the bytes transferred
before the contingency. When an inner operation reports a contingency,
the outer operation usually processes its partial results before reporting
the contingency itself.
|