# Receivers and Deferred Calls

> The receiver decides where a verb runs and how it completes, while the verb and its arguments stay the same.

`#include <uform/types.hpp>` 4.0.0-rc.1 · c58749fe

The receiver decides where a verb runs and how it completes, while the verb and its arguments stay the same:

- `model.text_encode(..)` runs on the calling thread.
- `model.on(executor).text_encode(..)` blocks on the executor, failing `status_code_t::contended_k` while another call holds it.
- `model.deferred(executor).text_encode(..)` submits the call and answers a `deferred`.

`model<receiver>` writes every verb once for all three: `model_t` owns the model and loads it, while `on` and `deferred` answer a `model_on<completion>` that refers to it and the executor, and `trainer<receiver>` does the same for a trainer. A blocking verb answers a `status_t` when it outputs nothing else and an `expected<T>` otherwise, so nothing about asynchrony reaches a caller who never asks for it. Each class spells that choice once, as its `deferrable<T>`, which names a `deferred<T>` under a deferred receiver.

```cpp
uf::deferred<uf::encoded_t> call =
    model.deferred(executor).text_encode(texts, uf::encode_outputs_t::embeddings_k, uform_scalar_f32_k, arena); // running now
other_work();
auto [encoded, status] = call.wait(); // blocks until final; destruction waits too
```

Any coroutine type awaits a deferred call, with any loop offering `post(void (*)(void*) noexcept, void*)`, so one model encodes on two executors at once:

```cpp
app::task encode_both(uf::model_t const& model, uf::executor_t const& east_executor, uf::executor_t const& west_executor,
                      uf::tape_view<char> texts, app::loop& loop) {
    auto [east_arena, east_arena_status] = uf::arena_t::make({.executor = east_executor.handle()});
    auto [west_arena, west_arena_status] = uf::arena_t::make({.executor = west_executor.handle()});
    uf::deferred<uf::encoded_t> east_call = model.deferred(east_executor)
                                                .text_encode(texts, uf::encode_outputs_t::embeddings_k,
                                                             uform_scalar_f32_k, east_arena);
    uf::deferred<uf::encoded_t> west_call = model.deferred(west_executor)
                                                .text_encode(texts, uf::encode_outputs_t::embeddings_k,
                                                             uform_scalar_f32_k, west_arena);
    auto [east_encoded, east_status] = co_await east_call.wait(loop); // both calls were already running
    auto [west_encoded, west_status] = co_await west_call.wait(loop);
    if (east_status.failed() || west_status.failed()) co_return;
    merge(east_encoded, west_encoded);
}
```

A `deferred` never moves, since the library writes into it, so it lives on the caller’s stack or in the awaiting coroutine’s frame. `status` answers `status_code_t::deferred_k` until the call is final, then its final status, and `wait()` blocks until then and answers what a blocking call would have. `co_await call.wait(loop)` suspends the coroutine and re-polls the status through `loop`, any type with `post(void (*)(void*) noexcept, void*)`, so the awaiter posts two words and allocates nothing, as Rust’s waker re-wakes itself. The library has no wake-up, so a scheduler runs other posted work between polls, and its own coroutine type owns the frame. Destroying an unfinished `deferred` waits for the call.

The library reads a deferred call’s inputs after the verb returns, so the tapes, spans, handlers and reports a deferred verb names must outlive the call, which C++ leaves to the caller. A deferred call allocates nothing: its outcome and outputs live inside the `deferred` itself. A refusal the SDK catches before the call crosses answers a `deferred` that is final from the start. An executor without threads of its own may run a deferred call at submission, final when the verb returns.

## API

### Types

#### `awaiter`

A [`deferred`](https://www.unum.cloud/docs/uform/cpp/training.md#deferred) being awaited, living in the awaiting coroutine's frame: each poll reads the status word, and one not yet final posts itself again.

[types.hpp:176 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/cpp/uform/types.hpp#L176)

```cpp
template <typename value_type_,
          a_scheduler scheduler_type_>
class awaiter
```

#### `answer_value`

What a final `answer_type_` carries beside its status, `void` for a bare `status_t`.

[types.hpp:205 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/cpp/uform/types.hpp#L205)

```cpp
template <typename answer_type_>
struct answer_value
```

#### `progress_t`

A progress handler the caller keeps for one call, passed as its own state; empty hears nothing.

[types.hpp:510 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/cpp/uform/types.hpp#L510)

```cpp
class progress_t
```

#### Class `deferred`

A submitted call: its outcome and its outputs, owned until the call is final, while the receiver, inputs, arena and handlers it was handed stay allocated and unchanged until then.7

[types.hpp:118 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/cpp/uform/types.hpp#L118)

```cpp
template <typename value_type_ = void>
class deferred
```

It never moves, as the library writes into it, and its one owner polls [`status`](#status), blocks in [`wait`](#wait) or awaits [`wait`](#wait) on its own loop; destruction waits, so nothing the call writes into goes away under it.

##### `answer_type`

What [`wait`](#wait) answers: what the blocking call would have.

[types.hpp:124 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/cpp/uform/types.hpp#L124)

```cpp
using answer_type = std::conditional_t<std::is_void_v<value_type_>, status_t, expected<value_type_>>;
```

##### `deferred`

An empty slot, final from the start, which [`resubmit`](#resubmit) fills.

[types.hpp:127 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/cpp/uform/types.hpp#L127)

```cpp
deferred() noexcept=default;
```

```cpp
template <typename submit_type_>
requires std::invocable<submit_type_ const&, uform_outcome_t*, outputs<value_type_>*>
deferred(submit_type_ const & submit) noexcept;
```

Runs `submit` with this call's outcome and outputs, where the library reports.

```cpp
deferred(status_t status) noexcept;
```

A call refused before it crossed, final from the start.

```cpp
deferred(deferred const &)=delete;
```

##### `resubmit`

Waits only while the slot's previous call still runs, then runs `submit` in its place.

[types.hpp:139 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/cpp/uform/types.hpp#L139)

```cpp
template <typename submit_type_>
requires std::invocable<submit_type_ const&, uform_outcome_t*, outputs<value_type_>*>
void resubmit(submit_type_ const & submit) noexcept;
```

##### `operator=`

[types.hpp:150 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/cpp/uform/types.hpp#L150)

```cpp
deferred & operator=(deferred const &)=delete;
```

##### `~deferred`

[types.hpp:151 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/cpp/uform/types.hpp#L151)

```cpp
~deferred() noexcept;
```

##### `status`

[`status_code_t::deferred_k`](https://www.unum.cloud/docs/uform/cpp/errors.md#deferred_k) until the call is final, then its final status, polled without blocking.

[types.hpp:155 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/cpp/uform/types.hpp#L155)

```cpp
status_code_t status() noexcept;
```

##### `wait`

Blocks until the call is final and answers what a blocking call would have.

[types.hpp:158 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/cpp/uform/types.hpp#L158)

```cpp
answer_type wait() noexcept;
```

```cpp
template <a_scheduler scheduler_type_>
awaiter<value_type_, scheduler_type_> wait(
    scheduler_type_ & loop) noexcept;
```

Awaits the call on `loop`, which polls its status until it is final.

### Constants

#### `is_deferred`

Whether `answer_type_` is a call the caller holds rather than a final answer.

[types.hpp:199 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/cpp/uform/types.hpp#L199)

```cpp
bool is_deferred;
```
