include/bedrock/result.h file

A value and an error returned together.

A result carries a value and a ResultError. ok selects which one the caller reads. The caller propagates that object. Success-path macros expand in place.

Classes

struct ResultError
Failure attached to a result.

Typedefs

using ResultError = struct ResultError
Failure attached to a result.

Functions

static auto result_error(const int code, const char* name, const char* message, const char* file, const int line) -> ResultError
Build an error value.
auto result_abort(const ResultError* error, const char* expectation, const char* file, int line) -> _Noreturn void
Print an error and abort the process.

Defines

#define BD_RESULT_ERROR(code, name, message)
Build an error stamped with the call site.
#define BD_DEFINE_RESULT(Name, T)
Define a result type that holds a T and a ResultError.
#define BD_RESULT_OK(Type, ok_value)
Success result of type Type.
#define BD_RESULT_ERR(Type, failure)
Failure result of type Type.
#define BD_RESULT_IS_OK(result)
True when result.ok is true.
#define BD_RESULT_IS_ERR(result)
True when result.ok is false.
#define BD_RESULT_TRY(Type, out, expr)
Assign a successful value, or return the error from this function.
#define BD_RESULT_EXPECT(Type, out, expr, expectation)
Assign a successful value, or abort with expectation.
#define BD_RESULT_UNWRAP(Type, out, expr)
Assign a successful value, or abort.
#define BD_RESULT_UNWRAP_OR(result, fallback)
The success value, or fallback when result failed.

Typedef documentation

typedef struct ResultError ResultError

Failure attached to a result.

code is 0 when the operation succeeded. A failure uses a non-zero code. name, message, and file are not copied; they must outlive the error. String literals and __FILE__ do.

Function documentation

static ResultError result_error(const int code, const char* name, const char* message, const char* file, const int line)

Build an error value.

Parameters
code Failure code. Zero means success; a failure uses a non-zero code.
name Short stable name. May be NULL.
message Human-readable detail. May be NULL.
file Source file. May be NULL.
line Source line.
Returns The error value. No allocation.

The string pointers are stored as given and are not copied.

_Noreturn void result_abort(const ResultError* error, const char* expectation, const char* file, int line)

Print an error and abort the process.

Parameters
error Error to report. May be NULL.
expectation Extra note, usually from BD_RESULT_EXPECT. May be NULL.
file Call site file. May be NULL.
line Call site line.

BD_RESULT_UNWRAP and BD_RESULT_EXPECT call this when ok is false. A NULL error is reported as an unknown failure. A NULL or empty expectation omits the extra note.

Define documentation

#define BD_DEFINE_RESULT(Name, T)

Define a result type that holds a T and a ResultError.

Parameters
Name Result type name.
T Value type stored on success.

ok is true on success and value holds the T. error is zeroed on success. On failure ok is false, error holds the failure, and value is zeroed. T is copied by assignment and must not contain a comma; typedef such a type first. Define each name once in a translation unit. The macro includes the typedef's semicolon.

#define BD_RESULT_TRY(Type, out, expr)

Assign a successful value, or return the error from this function.

Parameters
Type Result type from BD_DEFINE_RESULT.
out Destination for the success value.
expr Expression of type Type.

Evaluates expr once. The enclosing function returns Type, and expr has that same type. On failure out is left unchanged and the function returns the error. On success out receives the value.

#define BD_RESULT_EXPECT(Type, out, expr, expectation)

Assign a successful value, or abort with expectation.

Parameters
Type Result type from BD_DEFINE_RESULT.
out Destination for the success value.
expr Expression of type Type.
expectation Note stored with the abort. May be NULL.

Evaluates expr once. On failure this calls result_abort and does not return. On success out receives the value.

#define BD_RESULT_UNWRAP(Type, out, expr)

Assign a successful value, or abort.

Parameters
Type Result type from BD_DEFINE_RESULT.
out Destination for the success value.
expr Expression of type Type.

Evaluates expr once. On failure this calls result_abort and does not return. On success out receives the value.

#define BD_RESULT_UNWRAP_OR(result, fallback)

The success value, or fallback when result failed.

Parameters
result Result object.
fallback Value used when result.ok is false.

result is evaluated more than once when it succeeded, so pass a variable. fallback is evaluated only on failure.