dtolnay / cxx

Safe interop between Rust and C++
https://cxx.rs
Apache License 2.0
5.86k stars 331 forks source link

cxx.h in SWIG #259

Open sayrer opened 4 years ago

sayrer commented 4 years ago

It mostly works. I had to make these small modifications for SWIG 4.0.1. There is a recent 4.0.2 that I haven't tried.

#ifndef SWIG
template <typename T>
template <typename... Fields>
Box<T> Box<T>::in_place(Fields &&... fields) {
  Box box;
  box.uninit();
  ::new (box.ptr) T{std::forward<Fields>(fields)...};
  return box;
}
#endif

and

#ifndef SWIG
  template <typename... Fields>
  static Box in_place(Fields &&...);
#endif
dtolnay commented 4 years ago

Thanks for trying this. I don't know anything about SWIG so I'm not sure what it means to be "in SWIG". Why would this code work not in SWIG but fail in SWIG? Is it a toolchain version issue, or does SWIG contain something that conflicts with that member function? How does it fail without those ifndefs?

sayrer commented 4 years ago

SWIG seems to have its own header parser, and it doesn't yet support variadic templates. The two places where these are used in cxx aren't needed for SWIG-generated code, so they are safe to elide from SWIG's parse with these ifndefs.

SWIG's error is something like header-file.h:105: Error: Syntax error in input(3).

To be clear, these ifndefs only apply as input to SWIG's code generation, not compiling the cxx library itself.

sayrer commented 4 years ago

You can see some examples (still WIP) here:

https://github.com/sayrer/twitter-text/tree/b018b2b935d1b9b21a1ae46af7d774e4ad0a6d0a/rust_bindings

The twitter-text.i file generates bindings for Python, Java, and Ruby

sayrer commented 4 years ago

Here's what's currently required, given changes in cxx.h and the way the command line now produces its output.

https://github.com/sayrer/twitter-text/blob/61e8cac0287aa89130dd8cdaa1ab886109715108/rust_bindings/swig_clean.py

sayrer commented 4 years ago

It looks like most of the issues I'm hitting at least have PRs in the SWIG repo, so hopefully things will get fixed up from that end.

https://github.com/swig/swig/labels/C%2B%2B11

wsfulton commented 8 months ago

With recent improvements in SWIG, SWIG 4.2.1 correctly handles the Box variadic template instead of giving a Syntax error.