Skip to content

Commit a2c868a

Browse files
nixprimeshentubot
authored andcommitted
Add rvalue ref-qualified PosixErrorOr<T>::ValueOrDie() overloads.
This allows ValueOrDie() to be called on PosixErrorOr rvalues (e.g. temporaries) holding move-only types without extraneous std::move()s. PiperOrigin-RevId: 225098036 Change-Id: I662862e4f3562141f941845fc6e197edb27ce29b
1 parent 24c1158 commit a2c868a

File tree

3 files changed

+23
-9
lines changed

3 files changed

+23
-9
lines changed

test/syscalls/linux/itimer.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ SignalTestResult ItimerSignalTest(int id, clock_t main_clock,
126126
sa.sa_handler = &SignalTestSignalHandler;
127127
sa.sa_flags = SA_RESTART;
128128
sigemptyset(&sa.sa_mask);
129-
auto sigaction_cleanup = std::move(ScopedSigaction(signal, sa).ValueOrDie());
129+
auto sigaction_cleanup = ScopedSigaction(signal, sa).ValueOrDie();
130130

131131
int socketfds[2];
132132
TEST_PCHECK(socketpair(AF_UNIX, SOCK_STREAM, 0, socketfds) == 0);
@@ -167,7 +167,7 @@ SignalTestResult ItimerSignalTest(int id, clock_t main_clock,
167167
struct itimerval timer = {};
168168
timer.it_value = absl::ToTimeval(kPeriod);
169169
timer.it_interval = absl::ToTimeval(kPeriod);
170-
auto cleanup_itimer = std::move(ScopedItimer(id, timer).ValueOrDie());
170+
auto cleanup_itimer = ScopedItimer(id, timer).ValueOrDie();
171171

172172
// Unblock th1.
173173
//

test/util/posix_error.h

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,10 @@ class ABSL_MUST_USE_RESULT PosixErrorOr {
103103
bool ok() const;
104104

105105
// Returns a reference to our current value, or CHECK-fails if !this->ok().
106-
const T& ValueOrDie() const;
107-
T& ValueOrDie();
106+
const T& ValueOrDie() const&;
107+
T& ValueOrDie() &;
108+
const T&& ValueOrDie() const&&;
109+
T&& ValueOrDie() &&;
108110

109111
// Ignores any errors. This method does nothing except potentially suppress
110112
// complaints from any tools that are checking that errors are not dropped on
@@ -179,17 +181,29 @@ bool PosixErrorOr<T>::ok() const {
179181
}
180182

181183
template <typename T>
182-
const T& PosixErrorOr<T>::ValueOrDie() const {
184+
const T& PosixErrorOr<T>::ValueOrDie() const& {
183185
TEST_CHECK(absl::holds_alternative<T>(value_));
184186
return absl::get<T>(value_);
185187
}
186188

187189
template <typename T>
188-
T& PosixErrorOr<T>::ValueOrDie() {
190+
T& PosixErrorOr<T>::ValueOrDie() & {
189191
TEST_CHECK(absl::holds_alternative<T>(value_));
190192
return absl::get<T>(value_);
191193
}
192194

195+
template <typename T>
196+
const T&& PosixErrorOr<T>::ValueOrDie() const&& {
197+
TEST_CHECK(absl::holds_alternative<T>(value_));
198+
return std::move(absl::get<T>(value_));
199+
}
200+
201+
template <typename T>
202+
T&& PosixErrorOr<T>::ValueOrDie() && {
203+
TEST_CHECK(absl::holds_alternative<T>(value_));
204+
return std::move(absl::get<T>(value_));
205+
}
206+
193207
extern ::std::ostream& operator<<(::std::ostream& os, const PosixError& e);
194208

195209
template <typename T>
@@ -399,7 +413,7 @@ IsPosixErrorOkAndHolds(InnerMatcher&& inner_matcher) {
399413
if (!posixerroror.ok()) { \
400414
return (posixerroror.error()); \
401415
} \
402-
lhs = std::move(posixerroror.ValueOrDie())
416+
lhs = std::move(posixerroror).ValueOrDie()
403417

404418
#define EXPECT_NO_ERRNO(expression) \
405419
EXPECT_THAT(expression, IsPosixErrorOkMatcher())
@@ -419,7 +433,7 @@ IsPosixErrorOkAndHolds(InnerMatcher&& inner_matcher) {
419433
({ \
420434
auto _expr_result = (expr); \
421435
ASSERT_NO_ERRNO(_expr_result); \
422-
std::move(_expr_result.ValueOrDie()); \
436+
std::move(_expr_result).ValueOrDie(); \
423437
})
424438

425439
} // namespace testing

test/util/proc_util.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ MATCHER_P(ContainsMappings, mappings,
115115
return false;
116116
}
117117

118-
auto maps = std::move(maps_or.ValueOrDie());
118+
auto maps = std::move(maps_or).ValueOrDie();
119119

120120
// Does maps contain all elements in mappings? The comparator ignores
121121
// the major, minor, and inode fields.

0 commit comments

Comments
 (0)