You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
First of all, thanks for this amazing project. Having a safe bridge between Rust and C++ helps me to keep my anxiety under control :-).
I would like to hear your advice on whether it is possible, and if yes, the best way, to pass a Vec<u8> from Rust onto a C++ function expecting a mutable reference to std::vector<uint8_t>, where it would be then populated with some values based on some computation.
So, the ownership would be kept on Rust's side, but the mutable reference passed to C++ would be resized and used to populate the container.
fnmain(){let value_buf = Vec::new();// make_unique FakeStore, key, etc...let ok = store.get(key,&mut value_buf);// use value_buf}
My attempt to bridge:
mod ffi {unsafeexternC++ {
include("api.h");typeFakeStore;fnget(self:&mutFakeStore,key:&CxxString,value:&must CxxVector<u8>) -> bool;}}
But I couldn't get it to work. First it complained that I had to put the mutable ref into a Pin, but then I cannot Pin a CxxVector as it has a PhantomPinned and there I got stuck. So, I am probably trying some nonsensical approach 😅. Maybe this is not allowed at all, given that in general C++ could store the reference behind the scenes and keep it around even after the owning side (Rust) got dropped...
Is it possible to define a bridge for this API to work? Or would I need to change the C++ side to return an unique_ptr wrapping the vector and then having an UniquePtr<CxxVector> on the Rust side? Or is there another way?
Side-questions:
Would it change if I had an Vec<T> where T is some generic type instead of u8?
Given that I cannot (or can I?) return FakeStore by value, I cannot call its ctor, instead I have to wrap it in an UniquePtr, right? The same by-value reasoning holds for vector<T> that would also need to be returned boxed into a pointer such as unique_ptr, right?
Given that get takes FakeStore (this/self) by mutable reference, what are the implications on the bridge? I couldn't get self: &mut FakeStore to work, where FakeStore was treated as an opaque type type FakeStore in the unsafe extern "C++ section.
Thank you very much for the assistance and please let me know if I should provide more info.
Regards.
The text was updated successfully, but these errors were encountered:
First of all, thanks for this amazing project. Having a safe bridge between Rust and C++ helps me to keep my anxiety under control :-).
I would like to hear your advice on whether it is possible, and if yes, the best way, to pass a
Vec<u8>
from Rust onto a C++ function expecting a mutable reference tostd::vector<uint8_t>
, where it would be then populated with some values based on some computation.So, the ownership would be kept on Rust's side, but the mutable reference passed to C++ would be resized and used to populate the container.
An example looks like the following:
And "expected" Rust side:
My attempt to bridge:
But I couldn't get it to work. First it complained that I had to put the mutable ref into a
Pin
, but then I cannotPin
aCxxVector
as it has aPhantomPinned
and there I got stuck. So, I am probably trying some nonsensical approach 😅. Maybe this is not allowed at all, given that in general C++ could store the reference behind the scenes and keep it around even after the owning side (Rust) got dropped...Is it possible to define a bridge for this API to work? Or would I need to change the C++ side to return an
unique_ptr
wrapping thevector
and then having anUniquePtr<CxxVector>
on the Rust side? Or is there another way?Side-questions:
Vec<T>
where T is some generic type instead ofu8
?FakeStore
by value, I cannot call its ctor, instead I have to wrap it in an UniquePtr, right? The same by-value reasoning holds forvector<T>
that would also need to be returned boxed into a pointer such asunique_ptr
, right?get
takesFakeStore
(this/self) by mutable reference, what are the implications on the bridge? I couldn't getself: &mut FakeStore
to work, whereFakeStore
was treated as an opaque typetype FakeStore
in theunsafe extern "C++
section.Thank you very much for the assistance and please let me know if I should provide more info.
Regards.
The text was updated successfully, but these errors were encountered: