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

View File

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