Skip to content

Instantly share code, notes, and snippets.

@SuperPaintman
Last active March 3, 2018 11:56
Show Gist options
  • Save SuperPaintman/6c1d30e0f780e7176cc5e4e7899875b5 to your computer and use it in GitHub Desktop.
Save SuperPaintman/6c1d30e0f780e7176cc5e4e7899875b5 to your computer and use it in GitHub Desktop.
Rust flavored error handling for C
#ifndef RESULT_H_
#define RESULT_H_
// Includes
#include <stdbool.h>
#include <stdint.h>
// Macros
#define RESULT_TEMPLATE(name, typeOk, typeErr) \
typedef struct name { \
bool isOk; \
union { \
typeOk ok; \
typeErr err; \
}; \
} name
#define RESULT_OK(type, val) \
(type) { .isOk = true, .ok = (val) }
#define RESULT_ERR(type, val) \
(type) { .isOk = false, .err = (val) }
// Types
RESULT_TEMPLATE(Result, void *, void *);
#endif // RESULT_H_
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment