Nix 2.93.3
Lix: A modern, delicious implementation of the Nix package manager; unstable internal interfaces
Loading...
Searching...
No Matches
nix::Generator< T, Transform > Struct Template Reference

#include <generator.hh>

Classes

class  iterator

Public Types

using promise_type = _generator::promise<T, Transform>

Public Member Functions

 Generator (const Generator &)=delete
Generator & operator= (const Generator &)=delete
 Generator (Generator &&)=default
Generator & operator= (Generator &&)=default
std::optional< T > next ()
Generator< T, void > decay () &&
 operator Generator< T, void > () &&
iterator begin ()
iterator end ()

Friends

template<typename, typename>
struct _generator::promise

Detailed Description

template<typename T, typename Transform = void>
struct nix::Generator< T, Transform >

Coroutine-based iterator modeled loosely on Rust std::iter::Iterator interface. Like Rust's Iterator and unlike common C++ iterators, a Generator returns std::optional<T> values from its next() function, but unlike both it can also transform items produced within using a Transform function object the Generator holds before returning them via next(). To allow generator nesting a Transform may also return another Generator instance for any yielded value, in this case the new Generator will temporarily take priority over the previously running one and have its values returned until it is exhausted, then return to the previous Generator. This mechanism may nest Generator to arbitrary depths.

Template Parameters
Titem type
Transformtransform function object type, or void for no transform

Member Function Documentation

◆ decay()

template<typename T, typename Transform = void>
Generator< T, void > nix::Generator< T, Transform >::decay ( ) &&
inline

Type-erases the Transform.

Returns
a new Generator with the Transform type-erased

◆ next()

template<typename T, typename Transform = void>
std::optional< T > nix::Generator< T, Transform >::next ( )
inline

If the coroutine held by the Generator has not finished, runs it until it yields a value, throws any exception, or returns. If the coroutine yields a value this value is passed to a persistent instance of Transform that is held by the Generator, and the result of this call is returned. If the coroutine throws an exception, or the Transform throws an exception while processing an item, that exception is rethrown and the Generator will not return any more non-std::nullopt values from next(). Once the contained coroutine has completed or an exception has been thrown the Generator can no longer return any valid values, only std::nullopt. Exceptions thrown are thrown only once, further invocations of next() return std::nullopt.

Returns
std::nullopt if the coroutine has completed, or a value

◆ operator Generator< T, void >()

template<typename T, typename Transform = void>
nix::Generator< T, Transform >::operator Generator< T, void > ( ) &&
inline

Type-erases the Transform.

Returns
a new Generator with the Transform type-erased

The documentation for this struct was generated from the following file: