-
Notifications
You must be signed in to change notification settings - Fork 342
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
Passing std::string by value when calling Rust -> C++ #250
Comments
The non-straightforward part is whether a The problem is with hypothetical
A Do you know if:
|
Yes. So far as I know that is indeed something that our standard library does (and even if it didn't, I wouldn't want to guarantee that it wouldn't in future). I suppose, however, I was thinking of a (Thinking out loud as you can tell!) |
All the binding types are exactly what exists in the other language, they are not handles. We used to use handles in my work codebase and it was a bad experience. It falls apart when you want to call a method that takes In cxx the equivalent of a handle to a std::string is |
OK. Yes, I know they're not handles right now, but if we assume it's impossible to represent But yes. Your point on lifetimes and experience using them is appreciated, and I was worried it might head in that direction too. I'll do some thinking. It's very desirable for us to be able to call existing C++ APIs from Rust even if they take a (One solution I already discounted is to use
Maybe the hypothetical higher-level code generator always generates a C++ wrapper function which takes a |
That could work! A step further in that direction to make it seamless would be: extern "C" {
fn TakeString(s: CxxString);
}
// becomes callable as:
fn TakeString(s: impl IntoCxxString); where we have impls to make it work for &str, String, &CxxString, UniquePtr<CxxString>, etc. This would sort of imitate the implicit conversions or implicit copy construction you would get in C++ callers while still being able to move in UniquePtr<CxxString> if the caller has one available. TakeString("..."); |
cxx cannot currently cope with:
It says:
What do you think about adding support for this? We need this if we're to call many of our existing C++ APIs without having to write wrappers.
To me the best option would seem to be to implicitly turn a
CxxString
into a&CxxString
when calling from Rust to C++, then construct a newstd::string
from it within the generated .cc code.That seems straightforward (unless I'm missing some complexity, which I might be!). It does introduce a slightly greater level of marshalling/unmarshalling than we currently do. Perhaps you consider this a role for the higher-level code generator discussed in #228 and #239, but I'd probably claim that this is sufficiently useful it would be handy for all cxx consumers?
Further enhancements:
std::string
by value into Rust APIs.std::string
s. This will require marshalling (say)into
then unmarshalling at the other end by reconstructing real
std::string
s. That sounds absolutely horrid, but it's definitely something we're going to need to do if we wish to allow a significant fraction of our existing C++ APIs to be called fluidly. I must admit it's quite hard for me to make the case that this should be in corecxx
rather than a higher-level wrapper.The text was updated successfully, but these errors were encountered: