Please name callback types

Hello RIOT developers,

please be so kind as to name the types you are using in RIOT APIs as callbacks. This makes them tangible to downstream APIs (such as the Rust bindings) if they want to stay flexible as to the type’s signature.

This will make my life much easier when trying to stay compatible with non-breaking changes (such as switching between returning an int and a bool) without pinning which exact version of RIOT my code works with.

It is my impression that this practice is widespread in RIOT anyway, please keep it like that.

Thanks for your consideration.

Technical note for those curious about the mechanism I'm using

If there is a callback type, I can build wrappers that return just that type as extracted using a slightly weird trick; that only works as long as I get my hands on the callback type’s name, which is TTBOMK impossible if that type is only defined anonymously in a struct member).

When you say “name”, are you asking us to define a typedef rather than an int 0/1? In the example you cite, where it goes from int->bool, would it have been better if it had mapped to an enum { REPEAT=0, STOP=1 }?

The return values can be one likes. This is about the types of callback.

For example, rather than having

typedef struct {
    bool (*callback)(void *); /**< Called when X happens */
} some_thing_t;

(whose bool may flipflop at some point to int), I prefer to see

/** Type of X event callbacks
 *
 * @ret true if X is the event was blue
 */
typedef bool (*some_callback_t)(void *);
typedef struct {
    some_callback_t callback; /**< Called when X happens */
} some_thing_t;

because then I can pass on a some_callback_t, which is a type I can otherwise neither build myself (because it varies over RIOT releases) nor take from the C source (because it is anonymous).

1 Like