Last active
March 3, 2018 11:56
-
-
Save SuperPaintman/6c1d30e0f780e7176cc5e4e7899875b5 to your computer and use it in GitHub Desktop.
Rust flavored error handling for C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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