zyantific / zycore-c

Internal library providing platform independent types, macros and a fallback for environments without LibC.
MIT License
84 stars 49 forks source link

Status code extension #76

Open mochaaP opened 2 months ago

mochaaP commented 2 months ago

I propose two status code extensions:

/* ---------------------------------------------------------------------------------------------- */
/* Status codes (general purpose) - extensions                                                    */
/* ---------------------------------------------------------------------------------------------- */

/**
 * The operation failed with an unexpected error.
 */
#define ZYAN_STATUS_UNEXPECTED ZYAN_MAKE_STATUS(1u, ZYAN_MODULE_ZYCORE, 0xFFFFu)

/**
 * The operation was not implemented.
 */
#define ZYAN_STATUS_NOT_IMPLEMENTED ZYAN_MAKE_STATUS(1u, ZYAN_MODULE_ZYCORE, 0x78u)

p.s. there was also a typo in the comments of Zycore/Status.h:

 /**
- * The operation failed with an generic error.
+ * The operation failed with a generic error.
  */
 #define ZYAN_STATUS_FAILED \
     ZYAN_MAKE_STATUS(1u, ZYAN_MODULE_ZYCORE, 0x01u)
athre0z commented 2 months ago

Where in Zycore would you like to use this?

flobernd commented 2 months ago

UNEXPECTED seems to be the same as the existing FAILED status. Both are used to signal generic failures.

Contrary to exceptions, unexpected errors should not exist with status codes as per design every return value MUST be checked. Not doing that is a bug.

NOT_IMPLEMENTED would be ok to add for me, but is not as useful as a NotImplementedException IMO. It's probably better to call printf() and halt(), abort(), etc in the corpus of an unimplemented function. There is no meaningful runtime action to handle a NOT_IMPLEMENTED code. For the exception case, this is different as it at acts as a shortcut for 1) crashing the program and 2) signaling that the function never returns. In the end, using NOT_IMPLEMENTED or NotImplementedException is always a bug in the code.

mochaaP commented 2 months ago

UNEXPECTED is more like a guard (e.g. __builtin_unreachable) but with a defined behavior instead. NOT_IMPLEMENTED is for platform-dependent support. I hope this clears the confusion.

flobernd commented 2 months ago

Mhh, what would be the benefit of using UNEXPECTED stats code vs the ZYAN_UNREACHABLE macro? IMO unreachable code should really be unreachable. If it is executed, this is clearly a bug and I don't see a valid runtime strategy that a caller should use to handle this status code (besides terminating the application; but then it can as will be done at the root).

Regarding NOT_IMPLEMENTED: I think we have NOT_SUPPORTED for the mentioned use-case.

mochaaP commented 2 months ago

Regarding NOT_IMPLEMENTED: I think we have NOT_SUPPORTED for the mentioned use-case.

Not in Zycore:

image