-
Notifications
You must be signed in to change notification settings - Fork 338
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
[docs] Improve CxxString example with various usage patterns #1190
Comments
@nyurik Just to set the expectations straight, the #1185 is for now meant for Rust objects to be passed to C++ by value. The other way around is fairly easy as well (it can be done in a very similar manner, even somewhat simpler), BUT the implementor must guarantee that the C++ object passed by-value is not self-referential. Handling self-referential objects by value is naturally not possible. However, before I post the by-value patch (we already have a working solution in our private fork) I wanted to clarify the API (especially, the explicit layout specification). Similarly, I want to clarify the API for Rust type aliases/imports (#1187 with a minimal implementation in #1181), because this is directly related (we need to declare the same layout on the import in another bridge). For these two and also the exception change, I'm waiting for reaction from @dtolnay, so I can address potential maintainer feedback to get those finished and merged before publishing anything more complex. But back to your issue: As already mentioned in the documentation, C++
let_cxx_string!(key = "example");
let_cxx_string!(value = "");
let res = crate::ffi::GetValue2(&key, &mut value); it wouldn't work, since you can't get the mutable reference from a However, there already is explicit support for passing Rust int32_t GetValue(rust::Str key, rust::String &value_out); and then on the Rust side, you could call it via let key: &str = "some_key";
let mut value_out = String::new();
let res = crate::ffi::GetValue(key, &mut value_out); Disclaimer: I didn't try it. Alternatively, if you don't care about the performance (additional allocations/copies), you can construct BTW, you should not pass the non-modifiable key as an |
@schreter thank you for such an amazingly detailed answer!!! I will go into more details in subsequent posts, but one thing to reply to right away -- sadly the C++ part of the code is not under my "full" control -- I can get C++ side to add more functions, but they must use standard C++ things, not |
I'm also experimenting with exposing a Rust library to C++ and just found this interesting discussion. I'd like to use something like a Unfortunately, the Rust side of the cxx bridge consequently reports that As far as I understand this is exactly what @nyurik is trying to do as well and I agree that it would really make sense. Is there a technical reason why using Thank you for the answers and your work on this awesome project. |
@izolyomi Did you try to write the signature without (I didn't experiment with |
Thank you, you're right, it compiled without the Maybe I have missed this detail mentioned reading the documentation, but the parser is not very intuitive with this behavior though. Would it need a significant amount of work to enable parsing these namespace qualifiers? |
I am new to the Rust-C++ FFI, and have been trying to adapt it to one of our projects. I have been going over the CxxString docs, trying to understand the best usage pattern. I think it could be improved with a few more code examples. I would love to contribute to the docs/book, but will need to understand it first. I do think that the Json example gets a little in the way of answering a simple question of "given this existing C++ API that uses
std::string
, this is how to call it", but that could be just me.Given this C++ function, I need to either add some extra C++ code to work around the limitations of no-by-value passing, or some other steps:
By-value limitation
Apparently @schreter is working on this in #1185 (thank you!!! I would love to participate), so might be relevant here.
To work around the by-value limitation, my understanding is that i may need to create a wrapper C++ function. This seems a bit strange that C++ allows me to silently cast by ref to by val, but I haven't worked with it for a while, might be a bit ... "rusty".
Calling C++ function from Rust
This is what I would have expected to use, but it does not even begin to compile, so a bit lost with pinning and other magic.
Thank you!!!
The text was updated successfully, but these errors were encountered: