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
15 changes: 8 additions & 7 deletions orc-rt/include/orc-rt/SimplePackedSerialization.h
Original file line number Diff line number Diff line change
Expand Up @@ -599,16 +599,12 @@ template <typename SPSTagT> class SPSExpected;
/// See SPSSerializableError for more details.
template <typename T> struct SPSSerializableExpected {
SPSSerializableExpected() = default;
SPSSerializableExpected(Expected<T> E) {
explicit SPSSerializableExpected(Expected<T> E) {
if (E)
Val = decltype(Val)(std::in_place_index<0>, std::move(*E));
else
Val = decltype(Val)(std::in_place_index<1>, toString(E.takeError()));
}
SPSSerializableExpected(Error E) {
assert(E && "Cannot create Expected from Error::success()");
Val = decltype(Val)(std::in_place_index<1>, toString(std::move(E)));
}

Expected<T> toExpected() {
if (Val.index() == 0)
Expand All @@ -621,12 +617,17 @@ template <typename T> struct SPSSerializableExpected {

template <typename T>
SPSSerializableExpected<T> toSPSSerializableExpected(Expected<T> E) {
return std::move(E);
return SPSSerializableExpected<T>(std::move(E));
}

template <typename T>
SPSSerializableExpected<T> toSPSSerializableExpected(T Val) {
return SPSSerializableExpected<T>(std::move(Val));
}

template <typename T>
SPSSerializableExpected<T> toSPSSerializableExpected(Error E) {
return std::move(E);
return SPSSerializableExpected<T>(std::move(E));
}

template <typename SPSTagT, typename T>
Expand Down
22 changes: 21 additions & 1 deletion orc-rt/unittests/SimplePackedSerializationTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ TEST(SimplePackedSerializationTest, SerializeErrorFailure) {
EXPECT_EQ(toString(SE.toError()), std::string("test error message"));
}

TEST(SimplePackedSerializationTest, SerializeExpectedSuccess) {
TEST(SimplePackedSerializationTest, SerializeExpectedSuccessViaExpected) {
auto B = spsSerialize<SPSArgList<SPSExpected<uint32_t>>>(
toSPSSerializableExpected(Expected<uint32_t>(42U)));
if (!B) {
Expand All @@ -243,6 +243,26 @@ TEST(SimplePackedSerializationTest, SerializeExpectedSuccess) {
ADD_FAILURE() << "Unexpected failure value";
}

TEST(SimplePackedSerializationTest, SerializeExpectedSuccessViaValue) {
auto B = spsSerialize<SPSArgList<SPSExpected<uint32_t>>>(
toSPSSerializableExpected(uint32_t(42U)));
if (!B) {
ADD_FAILURE() << "Unexpected failure to serialize expected-success value";
return;
}
SPSSerializableExpected<uint32_t> SE;
if (!spsDeserialize<SPSArgList<SPSExpected<uint32_t>>>(*B, SE)) {
ADD_FAILURE() << "Unexpected failure to deserialize expected-success value";
return;
}

auto E = SE.toExpected();
if (E)
EXPECT_EQ(*E, 42U);
else
ADD_FAILURE() << "Unexpected failure value";
}

TEST(SimplePackedSerializationTest, SerializeExpectedFailure) {
auto B = spsSerialize<SPSArgList<SPSExpected<uint32_t>>>(
toSPSSerializableExpected<uint32_t>(
Expand Down
Loading