Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 37 additions & 10 deletions include/pfn/expected.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,11 +275,33 @@ template <class T, class E> class expected {
rhs.set_ = true;
}

constexpr T const &_value() const &
{
ASSERT(set_);
return v_;
}
constexpr T &_value() &
{
ASSERT(set_);
return v_;
}
constexpr T const &&_value() const &&
{
ASSERT(set_);
return ::std::move(v_);
}
constexpr T &&_value() &&
{
ASSERT(set_);
return ::std::move(v_);
}

template <typename Self, typename Fn>
static constexpr auto _and_then(Self &&self, Fn &&fn) //
noexcept(::std::is_nothrow_invocable_v<Fn, decltype(FWD(self).value())>
&& ::std::is_nothrow_constructible_v<E, decltype(FWD(self).error())>)
requires(::std::is_constructible_v<E, decltype(FWD(self).error())>)
requires(::std::is_invocable_v<Fn, decltype(FWD(self).value())>
&& ::std::is_constructible_v<E, decltype(FWD(self).error())>)
{
using result_t = ::std::remove_cvref_t<::std::invoke_result_t<Fn, decltype(FWD(self).value())>>;
static_assert(detail::_is_some_expected<result_t>);
Expand All @@ -293,14 +315,15 @@ template <class T, class E> class expected {
template <typename Self, typename Fn>
static constexpr auto _or_else(Self &&self, Fn &&fn) //
noexcept(::std::is_nothrow_invocable_v<Fn, decltype(FWD(self).error())>
&& ::std::is_nothrow_constructible_v<T, decltype(FWD(self).value())>)
requires(::std::is_constructible_v<T, decltype(FWD(self).value())>)
&& ::std::is_nothrow_constructible_v<T, decltype(FWD(self)._value())>)
requires(::std::is_invocable_v<Fn, decltype(FWD(self).error())>
&& ::std::is_constructible_v<T, decltype(FWD(self)._value())>)
{
using result_t = ::std::remove_cvref_t<::std::invoke_result_t<Fn, decltype(FWD(self).error())>>;
static_assert(detail::_is_some_expected<result_t>);
static_assert(::std::is_same_v<typename result_t::value_type, typename ::std::remove_cvref_t<Self>::value_type>);
if (self.has_value()) {
return result_t(::std::in_place, FWD(self).value());
return result_t(::std::in_place, FWD(self)._value());
}
return ::std::invoke(FWD(fn), FWD(self).error());
}
Expand All @@ -313,7 +336,8 @@ template <class T, class E> class expected {
|| ::std::is_nothrow_constructible_v<
::std::remove_cv_t<::std::invoke_result_t<Fn, decltype(FWD(self).value())>>,
::std::invoke_result_t<Fn, decltype(FWD(self).value())>>))
requires(::std::is_constructible_v<E, decltype(FWD(self).error())>)
requires(::std::is_invocable_v<Fn, decltype(FWD(self).value())>
&& ::std::is_constructible_v<E, decltype(FWD(self).error())>)
{
using value_t = ::std::remove_cv_t<::std::invoke_result_t<Fn, decltype(FWD(self).value())>>;
static_assert(detail::_is_valid_expected<value_t, E>);
Expand All @@ -333,11 +357,12 @@ template <class T, class E> class expected {
template <typename Self, typename Fn>
static constexpr auto _transform_error(Self &&self, Fn &&fn) //
noexcept(::std::is_nothrow_invocable_v<Fn, decltype(FWD(self).error())>
&& ::std::is_nothrow_constructible_v<T, decltype(FWD(self).value())>
&& ::std::is_nothrow_constructible_v<T, decltype(FWD(self)._value())>
&& ::std::is_nothrow_constructible_v<
::std::remove_cv_t<::std::invoke_result_t<Fn, decltype(FWD(self).error())>>,
::std::invoke_result_t<Fn, decltype(FWD(self).error())>>)
requires(::std::is_constructible_v<T, decltype(FWD(self).value())>)
requires(::std::is_invocable_v<Fn, decltype(FWD(self).error())>
&& ::std::is_constructible_v<T, decltype(FWD(self)._value())>)
{
using error_t = ::std::remove_cv_t<::std::invoke_result_t<Fn, decltype(FWD(self).error())>>;
static_assert(detail::_is_valid_unexpected<error_t>);
Expand All @@ -346,7 +371,7 @@ template <class T, class E> class expected {
if (not self.has_value()) {
return result_t(unexpect, ::std::invoke(FWD(fn), FWD(self).error()));
}
return result_t(::std::in_place, FWD(self).value());
return result_t(::std::in_place, FWD(self)._value());
}

public:
Expand Down Expand Up @@ -962,7 +987,7 @@ class expected<T, E> {
template <typename Self, typename Fn>
static constexpr auto _and_then(Self &&self, Fn &&fn) //
noexcept(::std::is_nothrow_invocable_v<Fn> && ::std::is_nothrow_constructible_v<E, decltype(FWD(self).error())>)
requires(::std::is_constructible_v<E, decltype(FWD(self).error())>)
requires(::std::is_invocable_v<Fn> && ::std::is_constructible_v<E, decltype(FWD(self).error())>)
{
using result_t = ::std::remove_cvref_t<::std::invoke_result_t<Fn>>;
static_assert(detail::_is_some_expected<result_t>);
Expand All @@ -976,6 +1001,7 @@ class expected<T, E> {
template <typename Self, typename Fn>
static constexpr auto _or_else(Self &&self, Fn &&fn) //
noexcept(::std::is_nothrow_invocable_v<Fn, decltype(FWD(self).error())>)
requires(::std::is_invocable_v<Fn, decltype(FWD(self).error())>)
{
using result_t = ::std::remove_cvref_t<::std::invoke_result_t<Fn, decltype(FWD(self).error())>>;
static_assert(detail::_is_some_expected<result_t>);
Expand All @@ -992,7 +1018,7 @@ class expected<T, E> {
&& (::std::is_void_v<::std::invoke_result_t<Fn>>
|| ::std::is_nothrow_constructible_v<::std::remove_cv_t<::std::invoke_result_t<Fn>>,
::std::invoke_result_t<Fn>>))
requires(::std::is_constructible_v<E, decltype(FWD(self).error())>)
requires(::std::is_invocable_v<Fn> && ::std::is_constructible_v<E, decltype(FWD(self).error())>)
{
using value_t = ::std::remove_cv_t<::std::invoke_result_t<Fn>>;
static_assert(detail::_is_valid_expected<value_t, E>);
Expand All @@ -1015,6 +1041,7 @@ class expected<T, E> {
&& ::std::is_nothrow_constructible_v<
::std::remove_cv_t<::std::invoke_result_t<Fn, decltype(FWD(self).error())>>,
::std::invoke_result_t<Fn, decltype(FWD(self).error())>>)
requires(::std::is_invocable_v<Fn, decltype(FWD(self).error())>)
{
using error_t = ::std::remove_cv_t<::std::invoke_result_t<Fn, decltype(FWD(self).error())>>;
static_assert(detail::_is_valid_unexpected<error_t>);
Expand Down
50 changes: 50 additions & 0 deletions tests/pfn/expected.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ using std::unexpected;
#include <catch2/catch_all.hpp>

#include <initializer_list>
#include <memory>
#include <stdexcept>
#include <string>
#include <type_traits>
Expand Down Expand Up @@ -2509,6 +2510,15 @@ TEST_CASE("expected non void", "[expected][polyfill]")
SUCCEED();
}
}

SECTION("move-only type")
{
using T = std::unique_ptr<int>;
expected<T, Error> e(std::in_place, std::make_unique<int>(42));
static_assert(std::is_same_v<decltype(std::move(e)), expected<T, Error> &&>);
auto res = std::move(e).and_then([](T p) -> expected<int, Error> { return *p + 1; });
CHECK(res.value() == 43);
}
}

SECTION("or_else")
Expand Down Expand Up @@ -2569,6 +2579,14 @@ TEST_CASE("expected non void", "[expected][polyfill]")
SUCCEED();
}
}

SECTION("move-only type")
{
using T = std::unique_ptr<int>;
expected<int, T> e(unexpect, std::make_unique<int>(42));
auto res = std::move(e).or_else([](T p) -> expected<int, int> { return unexpected(*p + 1); });
CHECK(res.error() == 43);
}
}

SECTION("transform")
Expand Down Expand Up @@ -2628,6 +2646,14 @@ TEST_CASE("expected non void", "[expected][polyfill]")
SUCCEED();
}
}

SECTION("move-only type")
{
using T = std::unique_ptr<int>;
expected<T, Error> e(std::in_place, std::make_unique<int>(42));
auto res = std::move(e).transform([](T p) { return *p + 1; });
CHECK(res.value() == 43);
}
}

