Skip to content

Commit

Permalink
Fix problem with C++ when returning a result containing a &T
Browse files Browse the repository at this point in the history
  • Loading branch information
Walter-Reactor committed Feb 7, 2025
1 parent f7010b3 commit f733576
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 6 deletions.
4 changes: 2 additions & 2 deletions example/cpp/include/diplomat_runtime.hpp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions feature_tests/c/include/ResultOpaque.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions feature_tests/cpp/include/ResultOpaque.d.hpp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions feature_tests/cpp/include/ResultOpaque.hpp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions feature_tests/cpp/include/diplomat_runtime.hpp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions feature_tests/dart/lib/src/ResultOpaque.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions feature_tests/js/api/ResultOpaque.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions feature_tests/js/api/ResultOpaque.mjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions feature_tests/src/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ pub mod ffi {
Err(Box::new(ResultOpaque(i)))
}

/// When we take &str, the return type becomes a Result
/// Test that this interacts gracefully with returning a reference type
pub fn takes_str<'a>(&'a mut self, _v: &str) -> &'a mut Self {
self
}

pub fn assert_integer(&self, i: i32) {
assert_eq!(i, self.0);
}
Expand Down
4 changes: 2 additions & 2 deletions tool/templates/cpp/runtime.hpp.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ inline capi::DiplomatWrite WriteFromString(std::string& string) {

template<class T> struct Ok {
T inner;
Ok(T&& i): inner(std::move(i)) {}
Ok(T&& i): inner(std::forward<T>(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>
Expand All @@ -66,7 +66,7 @@ template<class T> struct Ok {

template<class T> struct Err {
T inner;
Err(T&& i): inner(std::move(i)) {}
Err(T&& i): inner(std::forward<T>(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>
Expand Down

0 comments on commit f733576

Please sign in to comment.