cxx
cxx copied to clipboard
Non-exception based Result return value in C++
The docs on returning a Result<T> from C++ seems to talk a bit about bridging with StatusOr<T> though doesn't provide any examples, so curious what the suggested way of doing this. I can imagine doing it by mapping a StatusOr to an exception on failure, then mapping the resulting Err into something more specific on the Rust side, or to return some non-Result type and then construct a Result<T> from that on the Rust side. Is this the best way to accomplish this?
Given we don't have built in support for types like StatusOr, it would be nice to be able to somehow construct my own Result on the C++, but this doesn't seem possible from the docs and not sure how hard this would be to implement.
This will probably become more relevant with C++23 and std::expected.
MS STL and libstdc++ now have support for std::expected, an option to translate Result<T> to that would be very helpful
In the meantime what about a rust::Result
cpp class for RUST_CXX_NO_EXCEPTIONS
? That leaves the handling of rust::Result
to the caller and it could be converted to some other type (like std::expected
) in c++. Using std::expected
directly will be problematic for code that is not on or doesn't allow some features from C++23
(like chromium) Currently it looks like cxx just uses exceptions anyway even for RUST_CXX_NO_EXCEPTIONS
and includes std::exception
without a guard.