Skip to content
Closed
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
26 changes: 22 additions & 4 deletions cpp/src/arrow/status.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,10 @@ class ARROW_EXPORT [[nodiscard]] Status : public util::EqualityComparable<Status
inline bool Equals(const Status& s) const;

// AND the statuses.
inline Status operator&(const Status& s) const noexcept;
inline Status operator&(Status&& s) const noexcept;
inline Status operator&(const Status& s) const& noexcept;
inline Status operator&(const Status& s) && noexcept;
inline Status operator&(Status&& s) const& noexcept;
inline Status operator&(Status&& s) && noexcept;
Comment on lines +160 to +163

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks to me "pass by value" is simpler. To reduce half of the functions. There might be overhead of an additional move, should be trivial.

Status operator&(Status s) const&;
Status operator&(Status s) &&;

@PragmaTwice PragmaTwice Apr 18, 2023

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I do not understand what you mean by the "simpler" that you mentioned.

It should be like this in c++23 or later:

template <typename Self, typename T>
    requires std::same_as<std::remove_cvref_t<T>, Status>
Status operator&(this Self&& self, T&& other) {
    if (ok()) return std::forward<T>(other);
    return std::forward<Self>(self);
}

(hand-written, you can check it by compiler and lang-server)

inline Status& operator&=(const Status& s) noexcept;
inline Status& operator&=(Status&& s) noexcept;

Expand Down Expand Up @@ -422,22 +424,38 @@ bool Status::Equals(const Status& s) const {
/// \cond FALSE
// (note: emits warnings on Doxygen < 1.8.15,
// see https://github.com/doxygen/doxygen/issues/6295)
Status Status::operator&(const Status& s) const noexcept {
Status Status::operator&(const Status& s) const& noexcept {
if (ok()) {
return s;
} else {
return *this;
}
}

Status Status::operator&(Status&& s) const noexcept {
Status Status::operator&(const Status& s) && noexcept {
if (ok()) {
return s;
} else {
return std::move(*this);
}
}

Status Status::operator&(Status&& s) const& noexcept {
if (ok()) {
return std::move(s);
} else {
return *this;
}
}

Status Status::operator&(Status&& s) && noexcept {
if (ok()) {
return std::move(s);
} else {
return std::move(*this);
}
}

Status& Status::operator&=(const Status& s) noexcept {
if (ok() && !s.ok()) {
CopyFrom(s);
Expand Down
15 changes: 15 additions & 0 deletions cpp/src/arrow/status_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,21 @@ TEST(StatusTest, AndStatus) {
res = Status::Invalid("foo") & Status::IOError("bar");
ASSERT_TRUE(res.IsInvalid());

ASSERT_TRUE((a & Status::OK()).ok());
ASSERT_TRUE((c & Status::OK()).IsInvalid());
ASSERT_TRUE((a & Status::IOError("")).IsIOError());
ASSERT_TRUE((c & Status::IOError("")).IsInvalid());

ASSERT_TRUE((Status::OK() & a).ok());
ASSERT_TRUE((Status::OK() & c).IsInvalid());
ASSERT_TRUE((Status::IOError("") & a).IsIOError());
ASSERT_TRUE((Status::IOError("") & c).IsIOError());

ASSERT_TRUE((Status::OK() & Status::OK()).ok());
ASSERT_TRUE((Status::Invalid("") & Status::OK()).IsInvalid());
ASSERT_TRUE((Status::OK() & Status::IOError("")).IsIOError());
ASSERT_TRUE((Status::IOError("") & Status::Invalid("")).IsIOError());

res = Status::OK();
res &= Status::OK();
ASSERT_TRUE(res.ok());
Expand Down