Commit Graph

642 Commits

Author SHA1 Message Date
David Tolnay
a9a7ed18e6
Remove rust::Vec<T>::operator std::vector<T> 2020-04-25 18:02:08 -07:00
David Tolnay
85db5a01b2
Touch up PR 67 2020-04-25 18:02:08 -07:00
David Tolnay
4fcfa945c6
Wrap implementation of vector type conversion 2020-04-25 18:02:07 -07:00
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
122905eb22
Emit <vector> include if generated code requires it 2020-04-25 18:02:07 -07:00
David Tolnay
8b7f899347
Place vector include in sorted order 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
b6cf314a45
Unify Namespace parsing code 2020-04-19 20:59:20 -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
d71eb54c13
No longer need to iterate Namespace by value 2020-03-29 21:31:26 -07:00
David Tolnay
d815de0195
Levarage the Display impl for namespace printing 2020-03-29 21:31:26 -07:00
David Tolnay
7ece56fb09
Allow write_fmt to work on &OutFile 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
e2e7bc3c9e
Update to codespan-reporting 0.9 2020-03-18 15:17:37 -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
3577d451d7
Fix blank first line if no headers emitted 2020-03-17 21:48:13 -07:00
David Tolnay
b7a7cb6785
Provide more struct definitions where needed 2020-03-17 21:40:47 -07:00
David Tolnay
26ad0bd98c
Look for end of line when finding headers ifdefs 2020-03-17 21:34:16 -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
David Tolnay
277e3ccdba
Factor out C++ indirect return predicate 2020-03-17 00:11:01 -07:00
David Tolnay
2fb14e934b
Add Type::Void variant
Not currently usable as a function argument or explicit return value,
but will be required when we introduce Result for the case of fallible
void functions, whose return type will be Result<()>.
2020-03-15 23:11:40 -07:00
David Tolnay
8c7304998e
Bump inline namespace to match minor version 2020-03-13 01:29:23 -07:00
David Tolnay
3383ae719f
Add f32 and f64 types 2020-03-13 01:14:11 -07:00
David Tolnay
736cbcad40
Move header include path to rust/cxx.h 2020-03-11 16:51:38 -07:00
David Tolnay
4b3a66edf1
Implement returning unique_ptr from Rust to C++ 2020-03-06 16:22:14 -08:00
David Tolnay
5cd8d61f9d
Implement returning Box from Rust to C++ 2020-03-06 16:18:44 -08:00
David Tolnay
09011c304b
Fix alignment of by-value returns from Rust 2020-03-06 14:46:46 -08:00
David Tolnay
f51447e2b9
Implement moving ownership of struct arguments to Rust 2020-03-06 14:22:00 -08:00
David Tolnay
33169bdd59
Safe to assume more about the Box representation
https://doc.rust-lang.org/std/boxed/index.html:
So long as T: Sized, a Box<T> is guaranteed to be represented as a
single pointer and is also ABI-compatible with C pointers (i.e. the C
type T*).
2020-03-06 13:10:58 -08:00
David Tolnay
9c68b1a0c6
Lazily compute include set 2020-03-06 11:27:29 -08:00
David Tolnay
cc3767f0ed
Format with rustfmt 2019-10-07 2020-03-06 10:42:02 -08:00
David Tolnay
a46a237fe7
Make passing String by value to C++ const 2020-03-06 10:28:15 -08:00
David Tolnay
f6292378c7
Add rust::Box member type aliases 2020-03-01 21:09:11 -08:00
David Tolnay
baae443ae2
Change Str to Repr operator to explicit 2020-03-01 20:20:10 -08:00
David Tolnay
9081beb1a0
Remove redundant prefix from mangled Box related symbols 2020-03-01 19:52:09 -08:00
David Tolnay
9ad1fbc478
Pass ending comment to end_block
Clang-format doesn't always like the same ending comment as the start of
the block. In particular it wants:

    inline namespace cxxbridge01 {
      ...
    } // namespace cxxbridge01
2020-03-01 14:05:01 -08:00
David Tolnay
750755e557
Rename namespace to rust::inline cxxbridge01 2020-03-01 13:47:29 -08:00
David Tolnay
324437a263
Rename C++ RustBox to Box 2020-03-01 13:47:29 -08:00
David Tolnay
09dbe75a5d
Rename C++ RustStr to Str 2020-03-01 13:47:28 -08:00
David Tolnay
5608216979
Rename C++ RustString to String 2020-03-01 13:47:28 -08:00
David Tolnay
b92e66f4dc
Support nested blocks in C++ emitter 2020-03-01 13:37:15 -08:00
David Tolnay
7e219b8099
Use absolute qualified identifiers in generated C++
This avoids collision with the user's namespaces having the same name.

Less important in the context of the current names right now, but more
important after we move our public C++ API from cxxbridge:: to rust::.
In fact our example code already uses org::rust:: as the namespace,
inside of which a non-absolute rust:: would cause trouble.
2020-03-01 13:14:55 -08:00
David Tolnay
2a1eaac049
Accept non-\n line endings
On Windows, include_str brings in cxxbridge.h with \r\n line endings.
2020-02-24 02:02:58 -08:00
David Tolnay
7eb9c6b2a0
Touch up cxxbridge.h emit PR 2020-01-27 22:35:59 -08:00
RS
0a2d117fba Emit cxxbridge (#27)
* Working cxxbridge output.

* Add a way to emit cxxbridge.h
2020-01-27 21:47:37 -08:00
David Tolnay
4a44122b0f
Resolve Wreturn-type-c-linkage warnings 2020-01-25 16:26:54 -08:00
David Tolnay
8861bee797
Support opaque types that are not structs 2020-01-20 18:40:16 -08:00
David Tolnay
17955e2e8c
Implement special case types in extern Rust argument position 2020-01-20 18:09:41 -08:00
David Tolnay
e43b737f2c
Release 0.1.0 2020-01-08 08:57:24 -08:00
David Tolnay
7db7369797
Safe FFI between Rust and C++ 2020-01-08 08:57:24 -08:00