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

Avoid std::move when constructing results; allow diplomat_result to be constructed from trivially copyable lvalues #336

Merged
merged 4 commits into from
Jun 8, 2023

Conversation

Manishearth
Copy link
Contributor

Fixes #335

There's a small snag in that diplomat::Ok(result.ok) starts erroring. We only use that path for primitives, and it seems fine to add a non-moving constructor for trivially copyable types. I added some SFINAE for it; which seems to work.

My hunch was right that this affects cpp2 as well (which already avoids the std::move()), and the diplomat_runtime fix works for both.

@Manishearth Manishearth requested a review from sffc June 7, 2023 07:58
@Manishearth Manishearth changed the title Avoid std::move when constructing results Avoid std::move when constructing results; allow diplomat_result to be constructed from trivially copyable lvalues Jun 7, 2023
@Manishearth
Copy link
Contributor Author

r? either @sffc or @robertbastian , whoever feels more comfortable staring at SFINAE

@@ -56,6 +55,10 @@ template<> struct WriteableTrait<std::string> {
template<class T> struct Ok {
T inner;
Ok(T&& i): inner(std::move(i)) {}
// We don't want to expose an lvalue-capable constructor in general
// however there is no problem doing this for trivially copyable types
template<typename X = T, typename = typename std::enable_if<std::is_trivially_copyable<X>::value>::type>
Copy link
Contributor Author

Choose a reason for hiding this comment

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

the X=T is necessary here because enable_if needs to depend on parameters of the ctor not of the class (unfortunately)

I don't think it's possible to "override" the default value for X but I'm not sure.

Copy link
Contributor

@sffc sffc left a comment

Choose a reason for hiding this comment

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

Changes seem beneficial; the template looks plausible if it works

@Manishearth Manishearth merged commit 19b6e41 into rust-diplomat:main Jun 8, 2023
@Manishearth Manishearth deleted the cpp-result-move branch June 8, 2023 04:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

moving a temporary object prevents copy elision
2 participants