77 Commits

Author SHA1 Message Date
David Tolnay
91d1bb9b7d
Use absolute paths consistently in generated code 2020-04-25 18:02:07 -07:00
David Tolnay
76c13bf9da
Remove unused type_traits import when generating Rust Vec 2020-04-25 18:02:07 -07:00
David Tolnay
7c29546739
Revert some unrelated changes from PR 67 2020-04-25 13:13:43 -07:00
Myron Ahn
eba35cfce7
C++ std::vector<T> and Rust std::vec::Vec<T> support
Add basic std::vector and std::vec::Vec support across FFI boundary.
2020-04-25 12:47:04 -07:00
David Tolnay
05e11cca1f
Preserve span of self var in Receiver 2020-04-20 02:13:56 -07:00
David Tolnay
a73853b521
Fold qualified path into local_name 2020-04-20 01:20:05 -07:00
David Tolnay
41909e69d4
Consistently use C++ reference for receiver arguments 2020-04-20 00:55:15 -07:00
David Tolnay
c3f485c54b
Simplify variable name of receiver in C++ shims
Even without $, `self` can't collide with any of the variable names the
user has given to the other function parameters.
2020-04-20 00:52:05 -07:00
David Tolnay
439cde2129
Unindent a large non-header codepath 2020-04-20 00:46:25 -07:00
David Tolnay
86710616bb
Fix const in methods with shared reference receivers 2020-04-20 00:45:16 -07:00
David Tolnay
e439c772aa
Consistently use "receiver" for method self type 2020-04-20 00:24:58 -07:00
David Tolnay
891061bc7f
Use Symbol for mangled names throughout code generators 2020-04-19 23:35:54 -07:00
David Tolnay
3caa50ac99
Share function link name mangling logic 2020-04-19 21:38:44 -07:00
David Tolnay
9b5cfe1836
Resolve clippy redundant_pattern_matching lint 2020-04-19 21:13:06 -07:00
David Tolnay
4e7123f26f
Resolve clippy single_match lint 2020-04-19 21:13:05 -07:00
David Tolnay
0841930d34
Share one Namespace type between gen and macro 2020-04-19 20:38:28 -07:00
David Tolnay
26804bd83b
Use receiver name that resembles the Rust input more closely 2020-04-19 20:06:51 -07:00
David Tolnay
44395e335b
Match clang-format's style more consistently in generated code 2020-04-19 14:52:55 -07:00
David Tolnay
c1fe0055ca
Touch up PR 121 2020-04-17 16:44:03 -07:00
David Tolnay
f94bef1ba2
Replace itertools dependency 2020-04-17 16:44:03 -07:00
David Tolnay
46a54e7abe
Format with rustfmt 2020-03-11 2020-04-17 16:44:03 -07:00
Joel Galenson
187588eeac Mark default and copy constructors as deleted. 2020-04-17 16:19:54 -07:00
Joel Galenson
968738f127 Optimize the computation of the methods of a struct 2020-04-16 16:25:04 -07:00
Joel Galenson
c1c4e7ac6b Support calling Rust methods from C++
These methods can be declared in the bridge by naming the first
argument self and making it a reference to the containing class, e.g.,
  fn get(self: &R) -> usize;
  fn set(self: &mut R, n: usize);
This syntax requires Rust 1.43.
2020-04-16 16:25:04 -07:00
Joel Galenson
3d4f612b34 Support calling C++ methods from Rust
These methods can be declared in the bridge by naming the first
argument self and making it a reference to the containing class, e.g.,
  fn get(self: &C) -> usize;
  fn set(self: &mut C, n: usize);
This syntax requires Rust 1.43.

Note that the implementation also changes the internal naming of shim
functions so that they also contain the name of the owning class, if
any.  This allows defining multiple methods with the same name on
different objects.
2020-04-16 16:24:48 -07:00
David Tolnay
4770b4783b
Include the right headers when using a slice 2020-04-14 16:42:06 -07:00
David Tolnay
eb952bac14
Format with rustfmt and clang-format 2020-04-14 16:42:06 -07:00
Adrian Taylor
f5dd552036 Adding &[u8] support.
This change adds specifically, support for &[u8] with a corresponding
rust::Slice<uint8_t> type. No other types of slice are permitted. The
rationale is that it may be common to pass binary data back and forth
across the FFI boundary, so it's more urgent to get this in place sooner.
Broader support for other slices can wait for the future.

