Examples for using the result type.
Examples for using the result type.
#include <stdio.h>
#include <stdlib.h>
};
switch (self) {
return "NOT_FOUND";
return "INVALID";
return "PERMISSION_DENIED";
}
return "UNKNOWN";
}
}
};
fprintf(
stream,
"example_data@%p {\n", (
void*)self);
}
#define OK struct example_data
#define OK_DELETE example_data_delete
#define OK_DEBUG example_data_debug
#define ERROR enum error_kind
#define ERROR_DEBUG error_kind_debug
#define NAME example_result
success_result = example_result_from_ok((
struct example_data){
.value = 42,
.description = strdup("A successful result"),
});
fprintf(stdout, "\n");
fprintf(stdout, "\n");
fprintf(stdout, "\n");
fprintf(stdout, "\n");
DC_ASSERT(!example_result_is_error(&success_result));
DC_ASSERT(example_result_is_error(&error_result));
DC_ASSERT(example_result_is_error(&invalid_result));
DC_ASSERT(example_result_is_error(&permission_result));
struct example_data const* ok_data = example_result_get_okay(&success_result);
enum error_kind const* error_data = example_result_get_error(&error_result);
enum error_kind const* invalid_data = example_result_get_error(&invalid_result);
enum error_kind const* permission_data = example_result_get_error(&permission_result);
DC_ASSERT(example_result_get_okay(&error_result) == NULL);
DC_ASSERT(example_result_get_error(&success_result) == NULL);
}
return 0;
}
static void example_data_debug(struct example_data const *self, dc_debug_fmt fmt, FILE *stream)
static void example_data_delete(struct example_data *self)
static DC_PUBLIC void dc_debug_fmt_print(dc_debug_fmt fmt, FILE *stream, const char *format,...)
static DC_PUBLIC dc_debug_fmt dc_debug_fmt_new()
static DC_PUBLIC dc_debug_fmt dc_debug_fmt_scope_end(dc_debug_fmt fmt)
static DC_PUBLIC dc_debug_fmt dc_debug_fmt_scope_begin(dc_debug_fmt fmt)
#define DC_ASSERT(expr,...)
static void error_kind_debug(enum error_kind const *self, dc_debug_fmt, FILE *stream)
static char const * error_kind_to_str(enum error_kind self)
static void example_result_example()
@ ERROR_KIND_PERMISSION_DENIED
#define DC_SCOPED(type,...)
RAII in C. Call the destructor when the variable goes out of scope.
Debug format helpers for debug printin data structures.
static DC_PUBLIC FILE * stream(SELF *self)