fixup! docs(c_test): Add a poc regarding results in c

This commit is contained in:
Benedikt Peetz 2024-05-20 15:15:07 +02:00
parent 644342070f
commit 4b7cd3a0b7
Signed by: bpeetz
GPG Key ID: B6139BCB07CE946D
2 changed files with 144 additions and 111 deletions

View File

@ -10,9 +10,9 @@
* reporting what happened. Alternatively, many C libraries allow a callback to
* be logged in order to report a detailed description of any errors that
* occurred. However, the programming language known as Rust deliberately left
* out exceptions; instead they opted for return values to report errors, with a
* `RESULT` type. This type contains the actual value if there is one, or some
* error otherwise.
* out exceptions; instead they opted for return values to report errors, with
* a `RESULT` type. This type contains the actual value if there is one, or
* some error otherwise.
*
* This is the approach that this file defines.
*/
@ -39,13 +39,16 @@
* @see RESULT_M_TRY_DECL
*/
#define RESULT_M_TRY(type, dest, result) \
do { \
do \
{ \
RESULT (type) _result_##type##_try_at_##__LINE__ = result; \
if (_result_##type##_try_at_##__LINE__.err) { \
if (_result_##type##_try_at_##__LINE__.err) \
{ \
return _result_##type##_try_at_##__LINE__; \
} \
dest = _result_##type##_try_at_##__LINE__.value; \
} while (0)
} \
while (0)
/**
* @brief A macro which gives the value of a `RESULT` if present, else returns
@ -79,8 +82,8 @@
RESULT_M_TRY (type, dest, result)
/**
* @brief Template for a RESULT class (similar to the one that the language Rust
* has). This is the typename.
* @brief Template for a RESULT class (similar to the one that the language
* Rust has). This is the typename.
* @details This macro expands to a unique name per type, so `RESULT(int)` is
* roughly equivalent to the C++ `RESULT<int>`. Note that
* `RESULT(int*)` will not compile; if you want to use something along
@ -157,8 +160,8 @@
#define RESULT_EXPECT(type) Result_##type##_expect
/**
* @brief Unwraps the `RESULT` into a `type`, returning the supplied alternative
* otherwise
* @brief Unwraps the `RESULT` into a `type`, returning the supplied
* alternative otherwise
* @details If `RESULT_IS_OK`, this returns the `type` value of the `RESULT`.
* Otherwise, this returns the alternative supplied in the function
* arguments.
@ -186,8 +189,8 @@
/**
* @brief Declares a RESULT type for use.
* @details This declares the struct and functions for a given type. Note that
* the functions still need to be defined. The values inside the struct
* are implementation details, and may change at any time; do not use
* the functions still need to be defined. The values inside the
* struct are implementation details, and may change at any time; do not use
* them.
*
* @param template parameter
@ -195,7 +198,8 @@
* @see RESULT_DEFINE
*/
#define RESULT_DECLARE(type) \
typedef struct RESULT(type) { \
typedef struct RESULT (type) \
{ \
type value; \
/* string literals only */ \
const char *err; \
@ -218,21 +222,26 @@
* @see RESULT_DECLARE
*/
#define RESULT_DEFINE(type) \
RESULT(type) RESULT_OK(type)(type value) { \
RESULT (type) RESULT_OK (type) (type value) \
{ \
RESULT (type) res = { .value = value, .err = NULL }; \
return res; \
} \
RESULT(type) RESULT_ERR(type)(const char *err) { \
RESULT (type) RESULT_ERR (type) (const char *err) \
{ \
RESULT (type) res = { .err = err }; \
return res; \
} \
bool RESULT_IS_OK(type)(const RESULT(type) * res) { \
bool RESULT_IS_OK (type) (const RESULT (type) * res) \
{ \
return res->err == NULL; \
} \
bool RESULT_IS_ERR(type)(const RESULT(type) * res) { \
bool RESULT_IS_ERR (type) (const RESULT (type) * res) \
{ \
return res->err != NULL; \
} \
type RESULT_UNWRAP(type)(const RESULT(type) * res) { \
type RESULT_UNWRAP (type) (const RESULT (type) * res) \
{ \
if (RESULT_IS_ERR (type) (res)) \
panicf ("Attempted to unwrap empty Result of type " #type \
". Instead had error: %s", \
@ -240,17 +249,20 @@
return res->value; \
} \
type RESULT_EXPECT (type) (const RESULT (type) * res, \
const char *message_on_err) { \
const char *message_on_err) \
{ \
if (RESULT_IS_ERR (type) (res)) \
panic (message_on_err); \
return res->value; \
} \
type RESULT_UNWRAP_OR(type)(const RESULT(type) * res, type else_val) { \
type RESULT_UNWRAP_OR (type) (const RESULT (type) * res, type else_val) \
{ \
if (RESULT_IS_ERR (type) (res)) \
return else_val; \
return res->value; \
} \
const char *RESULT_UNWRAP_ERR(type)(const RESULT(type) * res) { \
const char *RESULT_UNWRAP_ERR (type) (const RESULT (type) * res) \
{ \
if (RESULT_IS_OK (type) (res)) \
panic ("Result was not an error; type: " #type); \
return res->err; \

View File

@ -7,35 +7,44 @@
typedef const char *string;
enum ResultTag {
enum ResultTag
{
OK,
ERR,
};
Result(u_int32_t, string) {
Result (u_int32_t, string)
{
enum ResultTag tag;
union {
union
{
u_int32_t ok;
string err;
} value;
};
Result(string, ResultTag) {
Result (string, ResultTag)
{
enum ResultTag tag;
union {
union
{
string ok;
enum ResultTag err;
} value;
};
Result(u_int32_t, string) get_a() {
Result (u_int32_t, string) get_a ()
{
int r = rand (); // Returns a pseudo-random integer between 0 and RAND_MAX.
Result (u_int32_t, string) result;
if (r % 2 == 0) {
if (r % 2 == 0)
{
result.value.ok = 32;
result.tag = OK;
} else {
}
else
{
result.value.err = "Can't access a! Have you run the foo?";
result.tag = ERR;
}
@ -43,14 +52,18 @@ Result(u_int32_t, string) get_a() {
return result;
}
Result(string, ResultTag) get_b() {
Result (string, ResultTag) get_b ()
{
int r = rand (); // Returns a pseudo-random integer between 0 and RAND_MAX.
Result (string, ResultTag) result;
if (r % 2 == 0) {
if (r % 2 == 0)
{
result.value.ok = "Wow could defudge the foo!";
result.tag = OK;
} else {
}
else
{
result.value.err = ERR;
result.tag = ERR;
}
@ -58,21 +71,29 @@ Result(string, ResultTag) get_b() {
return result;
}
int main() {
int
main ()
{
srand (time (NULL)); // Initialization, should only be called once.
printf ("Hello World!\n");
Result (u_int32_t, string) result = get_a ();
if (result.tag == OK) {
if (result.tag == OK)
{
printf ("Result is ok with value: '%d'\n", result.value.ok);
} else {
}
else
{
printf ("Result is err with value: '%s'\n", result.value.err);
}
Result (string, ResultTag) result_b = get_b ();
if (result.tag == OK) {
if (result.tag == OK)
{
printf ("Result B is ok with value: '%s'\n", result_b.value.ok);
} else {
}
else
{
printf ("Result B is err with value: '%i'\n", result_b.value.err);
}