SECTION("transform_error")
Expand Down Expand Up @@ -2685,6 +2711,14 @@ TEST_CASE("expected non void", "[expected][polyfill]")
SUCCEED();
}
}

SECTION("move-only type")
{
using T = std::unique_ptr<int>;
expected<int, T> e(unexpect, std::make_unique<int>(42));
auto res = std::move(e).transform_error([](T p) { return *p + 1; });
CHECK(res.error() == 43);
}
}
}

Expand Down Expand Up @@ -4223,6 +4257,14 @@ TEST_CASE("expected void", "[expected_void][polyfill]")
SUCCEED();
}
}

SECTION("move-only type")
{
using T = std::unique_ptr<int>;
expected<void, T> e(unexpect, std::make_unique<int>(42));
auto res = std::move(e).or_else([](T p) -> expected<void, int> { return unexpected(*p + 1); });
CHECK(res.error() == 43);
}
}

SECTION("transform")
Expand Down Expand Up @@ -4332,6 +4374,14 @@ TEST_CASE("expected void", "[expected_void][polyfill]")
SUCCEED();
}
}

SECTION("move-only type")
{
using T = std::unique_ptr<int>;
expected<void, T> e(unexpect, std::make_unique<int>(42));
auto res = std::move(e).transform_error([](T p) { return *p + 1; });
CHECK(res.error() == 43);
}
}
}

Expand Down