Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions book/src/binding/result.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,16 @@ you'd like for the Rust error to have.
...namespace behavior {
...
template <typename Try, typename Fail>
static void trycatch(Try &&func, Fail &&fail) noexcept try {
static void trycatch(Try &&func, Fail &&fail) noexcept {
#if defined(RUST_CXX_NO_EXCEPTIONS)
func();
} catch (const std::exception &e) {
fail(e.what());
#else
try {
func();
} catch (const std::exception &e) {
fail(e.what());
}
#endif
}
...
...} // namespace behavior
Expand Down
12 changes: 9 additions & 3 deletions gen/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,10 +410,16 @@ pub(super) fn write(out: &mut OutFile) {
" ::std::is_same<decltype(trycatch(::std::declval<Try>(), ::std::declval<Fail>())),",
);
writeln!(out, " missing>::value>::type");
writeln!(out, "trycatch(Try &&func, Fail &&fail) noexcept try {{");
writeln!(out, "trycatch(Try &&func, Fail &&fail) noexcept {{");
writeln!(out, "#if defined(RUST_CXX_NO_EXCEPTIONS)");
writeln!(out, " func();");
writeln!(out, "}} catch (::std::exception const &e) {{");
writeln!(out, " fail(e.what());");
writeln!(out, "#else");
writeln!(out, " try {{");
writeln!(out, " func();");
writeln!(out, " }} catch (::std::exception const &e) {{");
writeln!(out, " fail(e.what());");
writeln!(out, " }}");
writeln!(out, "#endif");
writeln!(out, "}}");
out.end_block(Block::Namespace("behavior"));
}
Expand Down
12 changes: 12 additions & 0 deletions gen/src/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1123,7 +1123,19 @@ fn write_rust_function_shim_impl(
if sig.throws {
out.builtin.rust_error = true;
writeln!(out, " if (error$.ptr) {{");
writeln!(out, "#if defined(RUST_CXX_NO_EXCEPTIONS)");
writeln!(
out,
" const auto msg = static_cast<char const *>(error$.ptr);"
);
writeln!(
out,
" std::fprintf(stderr, \"Error: %s Aborting.\\n\", msg);"
);
writeln!(out, " std::abort();");
writeln!(out, "#else");
writeln!(out, " throw ::rust::impl<::rust::Error>::error(error$);");
writeln!(out, "#endif");
writeln!(out, " }}");
}
if indirect_return {
Expand Down