Skip to content

Commit 8c210ca

Browse files
Apply suggestions from code review
Co-authored-by: Lin Zhihao <[email protected]>
1 parent 6361e25 commit 8c210ca

File tree

1 file changed

+25
-20
lines changed

1 file changed

+25
-20
lines changed

src/ystdlib/error_handling/Result.hpp

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,49 +10,54 @@
1010

1111
namespace ystdlib::error_handling {
1212
/**
13-
* A convenience alias for Outcome's std_result.
13+
* A Rust-style `Result<T, E>` type for standardized, exception-free error handling.
1414
*
1515
* 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.
1818
*
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.
2121
*/
2222
template <typename ReturnType, typename ErrorType = std::error_code>
2323
using Result = OUTCOME_V2_NAMESPACE::std_result<ReturnType, ErrorType>;
2424

2525
/**
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>`).
3328
*/
3429
[[nodiscard]] inline auto success() -> OUTCOME_V2_NAMESPACE::success_type<void> {
3530
return OUTCOME_V2_NAMESPACE::success();
3631
}
3732

3833
/**
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.
4037
*
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()`).
4342
*
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.
4545
*/
46+
#ifdef OUTCOME_TRYX
4647
// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
4748
#define YSTDLIB_ERROR_HANDLING_TRYX(expr) (OUTCOME_TRYX(expr))
49+
#endif
4850

4951
/**
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>`).
5154
*
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.
5456
*
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.
5661
*/
5762
// NOLINTBEGIN(cppcoreguidelines-avoid-do-while, cppcoreguidelines-macro-usage)
5863
#define YSTDLIB_ERROR_HANDLING_TRYV(expr) \

0 commit comments

Comments
 (0)