Comments (1)
I started putting together a design for the exception-to-Result direction of this.
I'm trying to leave open the possibility of different codebases having different try-catch logic for extracting messages from exceptions, while still providing a reasonable default conversion. I think there will be a way for us to accommodate this with an expression SFINAE that looks like the following:
struct trycatch {
template <typename T> trycatch(T);
static char use_default;
};
template <typename Try, typename Fail,
typename = decltype(trycatch(std::declval<Try>()).use_default)>
static void trycatch(Try &&func, Fail &&fail) noexcept try {
func();
} catch (const std::exception &e) {
fail(e.what());
}
which we invoke in generated code roughly as:
// union with the same layout as std::result::Result<T, cxx::Exception>
rust::Result<T> result;
trycatch(
[&] {
new (&result) rust::Result<T>(the_function(args...));
},
[&](rust::Exception e) noexcept {
new (&result) rust::Result<T>(std::move(e));
});
return result;
In this implementation, CXX provides a default try-catch based on std::exception::what
but the user can replace it by providing any function template with this signature in their header:
template <typename Try, typename Fail>
void trycatch(Try &&func, Fail &&fail) noexcept;
Since our generated code includes the user's header before our default provided trycatch implementation, according to https://timsong-cpp.github.io/cppwp/basic.scope.hiding their trycatch function hides the dummy trycatch struct and the expression SFINAE makes our default provided one not exist.
As an example in Folly's case the codebase might use this which provides more type information:
template <typename Try, typename Fail>
static void trycatch(Try &&func, Fail &&fail) noexcept try {
func();
} catch (const std::exception &e) {
fail(folly::toStdString(folly::exceptionStr(e)));
} catch (...) {
fail("<unknown exception>");
}
from cxx.
Related Issues (20)
- cxx::bridge behind a feature HOT 1
- Proposal: Implement Panic-to-Exception Handling Mode on FFI Boundary (Seeking Feedback)
- Can't figure out proper shim for UniquePtr instead of returning global singleton
- Compilation Issue: Linking to std in no_std Environment
- Compiling cdylib HOT 1
- Is it possible to mark a C++ type as `Trivial`? HOT 2
- Question: estimate workload to call functions from geogram HOT 1
- Returning `CxxString` from Rust to C++ HOT 5
- Extern C++ Enums HOT 5
- Advice/example on docs.rs strategy HOT 2
- Feature Request: Add support for nested Vector types HOT 1
- Building two CXX C++ bindings with the same symbols but in different namespaces. HOT 2
- Including bridge from another crate
- Error in configuring crates_universe when included as a Bazel module
- Error compiling `cxx.cc` on Windows HOT 1
- Proposal: implementing native C++ std::error_code
- Link failure when using CXX-backed function from another crate HOT 1
- Unsupported element type of Vec<[u64; 3]>
- How to include C++ files whose directory is given by an environment variable? HOT 1
- Error compiling demo to wasm on macOS HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cxx.