[orc-rt] Add support for constructing Expected<Error> values. (#161656)

These will be used in upcoming RPC support patches where the outer
Expected value captures any RPC-infrastructure errors, and the inner
Error is returned from the romet call (i.e. the remote handler's return
type is Error).
This commit is contained in:
Lang Hames
2025-10-02 22:36:02 +10:00
committed by GitHub
parent 9583b399d8
commit 99ce206246
2 changed files with 34 additions and 0 deletions

View File

@@ -288,6 +288,10 @@ private:
Error *Err;
};
/// Tag to force construction of an Expected value in the success state. See
/// Expected constructor for details.
struct ForceExpectedSuccessValue {};
template <typename T> class ORC_RT_NODISCARD Expected {
template <class OtherT> friend class Expected;
@@ -310,6 +314,13 @@ public:
new (getErrorStorage()) error_type(Err.takePayload());
}
template <typename OtherT>
Expected(OtherT &&Val, ForceExpectedSuccessValue _,
std::enable_if_t<std::is_convertible_v<OtherT, T>> * = nullptr)
: HasError(false), Unchecked(true) {
new (getStorage()) storage_type(std::forward<OtherT>(Val));
}
/// Create an Expected from a T value.
template <typename OtherT>
Expected(OtherT &&Val,

View File

@@ -386,6 +386,29 @@ TEST(ErrorTest, ExpectedCovariance) {
(void)!!A2;
}
// Test that Expected<Error> works as expected with .
TEST(ErrorTest, ExpectedError) {
{
// Test success-success case.
Expected<Error> E(Error::success(), ForceExpectedSuccessValue());
EXPECT_TRUE(!!E);
cantFail(E.takeError());
auto Err = std::move(*E);
EXPECT_FALSE(!!Err);
}
{
// Test "failure" success case.
Expected<Error> E(make_error<StringError>("foo"),
ForceExpectedSuccessValue());
EXPECT_TRUE(!!E);
cantFail(E.takeError());
auto Err = std::move(*E);
EXPECT_TRUE(!!Err);
EXPECT_EQ(toString(std::move(Err)), "foo");
}
}
// Test that the ExitOnError utility works as expected.
TEST(ErrorTest, CantFailSuccess) {
cantFail(Error::success());