autocxx
autocxx copied to clipboard
Make templated C++ types useful
Split off from #106. At the moment, a templated C++ type gets made into a completely opaque type called AutocxxConcrete0...n. These types don't have constructors, accessors, or methods, so are useless except as an opaque token passed from C++, through Rust, back into C++.
Options are:
- Make
cxxaccept templated types, as mentioned in https://github.com/google/autocxx/issues/106#issuecomment-749274227 and https://github.com/dtolnay/cxx/issues/767 - Alternatively (far less preferably) add constructors, methods and accessors to these types to make them actually useful.
The LLVM C++ API defines a class called ErrorOr<T> whose semantics resemble the Rust Result struct. ErrorOr<T> is used by many functions as return value, and due to this, all those functions cannot be used through autocxx. An example of this is llvm::MemoryBuffer::getFile():
// Inside class MemoryBuffer
static ErrorOr<std::unique_ptr<MemoryBuffer>>
getFile(const Twine &Filename,
bool IsText = false,
bool RequiresNullTerminator = true,
bool IsVolatile = false);
I read the documentation, but I couldn't figure out the recommended way to deal with this situation. @adetaylor, can you advise on how I should fix this issue in order to use these functions?
Yeah, per this bug they're just opaque from Rust.
The workaround is to define new C++ functions like this, to let you unpack the error.
bool IsAnError(ErrorOr<std::unique_ptr<MemoryBuffer>>) {
// ...
}
std::unique_ptr<MemoryBuffer> UnpackSuccessValue(ErrorOr<std::unique_ptr<MemoryBuffer>>) {
// unpack and return the thing
}
Ask autocxx to generate bindings for those. Whenever you call any function which gives you the pesky ErrorOr<std::unique_ptr<T>>, you can pass it into those functions to get out some useful information.
This is not ideal, of course. That's why this bug exists.
It looks like, due to the issue of ErrorOr<T>, the method llvm::MemoryBuffer::getFile() is generated as follows:
#[doc = "autocxx bindings couldn't be generated: This function or method uses a type where one of the template parameters was incomprehensible to bindgen/autocxx - probably because it uses template specialization."]
fn getFile(_uhoh: autocxx::BindingGenerationFailure) {}
How should I convince autocxx to generate that method with an opaque type (e.g., ErrorOrUniquePtrMemoryBuffer) that I could then pass to custom functions like the above IsAnError()?
Oh, I see.
Unfortunately that's currently not possible. But, I just raised #1009 as a future enhancement to make that possible. I'll try to look into it sometime in the next few weeks (no promises at all.)