Skip to content

Commit 044c727

Browse files
committed
prevent ResultError derived class being used as the expected value in Reesult class
1 parent 9f81839 commit 044c727

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

include/common/result.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ namespace zest {
1919
* @tparam Errs List of possible error types. Must inherit from ResultError.
2020
* Must contain at least 1 type. All types must be unique.
2121
*/
22-
template<typename T, traits::IsResultError... Errs>
22+
template<traits::NotResultError T, traits::IsResultError... Errs>
2323
requires traits::NotEmpty<Errs...> && traits::AllUnique<Errs...>
2424
class Result {
2525
public:
@@ -35,7 +35,7 @@ class Result {
3535
* @tparam U Type convertible to T, and U not derived from ResultError
3636
* @param value Value to initialize the result with.
3737
*/
38-
template<typename U>
38+
template<traits::NotResultError U>
3939
requires std::convertible_to<U, T>
4040
constexpr Result(U&& value)
4141
: value(std::forward<U>(value)),
@@ -48,7 +48,7 @@ class Result {
4848
* @param error Error to store.
4949
* @note Requires T to have a defined sentinel value (via SentinelValue<T>).
5050
*/
51-
template<typename U, traits::IsResultError E>
51+
template<traits::NotResultError U, traits::IsResultError E>
5252
requires std::convertible_to<U, T> && traits::InPack<E, Errs...>
5353
constexpr Result(U&& value, E&& error)
5454
: value(std::forward<U>(value)),
@@ -94,7 +94,7 @@ class Result {
9494
* - The fallback value must be convertible to the value type of the Result type returned by the
9595
* callable.
9696
*/
97-
template<typename Self, typename F, typename U>
97+
template<typename Self, typename F, traits::NotResultError U>
9898
requires std::invocable<F, decltype((std::declval<Self>().value))>
9999
&& traits::IsResult<traits::value_return_t<Self, F>>
100100
&& traits::
@@ -181,7 +181,7 @@ class Result {
181181
* @param self the current Result instance
182182
* @param val the value to use if self contains an error
183183
*/
184-
template<typename Self, typename U>
184+
template<typename Self, traits::NotResultError U>
185185
requires std::convertible_to<U, T>
186186
constexpr T or_default(this Self&& self, U&& val) {
187187
if (self.has_error()) {

include/common/result_impl.hpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,12 @@ concept AllUnique = ([]<typename U>(std::type_identity<U>) {
102102
template<typename T>
103103
concept IsResultError = std::is_base_of_v<ResultError, std::remove_cvref_t<T>>;
104104

105+
/**
106+
* @brief Check if a given type is not derived from ResultError
107+
*/
108+
template<typename T>
109+
concept NotResultError = !IsResultError<T>;
110+
105111
/**
106112
* @brief Check whether a given type is in a typename pack
107113
*
@@ -232,7 +238,7 @@ using error_return_t = invoke_result_t<F, decltype((std::get<E>(std::declval<R>(
232238
} // namespace traits
233239

234240
// forward-declare Result
235-
template<typename T, traits::IsResultError... Errs>
241+
template<traits::NotResultError T, traits::IsResultError... Errs>
236242
requires traits::NotEmpty<Errs...> && traits::AllUnique<Errs...>
237243
class Result;
238244

0 commit comments

Comments
 (0)