|
10 | 10 |
|
11 | 11 | namespace ystdlib::error_handling { |
12 | 12 | /** |
13 | | - * A convenience alias for Outcome's std_result. |
| 13 | + * A Rust-style `Result<T, E>` type for standardized, exception-free error handling. |
14 | 14 | * |
15 | 15 | * This alias standardizes error handling across the codebase by defaulting the error type to |
16 | | - * `std::error_code`, which interoperates with the ystdlib::error_handling::ErrorCode framework, |
17 | | - * making it easier to compose errors and propagate them across different modules and libraries. |
| 16 | + * `std::error_code`, which interoperates with the `ystdlib::error_handling::ErrorCode`, making it |
| 17 | + * easier to compose errors and propagate them across different modules and libraries. |
18 | 18 | * |
19 | | - * @tparam ReturnType The type returned upon success. |
20 | | - * @tparam ErrorType The type returned upon failure. |
| 19 | + * @tparam ReturnType The type returned on success. |
| 20 | + * @tparam ErrorType The type used to represent errors. |
21 | 21 | */ |
22 | 22 | template <typename ReturnType, typename ErrorType = std::error_code> |
23 | 23 | using Result = OUTCOME_V2_NAMESPACE::std_result<ReturnType, ErrorType>; |
24 | 24 |
|
25 | 25 | /** |
26 | | - * Default return value for ystdlib::error_handling::Result<void> when function succeeds. |
27 | | - * |
28 | | - * Example: |
29 | | - * auto my_func() -> Result<void> { |
30 | | - * // ... |
31 | | - * return success(); |
32 | | - * } |
| 26 | + * @return A value indicating successful completion of a function that returns a void result (i.e., |
| 27 | + * `Result<void, E>`). |
33 | 28 | */ |
34 | 29 | [[nodiscard]] inline auto success() -> OUTCOME_V2_NAMESPACE::success_type<void> { |
35 | 30 | return OUTCOME_V2_NAMESPACE::success(); |
36 | 31 | } |
37 | 32 |
|
38 | 33 | /** |
39 | | - * Propagates errors like Rust's `?` operator. |
| 34 | + * A function-style macro that emulates Rust’s try (`?`) operator for error propagation. |
| 35 | + * |
| 36 | + * @param expr An expression that evaluates to a `Result` object. |
40 | 37 | * |
41 | | - * Evaluates `expr`, and if it contains an error, returns the error from the calling function. |
42 | | - * Otherwise, extracts and returns the value. |
| 38 | + * Behavior: |
| 39 | + * - If `expr` represents an error (i.e., `expr.has_error()` returns true), the macro performs an |
| 40 | + * early return from the enclosing function with the contained error. |
| 41 | + * - Otherwise, it unwraps and yields the successful value as an rvalue reference (`expr.value()`). |
43 | 42 | * |
44 | | - * Only supported on AppleClang, Clang, and GCC due to reliance on Outcome's TRY macros. |
| 43 | + * NOTE: This macro is only supported on GCC and Clang due to reliance on compiler-specific |
| 44 | + * extensions. |
45 | 45 | */ |
| 46 | +#ifdef OUTCOME_TRYX |
46 | 47 | // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) |
47 | 48 | #define YSTDLIB_ERROR_HANDLING_TRYX(expr) (OUTCOME_TRYX(expr)) |
| 49 | +#endif |
48 | 50 |
|
49 | 51 | /** |
50 | | - * Error propagation macro for expressions that return void on success. |
| 52 | + * A function-style macro for propagating errors from expressions that evaluate to a void result |
| 53 | + * (`Result<void, E>`). |
51 | 54 | * |
52 | | - * Evaluates `expr`, and if it contains an error, returns the error from the calling function. |
53 | | - * Intended for use with expressions that return `Result<void>`. |
| 55 | + * @param expr An expression that evaluates to a `Result<void, E>` object. |
54 | 56 | * |
55 | | - * Only supported on AppleClang, Clang, and GCC due to reliance on Outcome's TRY macros. |
| 57 | + * Behavior: |
| 58 | + * - If `expr` represents an error (i.e., `expr.has_error()` returns true), the macro performs an |
| 59 | + * early return from the enclosing function with the contained error. |
| 60 | + * - Otherwise, execution continues normally. |
56 | 61 | */ |
57 | 62 | // NOLINTBEGIN(cppcoreguidelines-avoid-do-while, cppcoreguidelines-macro-usage) |
58 | 63 | #define YSTDLIB_ERROR_HANDLING_TRYV(expr) \ |
|
0 commit comments