cxx icon indicating copy to clipboard operation
cxx copied to clipboard

Non-exception based Result return value in C++

Open snowp opened this issue 2 years ago • 3 comments

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.

snowp avatar May 29 '22 16:05 snowp

This will probably become more relevant with C++23 and std::expected.

Fulgen301 avatar Aug 03 '22 10:08 Fulgen301

MS STL and libstdc++ now have support for std::expected, an option to translate Result<T> to that would be very helpful

razaqq avatar Aug 24 '22 14:08 razaqq

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.

bridiver avatar Jun 07 '23 23:06 bridiver