Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow catch behavior to be customized #77

Merged
merged 2 commits into from
Mar 18, 2020
Merged

Allow catch behavior to be customized #77

merged 2 commits into from
Mar 18, 2020

Conversation

dtolnay
Copy link
Owner

@dtolnay dtolnay commented Mar 18, 2020

As proposed in #53 (comment). If the user's cxx::bridge invocation includes any header that defines the following function, they get it's behavior as the exception⟶Result conversion.

namespace rust::behavior {
template <typename Try, typename Fail>
static void trycatch(Try &&func, Fail &&fail) noexcept try {
  func();
} catch (/* up to you */) {
  fail(/* const char *msg */);
}
}

The default behavior is equivalent to:

} catch (const std::exception &e) {
  fail(e.what());
}

Codebases that use Folly, for example, may be interested in behavior like this instead for better type information on the error messages:

} catch (const std::exception &e) {
  fail(folly::exceptionStr(e));
} catch (...) {
  fail("<unknown exception>");
}

If the user's cxx::bridge invocation includes any header that defines
the following function, they get it's behavior as the exception-to-Result
conversion.

    namespace rust::behavior {
    template <typename Try, typename Fail>
    static void trycatch(Try &&func, Fail &&fail) noexcept try {
      func();
    } catch (/* up to you */) {
      fail(/* const char *msg */);
    }
    }

The default behavior is equivalent to:

    } catch (const std::exception &e) {
      fail(e.what());
    }

Codebases that use Folly, for example, may be interested in behavior
like this instead for better type information on the error messages:

    } catch (const std::exception &e) {
      fail(folly::exceptionStr(e));
    } catch (...) {
      fail("<unknown exception>");
    }
Copy link
Owner Author

@dtolnay dtolnay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apparently doesn't work in x86_64-pc-windows-msvc.

C:\Users\travis\build\dtolnay\cxx\target\debug\build\cxx-test-suite-2db38eca0276da33\out\tests\ffi\lib.rs.cc(190):
error C2904: 'trycatch': name already used for a template in the current scope

@dtolnay dtolnay closed this Mar 18, 2020
@dtolnay dtolnay deleted the catch branch March 18, 2020 06:19
@dtolnay dtolnay restored the catch branch March 18, 2020 18:32
@dtolnay dtolnay reopened this Mar 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant