Skip to content

Commit

Permalink
Merge pull request #77 from dtolnay/catch
Browse files Browse the repository at this point in the history
Allow catch behavior to be customized
  • Loading branch information
dtolnay committed Mar 18, 2020
2 parents 559fbb3 + 0472233 commit 57d01e4
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions gen/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,21 +205,31 @@ fn write_include_cxxbridge(out: &mut OutFile, apis: &[Api], types: &Types) {
writeln!(out, "}};");
}

out.end_block("namespace cxxbridge02");

if needs_trycatch {
out.next_section();
out.begin_block("namespace behavior");
out.include.exception = true;
out.include.type_traits = true;
out.include.utility = true;
writeln!(out, "class missing {{}};");
writeln!(out, "missing trycatch(...);");
writeln!(out);
writeln!(out, "template <typename Try, typename Fail>");
writeln!(out, "static typename std::enable_if<");
writeln!(
out,
"static void trycatch(Try &&func, Fail &&fail) noexcept try {{",
" 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, " func();");
writeln!(out, "}} catch (const ::std::exception &e) {{");
writeln!(out, " fail(e.what());");
writeln!(out, "}}");
out.end_block("namespace behavior");
}

out.end_block("namespace cxxbridge02");
out.end_block("namespace rust");
}

Expand Down Expand Up @@ -316,7 +326,7 @@ fn write_cxx_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
write!(out, " ");
if efn.throws {
writeln!(out, "::rust::Str::Repr throw$;");
writeln!(out, " ::rust::trycatch(");
writeln!(out, " ::rust::behavior::trycatch(");
writeln!(out, " [&] {{");
write!(out, " ");
}
Expand Down

0 comments on commit 57d01e4

Please sign in to comment.