Nix 2.36.0
Nix, the purely functional package manager; unstable internal interfaces
Loading...
Searching...
No Matches
nix::Goal::Co< T > Struct Template Reference

#include <goal.hh>

Inheritance diagram for nix::Goal::Co< T >:
nix::Goal::CoBase

Public Member Functions

 Co (HandleType< T > h) noexcept
AwaitableFrame< T > & frame () const
Public Member Functions inherited from nix::Goal::CoBase
 CoBase (CoBase &&rhs) noexcept
CoBase & operator= (CoBase &&rhs) noexcept
 CoBase (const CoBase &)=delete
CoBase & operator= (const CoBase &)=delete

Additional Inherited Members

Protected Member Functions inherited from nix::Goal::CoBase
 CoBase (std::coroutine_handle<> h) noexcept
Protected Attributes inherited from nix::Goal::CoBase
std::coroutine_handle handle

Detailed Description

template<typename T>
struct nix::Goal::Co< T >

C++20 coroutine wrapper for use in goal logic. Coroutines are functions that use co_await/co_return (and co_yield, but not supported by Co).

Co is meant to be used by methods of subclasses of Goal. The main functionality provided by Co is

  • co_await Suspend{}: Suspends the goal.
  • co_await f(): Waits until f() finishes.
  • co_return value: Ends coroutine. The top-level Co<ExitCode> returns the goal's exit code, which work turns into goal completion.

The idea is that you implement the goal logic using coroutines, and do the core thing a goal can do, suspension, when you have children you're waiting for. Coroutines allow you to resume the work cleanly.

Note
Brief explanation of C++20 coroutines: When you Co f(), a std::coroutine_handle<promise_type> is created, alongside its promise_type. There are suspension points at the beginning of the coroutine, at every co_await, and at the final (possibly implicit) co_return. Once suspended, you can resume the std::coroutine_handle by doing coroutine_handle.resume(). Suspension points are implemented by passing a struct to the compiler that implements await_suspend. await_suspend can either say "cancel suspension", in which case execution resumes, "suspend", in which case control is passed back to the caller of coroutine_handle.resume() or the place where the coroutine function is initially executed in the case of the initial suspension, or await_suspend can specify another coroutine to jump to.
Resources:

The documentation for this struct was generated from the following file:
  • libstore/include/nix/store/build/goal.hh