But, both C++ and Rust-side bindings should allow the existing support
to be broadened to other Slice types in future without code changes.

A few specific notes:

* The name "rust::Slice" might be better as "rust::SliceRef" but I'm
  following the precedent of "rust::Str".
* It would be good to add constructors from std::span but as that's
  a C++20 feature, that may have to wait until C++ feature detection
  is resolved.
* Internally, this follows the pattern of &str, where the parser will
  initially recognize this as Type::Ref (of Type::Slice) but then
  will replace that with Type::SliceRefU8. Type::Slice should not
  persist through later stages. As we later come to support other
  types of slice, we would probably want to remove Type::SliceRefU8.
2020-04-14 14:13:48 -07:00
David Tolnay
8e0866125c
Verify that header sections are found even if not used
This prevents typos in the endif part of the section even when test
coverage is not perfect.
2020-04-10 12:20:49 -07:00
David Tolnay
59b5ba1f14
Include BaseTsd.h to get SSIZE_T 2020-04-10 11:36:08 -07:00
David Tolnay
b8a6fb2755
Define rust::isize with Windows support 2020-04-10 11:17:28 -07:00
David Tolnay
5383891622
Do not emit UniquePtr::new for opaque C types 2020-04-09 20:56:44 -07:00
David Tolnay
737e02eeb1
Remove unneeded return from void functions 2020-04-04 21:52:46 -07:00
David Tolnay
75dca2e846
Passing function pointer from Rust to C++ 2020-03-29 21:50:25 -07:00
David Tolnay
d815de0195
Levarage the Display impl for namespace printing 2020-03-29 21:31:26 -07:00
David Tolnay
754e21c678
Add a Namespace type in gen 2020-03-29 21:31:26 -07:00
David Tolnay
d140274c10
Emit bitcopy forward declaration needed by String constructor 2020-03-25 22:21:42 -07:00
David Tolnay
9964262630
Implement and test some more fallible return types 2020-03-25 16:44:11 -07:00
David Tolnay
30430f13c9
Include <cstddef> for size_t 2020-03-19 20:49:34 -07:00
David Tolnay
33d3029780
Add flag to inject additional #include lines 2020-03-18 18:18:36 -07:00
David Tolnay
417305a41a
Add function pointer types to syntax tree 2020-03-18 17:03:26 -07:00
David Tolnay
e68634c0a1
Fix missing newline after cxxbridge02$exception declaration 2020-03-18 12:03:40 -07:00
David Tolnay
047223384e
Make trycatch sfinae work on msvc 2020-03-18 11:35:39 -07:00
David Tolnay
3e3e0af976
Allow catch behavior to be customized
If the user's cxx::bridge invocation includes any header that defines
the following function, they get it's behavior as the exception-to-Result
conversion.

    namespace rust::behavior {
    template <typename Try, typename Fail>
    static void trycatch(Try &&func, Fail &&fail) noexcept try {
      func();
    } catch (/* up to you */) {
      fail(/* const char *msg */);
    }
    }

The default behavior is equivalent to:

    } catch (const std::exception &e) {
      fail(e.what());
    }

Codebases that use Folly, for example, may be interested in behavior
like this instead for better type information on the error messages:

    } catch (const std::exception &e) {
      fail(folly::exceptionStr(e));
    } catch (...) {
      fail("<unknown exception>");
    }
2020-03-17 23:03:24 -07:00
David Tolnay
82c1617925
Catch block isn't allowed to throw 2020-03-17 22:54:12 -07:00
David Tolnay
5d12144989
Factor catch implementation to static function 2020-03-17 22:22:24 -07:00
David Tolnay
4791f1c115
Include <utility> when using std::move 2020-03-17 21:55:19 -07:00
David Tolnay
b7a7cb6785
Provide more struct definitions where needed 2020-03-17 21:40:47 -07:00
David Tolnay
ebef4a23a2
Implement fallible C++ functions 2020-03-17 17:34:52 -07:00
David Tolnay
1e548174c3
Implement fallible Rust functions 2020-03-17 00:15:48 -07:00