uvco 0.1
Loading...
Searching...
No Matches
Concepts | Functions
Run

Concepts

concept  uvco::MainFunction
 

Functions

Promise< void > uvco::yield ()
 Suspend current coroutine until next event loop iteration.
 
MultiPromise< unsigned > uvco::yield (unsigned count)
 Generate count values from 0 to count - 1.
 
template<typename R , MainFunction< R > F>
uvco::runMain (F main, Scheduler::RunMode mode=Scheduler::RunMode::Deferred)
 

Detailed Description

Function Documentation

◆ runMain()

template<typename R , MainFunction< R > F>
R uvco::runMain ( main,
Scheduler::RunMode  mode 
)

Set up event loop, then run main function to set up promises. Finally, clean up once the event loop has finished. An exception thrown within a coroutine is rethrown here.

MainFunction is a function taking a single const Loop& argument, and returning a Promise<R>. The supplied Loop is necessary to instantiate different types of resources, such as TCP streams or timers.

Example:

runMain<void>([](const Loop& loop) -> Promise<void> {
// Set up resources here.
TtyStream stdin = TtyStream::stdin(loop);
std::optional<std::string> line = co_await stdin.read();
co_await stdin.close();
co_return;
});
Definition loop.h:26
Definition promise.h:76
Promise< std::optional< std::string > > read(size_t maxSize=defaultMaxReadSize)
Definition stream.cc:50
Promise< void > close()
Definition stream.cc:89
Definition stream.h:167
static TtyStream stdin(const Loop &loop)
Definition stream.h:177
51 {
52 Loop loop{mode};
53 Promise<R> promise = main(loop);
54 runLoop(loop);
55 return promise.unwrap();
56}
void runLoop(Loop &loop)
Definition run.cc:32

◆ yield() [1/2]

Promise< void > uvco::yield ( )

Suspend current coroutine until next event loop iteration.

24{ co_await YieldAwaiter_{}; }

◆ yield() [2/2]

MultiPromise< unsigned > uvco::yield ( unsigned  count)

Generate count values from 0 to count - 1.

26 {
27 for (unsigned i = 0; i < count; ++i) {
28 co_yield i;
29 }
30}