Code Monkey home page Code Monkey logo

Comments (1)

dtolnay avatar dtolnay commented on July 29, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.