-
Notifications
You must be signed in to change notification settings - Fork 45
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
Add option to cpp_class! macro for std::marker::PhantomPinned #72
Comments
Thanks for the bug report. I would recommend to do something like:
That is: wrap your string class within a unique_ptr. in C++ rather than with a Box in rust. (
Yeah, I whish this was possible. Maybe in C++23 or later? (https://wg21.link/P1144 / https://wg21.link/P1029 ) |
In my view the greatest "benefit" of I wonder if you've every thought about separating out these features into one macro that makes the object/memory and another macro that implements some traits?
I agree on some level it would be easier, and perhaps even more ergonomic, to expose C++ classes to Rust indirectly through C++ smart pointers (although some of them are not relocatable either) or through custom wrappers written in Rust itself. However, the idea of |
Yes, that's right.
It would add the PhantomPinned, and implement stuff like:
|
I like the idea, but would maybe have the macro do even less. In my initial comment I gave an example of
Unfortunately, due to the lack of support for higher-kinded types in Rust, and the profundity of Rust standard and non-standard heap-allocated containers, there is no easy way to automate generate these wrappers. Rather than create a Swiss Army knife macro that tries to do everything, would it be easier to have a:
That just generates code like this:
And maybe also implements If an idiomatic usage pattern emerges then you could always write a macro that builds on this functionality to do more. It's much harder to write a macro that does less. |
C++ allows the use of self-referential structures by convention; Rust does not. Using cpp_class! to wrap a class that is not trivially relocatable is unsafe because Rust might move the object and break internal references. Unfortunately this precludes the use of cpp_class! for most of the standard library.
Rust now supports the concept of pinning, or preventing an object from being moved/relocated in memory. With the pinning API it is now possible to do:
The only problem is Rust does not know that
struct CppString
is not relocatable and will happily move it out of thePin<>
. We must tell Rust thatCppString
is notUnpin
, which is achieved in the example above using the nightly featureimpl !Unpin for CppString {}
. Unfortunately this means the above code doesn't compile with stable Rust.It is possible to achieve the same effect with stable Rust using the marker type
PhantonPinned
, which implements!Unpin
and thereby makes any structure containing it also!Unpin
. This would conceptually be used to make a structureMyStruct
implement!Unpin
like this. Of note, the presence of phantom types does not increase the size of the structure, affect is alignment, or have any performance drawbacks.It would seem that to do this for C++ classes would require modification of the
cpp_class!
macro. I confess I have yet to learn much macro programming and had some trouble following all themacro_rules!
and downstream parsing functions for thecpp_class!
macro, so I do apologize that this is not a pull request. Some ways this could possibly look:The text was updated successfully, but these errors were encountered: