Skip to content
This repository was archived by the owner on May 3, 2026. It is now read-only.

Commit 43de6d0

Browse files
committed
add Result::transform
1 parent 963d670 commit 43de6d0

1 file changed

Lines changed: 18 additions & 0 deletions

File tree

include/common/result.hpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,24 @@ class Result<T, Errs...> {
228228
}, std::forward<Self>(self).error);
229229
}
230230

231+
/**
232+
* @brief If this Result instance does not contain an error, apply a callable to the expected
233+
* value
234+
*
235+
* @tparam Self deduced self type
236+
* @tparam F the type of the callable
237+
* @param self the current Result instance
238+
* @param f the callable
239+
*/
240+
template<typename Self, typename F>
241+
requires std::invocable<F, Self> && std::convertible_to<std::invoke_result_t<F, Self>, Self>
242+
constexpr Self transform(this Self&& self, F&& f) {
243+
if (self.has_error()) {
244+
return std::forward<Self>(self);
245+
}
246+
return std::invoke(f, std::forward<Self>(self));
247+
}
248+
231249
constexpr operator T&() & {
232250
return value;
233251
}

0 commit comments

Comments
 (0)