Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve move semantics in Expected #4326

Merged
merged 2 commits into from
Nov 28, 2022
Merged
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
26 changes: 14 additions & 12 deletions src/ripple/basics/Expected.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@
#define RIPPLE_BASICS_EXPECTED_H_INCLUDED

#include <ripple/basics/contract.h>

#include <boost/outcome.hpp>

#include <concepts>
#include <stdexcept>
#include <type_traits>

Expand Down Expand Up @@ -132,17 +135,16 @@ class [[nodiscard]] Expected
using Base = boost::outcome_v2::result<T, E, detail::throw_policy>;

public:
template <
typename U,
typename = std::enable_if_t<std::is_convertible_v<U, T>>>
constexpr Expected(U r) : Base(T{std::forward<U>(r)})
template <typename U>
requires std::convertible_to<U, T> constexpr Expected(U && r)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh. We really need to tune up clang-format for requires clauses. I think this would read way better as...

    template <typename U>
    requires std::convertible_to<U, T>
    constexpr Expected(U&& r)
        : Base(T{std::forward<U>(r)})
    {
    }

But we need to get a few test cases into the code base before we clean that up. Carry on...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is because we use clang 10 for formatting. When I use clang 15 is formats in a reasonable way. I hope we can update our clang format shortly. I also don't mind delaying this patch until after we update formatting.

: Base(T{std::forward<U>(r)})
{
}

template <
typename U,
typename = std::enable_if_t<std::is_convertible_v<U, E>>>
constexpr Expected(Unexpected<U> e) : Base(E{std::forward<U>(e.value())})
template <typename U>
requires std::convertible_to<U, E> &&
(!std::is_reference_v<U>)constexpr Expected(Unexpected<U> e)
: Base(E{std::move(e.value())})
{
}

Expand Down Expand Up @@ -215,10 +217,10 @@ class [[nodiscard]] Expected<void, E>
{
}

template <
typename U,
typename = std::enable_if_t<std::is_convertible_v<U, E>>>
constexpr Expected(Unexpected<U> e) : Base(E{std::forward<U>(e.value())})
template <typename U>
requires std::convertible_to<U, E> &&
(!std::is_reference_v<U>)constexpr Expected(Unexpected<U> e)
: Base(E{std::move(e.value())})
{
}

Expand Down