A header-only C library implementing Result
and Optional
types inspired by Rust, for modern and safe error handling in C.
- Result types: Explicit error handling without exceptions
- Optional types: Type-safe optional values
- Error chaining: Full error traceability with stack traces
- Error domains: Classification and organization of errors
- Header-only: Easy integration
#include "result.h"
RESULT_TYPE(Int, int);
Result(Int) divide(int a, int b)
{
if (b == 0)
return Fail(Int, MATH_DOMAIN, MATH_ERR_DIV_BY_ZERO);
return Ok(Int, a / b);
}
int main()
{
Result(Int) result = divide(10, 2);
if (is_ok(result))
printf("Result: %d\n", unwrap_ok(result));
else
print_error_chain(stderr, unwrap_error(result));
return 0;
}
#include "result.h"
OPTIONAL_TYPE(Int, int);
Optional(Int) find_value(int arr[], size_t len, int target)
{
for (size_t i = 0; i < len; i++) {
if (arr[i] == target)
return Some(Int, arr[i]);
}
return None(Int);
}
int main()
{
int arr[] = {1, 2, 3, 4, 5};
Optional(Int) result = find_value(arr, 5, 3);
int value = or_some(result, -1);
printf("Found value: %d\n", value);
return 0;
}
meson setup builddir
meson compile -C builddir
./builddir/result_example filename.txt
./builddir/optional_example 42
./builddir/chaining_example
./builddir/map_example
Ok(Type, value)
- Creates a successful resultFail(Type, domain, code)
- Creates an error resultis_ok(result)
/is_error(result)
- State checkingunwrap_ok(result)
- Extracts value (panics on error)or_ok(result, default)
- Returns value or defaultTRY(Type, var, expr)
- Error propagation
Some(Type, value)
- Present valueNone(Type)
- Absent valueis_some(opt)
/is_none(opt)
- Presence checkingunwrap_some(opt)
- Extracts value (panics if absent)or_some(opt, default)
- Returns value or default
See the examples/
directory for complete usage examples:
result.c
- File error handlingoptional.c
- Optional valueschaining.c
- Error chainingmap.c
- Result transformation
MIT License - See LICENSE for details.