1
1
mirror of https://github.com/trafi/maybe-result-cpp synced 2024-11-22 02:32:02 +01:00

Use more precise into_err result.

This commit is contained in:
Nerijus Arlauskas 2016-09-22 15:35:07 +03:00
parent f41e0e0f67
commit eb446874f5
2 changed files with 66 additions and 38 deletions

@ -146,7 +146,7 @@ namespace maybe {
OPTIONAL_MUTABLE_CONSTEXPR T&& ok_value() &&
{
return std::move(var_ok.value());
return var_ok.value();
}
#else
@ -156,7 +156,7 @@ namespace maybe {
*
* @return T
*/
T const& ok_value() const
constexpr T const& ok_value() const
{
return var_ok.value();
}
@ -173,15 +173,27 @@ namespace maybe {
template <class V>
constexpr T ok_value_or(V&& v) const&
{
return var_ok.value_or(std::forward<V>(v));
return var_ok.value_or(std::experimental::constexpr_forward<V>(v));
}
#if OPTIONAL_HAS_MOVE_ACCESSORS == 1
template <class V>
OPTIONAL_MUTABLE_CONSTEXPR T ok_value_or(V&& v) &&
{
return var_err.value_or(std::forward<V>(v));
return var_ok.value_or(std::experimental::constexpr_forward<V>(v));
}
#else
template <class V>
T ok_value_or(V&& v) &&
{
return var_ok.value_or(std::experimental::constexpr_forward<V>(v));
}
#endif
#else
/**
@ -190,7 +202,7 @@ namespace maybe {
* @return T
*/
template <class V>
T ok_value_or(V&& v) const
constexpr T ok_value_or(V&& v) const
{
return var_ok.value_or(std::forward<V>(v));
}
@ -211,7 +223,7 @@ namespace maybe {
OPTIONAL_MUTABLE_CONSTEXPR E&& err_value() &&
{
return std::move(var_err.value());
return var_err.value();
}
#else
@ -221,7 +233,7 @@ namespace maybe {
*
* @return E
*/
E const& err_value() const
constexpr E const& err_value() const
{
return var_err.value();
}
@ -238,15 +250,27 @@ namespace maybe {
template <class V>
constexpr E err_value_or(V&& v) const&
{
return var_err.value_or(std::forward<V>(v));
return var_err.value_or(std::experimental::constexpr_forward<V>(v));
}
#if OPTIONAL_HAS_MOVE_ACCESSORS == 1
template <class V>
OPTIONAL_MUTABLE_CONSTEXPR E err_value_or(V&& v) &&
{
return var_err.value_or(std::forward<V>(v));
return var_err.value_or(std::experimental::constexpr_forward<V>(v));
}
#else
template <class V>
E err_value_or(V&& v) &&
{
return var_err.value_or(std::experimental::constexpr_forward<V>(v));
}
#endif
#else
/**
@ -334,14 +358,14 @@ namespace maybe {
inline auto and_then(F op) noexcept -> typename std::result_of<F(T)>::type;
/**
* Converts into another result with different ok type `R` and forwards the same error.
* Converts into another result with different ok type `U` and forwards the same error.
*
* The ok type `R` must have `R()` constructor in case the result does not contain an err.
* The ok type `U` must have `U()` constructor in case the result does not contain an err.
*
* @return maybe::result<R, E>
* @return maybe::result<U, E>
*/
template <typename R>
inline auto into_err() noexcept -> R;
template <typename U>
inline auto into_err() noexcept -> maybe::result<U, E> const;
};
template <typename T, typename E>
@ -371,6 +395,14 @@ namespace maybe {
{
}
result(internal::placeholder, E&& value) : var_err(std::forward<E>(value))
{
}
result(internal::placeholder, const E& value) : var_err(value)
{
}
result(E&& value) : var_err(std::forward<E>(value))
{
}
@ -591,14 +623,11 @@ namespace maybe {
inline auto and_then(F op) noexcept -> typename std::result_of<F()>::type;
/**
* Converts into another result with different ok type `R` and forwards the same error.
* Converts into another result with ok type void and forwards the same error.
*
* The ok type `R` must have `R()` constructor in case the result does not contain an err.
*
* @return maybe::result<R, E>
* @return maybe::result<void, E>
*/
template <typename R>
inline auto into_err() noexcept -> R;
inline auto into_err() noexcept -> maybe::result<void, E>;
};
template <typename E>

@ -20,7 +20,7 @@ inline auto maybe::result<T, E>::map(F f) noexcept -> maybe::result<R, E>
return maybe::result<R, E>::err(std::forward<E>(err_value()));
}
return maybe::result<R, E>::ok(f(ok_value()));
return maybe::result<R, E>(f(ok_value()), internal::placeholder{});
};
template <typename T, typename E>
@ -30,7 +30,7 @@ inline auto maybe::result<T, E>::map_void() noexcept -> maybe::result<void, E>
return maybe::result<void, E>::err(std::forward<E>(err_value()));
}
return maybe::result<void, E>::ok();
return maybe::result<void, E>();
};
template <typename T, typename E>
@ -41,7 +41,7 @@ inline auto maybe::result<T, E>::map_value(U value) noexcept -> maybe::result<U,
return maybe::result<U, E>::err(std::forward<E>(err_value()));
}
return maybe::result<U, E>::ok(std::forward<U>(value));
return maybe::result<U, E>(std::forward<U>(value), internal::placeholder{});
};
template <typename T, typename E>
@ -52,9 +52,9 @@ inline auto maybe::result<T, E>::map_err(F f) noexcept
typedef maybe::result<T, typename std::result_of<F(E)>::type> return_result_t;
if (is_ok()) {
return return_result_t::ok(std::forward<T>(ok_value()));
return return_result_t(std::forward<T>(ok_value()), internal::placeholder{});
}
return return_result_t::err(f(err_value()));
return return_result_t(internal::placeholder{}, f(err_value()));
};
template <typename T, typename E>
@ -62,7 +62,7 @@ template <typename U>
inline auto maybe::result<T, E>::map_err_value(U value) noexcept -> maybe::result<T, U>
{
if (is_ok()) {
return maybe::result<T, U>::ok(std::forward<T>(ok_value()));
return maybe::result<T, U>(std::forward<T>(ok_value()), internal::placeholder{});
}
return maybe::result<T, U>::err(std::forward<U>(value));
@ -81,13 +81,13 @@ inline auto maybe::result<T, E>::and_then(F f) noexcept -> typename std::result_
};
template <typename T, typename E>
template <typename R>
inline auto maybe::result<T, E>::into_err() noexcept -> R
template <typename U>
inline auto maybe::result<T, E>::into_err() noexcept -> maybe::result<U, E> const
{
if (is_err()) {
return R::err(std::forward<E>(err_value()));
return maybe::result<U, E>(internal::placeholder{}, std::forward<E>(err_value()));
}
return R::default_ok();
return maybe::result<U, E>::default_ok();
};
template <typename E>
@ -100,7 +100,7 @@ inline auto maybe::result<void, E>::map(F f) noexcept
if (is_err()) {
return return_result_t::err(std::forward<E>(err_value()));
}
return return_result_t::ok(f());
return return_result_t(f(), internal::placeholder{});
};
template <typename E>
@ -111,7 +111,7 @@ inline auto maybe::result<void, E>::map_value(U value) noexcept -> maybe::result
return maybe::result<U, E>::err(std::forward<E>(err_value()));
}
return maybe::result<U, E>::ok(std::forward<U>(value));
return maybe::result<U, E>(std::forward<U>(value), internal::placeholder{});
};
template <typename E>
@ -124,7 +124,7 @@ inline auto maybe::result<void, E>::map_err(F f) noexcept
if (is_ok()) {
return return_result_t::ok();
}
return return_result_t::err(f(err_value()));
return return_result_t(internal::placeholder{}, f(err_value()));
};
template <typename E>
@ -135,7 +135,7 @@ inline auto maybe::result<void, E>::map_err_value(U value) noexcept -> maybe::re
return maybe::result<void, U>::ok();
}
return maybe::result<void, U>::err(std::forward<U>(value));
return maybe::result<void, U>(std::forward<U>(value));
};
template <typename E>
@ -151,11 +151,10 @@ inline auto maybe::result<void, E>::and_then(F f) noexcept -> typename std::resu
};
template <typename E>
template <typename R>
inline auto maybe::result<void, E>::into_err() noexcept -> R
inline auto maybe::result<void, E>::into_err() noexcept -> maybe::result<void, E>
{
if (is_err()) {
return R::err(std::forward<E>(err_value()));
return maybe::result<void, E>(std::forward<E>(err_value()));
}
return R::default_ok();
return maybe::result<void, E>::default_ok();
};