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

Move result out of Future instead of copying it #177

Closed
wants to merge 1 commit into from
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
6 changes: 3 additions & 3 deletions support-lib/cpp/Future.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ template <typename T>
struct ValueHolder {
using type = T;
std::optional<T> value;
T getValueUnsafe() const {return *value;}
T&& getValueUnsafe() && {return std::move(*value);}
};
template <>
struct ValueHolder<void> {
using type = bool;
std::optional<bool> value;
void getValueUnsafe() const {}
void getValueUnsafe() && {}
};

template<typename T>
Expand Down Expand Up @@ -295,7 +295,7 @@ class Future {
sharedState->cv.wait(lk, [state = sharedState] {return state->isReady();});
#endif
if (!sharedState->exception) {
return sharedState->getValueUnsafe();
return std::move(*sharedState).getValueUnsafe();
} else {
std::rethrow_exception(sharedState->exception);
}
Expand Down
29 changes: 29 additions & 0 deletions test-suite/handwritten-src/objc/tests/DBCppFutureTests.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#import <XCTest/XCTest.h>

#include <Future.hpp>

@interface DBCppFutureTests : XCTestCase

@end

@implementation DBCppFutureTests

- (void)setUp
{
[super setUp];
}

- (void)tearDown
{
[super tearDown];
}

- (void)testCppFutureMovesResult
{
auto future = djinni::Promise<std::unique_ptr<int>>::resolve(std::make_unique<int>(5));
auto ptr = future.get();
XCTAssertTrue(!!ptr);
XCTAssertEqual(*ptr, 5);
}

@end
Loading