r/cprogramming • • 4d ago

Added a pure C99, Rust-inspired error handling module (<cerror.h>) to my library arstd

https://github.com/mainak55512/arstd

Just added <cerror.h> to arstd (my C99 arena standard library) to bring Rust-style Result ergonomics to strict ISO C99—with zero compiler extensions (no typeof or statement expressions) so it compiles cleanly across GCC, Clang, and MSVC.

It includes typed Result wrappers, 3-arg and 4-arg TRY propagation, UNWRAP/EXPECT/UNWRAP_OR, and an optional custom error table with built-in CERROR_OK and CERROR_DEFAULT fallbacks:


CERROR_RESULT(int, ResInt);

CERROR_RESULT(const char *, ResStr);

ResInt parse_port(int port) {

if (port < 1 || port > 65535) return ERR(ResInt, cerror("ERR_INVALID_PORT"));

return OK(ResInt, port);

}

ResStr connect_server(int port) {

int valid_port;

TRY(valid_port, ResStr, ResInt, parse_port(port)); // early-returns Err on failure

// work with valid_port...

return OK(ResStr, "Connected");

}

Repo: https://github.com/mainak55512/arstd

0 Upvotes

2 comments sorted by

1

u/Key_River7180 4d ago

i am happy with tuples thx