#ifndef COMMON_RESULT_H #define COMMON_RESULT_H #include "common/linked.h" #include "common/maybe_unused.h" #include #define RESULT(VALUE, ERROR, STRUCT_NAME) \ typedef struct { \ bool ok; \ union { \ VALUE value; \ ERROR error; \ }; \ } STRUCT_NAME; #define RESULT_CTORS(VALUE, ERROR, STRUCT_NAME, FUNCTION_PREFIX) \ MAYBE_UNUSED static inline STRUCT_NAME FUNCTION_PREFIX##_ok(VALUE value) \ { \ return (STRUCT_NAME) { \ .ok = true, \ .value = value, \ }; \ } \ \ MAYBE_UNUSED static inline STRUCT_NAME FUNCTION_PREFIX##_error(ERROR error \ ) \ { \ return (STRUCT_NAME) { \ .ok = false, \ .error = error, \ }; \ } #define RESULT_WITH_CTORS(VALUE, ERROR, STRUCT_NAME, FUNCTION_PREFIX) \ RESULT(VALUE, ERROR, STRUCT_NAME) \ RESULT_CTORS(VALUE, ERROR, STRUCT_NAME, FUNCTION_PREFIX) #endif