Nix 2.36.0
Nix, the purely functional package manager; unstable internal interfaces
Loading...
Searching...
No Matches
result.hh File Reference
#include "nix/util/error.hh"
#include <boost/outcome/result.hpp>
#include <functional>
#include <type_traits>

Go to the source code of this file.

Macros

#define NIX_THROW_NATIVE_SYS_ERROR_HANDLER(...)

Typedefs

template<typename T, typename E>
using nix::Result = outcome::unchecked<T, E>

Functions

template<typename T, typename E, typename F>
T nix::valueOr (Result< T, E > &&result, F &&f)

Macro Definition Documentation

◆ NIX_THROW_NATIVE_SYS_ERROR_HANDLER

#define NIX_THROW_NATIVE_SYS_ERROR_HANDLER ( ...)
Value:
[&](auto err__) { throw ::nix::NativeSysError(err__, __VA_ARGS__); }

A handler for nix::valueOr that throws a NativeSysError from the error, with the given format string and arguments.

Example:

auto fd = valueOr(openFileReadonly(path), NIX_THROW_NATIVE_SYS_ERROR_HANDLER("opening file %s", PathFmt(path)));

Typedef Documentation

◆ Result

template<typename T, typename E>
using nix::Result = outcome::unchecked<T, E>

Either a T or an error E, for functions whose failures are expected and should be checked by the caller rather than thrown.

Todo
Consider C++23 std::expected instead. Then we could get rid of valueOr because we would already have something similar from the standard library.

Function Documentation

◆ valueOr()

template<typename T, typename E, typename F>
T nix::valueOr ( Result< T, E > && result,
F && f )

The value of result, or else what f returns given its error.

Usually f throws instead, e.g. a NativeSysError built from the error with some context, in which case it need not return anything.