RFC: Use more enums
Convert modbus_rtu_mode to a enum instead of two #define. This then extends the idea by enforcing compiler checking that these values are more correctly used.
Questions:
-
Return Types?
MODBUS_API int modbus_rtu_get_serial_mode(modbus_t *ctx);vsMODBUS_API modbus_rtu_mode modbus_rtu_get_serial_mode(modbus_t *ctx);As the return value is bothmodbus_rtu_modeand-1(for error) should it be typed tomodbus_rtu_modeorint. Both are valid answers inCbut I believemodbus_rtu_modewould lead to incorrect assumptions. Would aunionbe a more clear response? -
Is the idea sounds? If so I believe that there is more examples that would work well as an enum. Example
#define MODBUS_RTU_RTS_NONE 0
#define MODBUS_RTU_RTS_UP 1
#define MODBUS_RTU_RTS_DOWN 2
Does it make the API better? Will it cause API issues in the feature to add/remove values?
- Enum naming?
Should the name include
_t?modbus.h: modbus_error_recovery_modemodbus-private.h: modbus_backend_type_t
I don't want to change the return types (when error or enum could be returned), I think we should accept that as a limitation of the C language (Rust rocks here...) but I like the idea to leverage the compiler to help us.