Skip to content

C++ implementation of Rust's result type

License

Notifications You must be signed in to change notification settings

peelonet/peelo-result

Repository files navigation

peelo-result

Build

Header only C++11 implementation of Rust's Result type.

Doxygen generated API documentation.

Usage

result class has two static methods that construct result instances: ok and error. First one creates an "OK" result, which holds some kind of value, while the second one creates erroneous result which holds some kind of error.

Whether an result contains an value or not can be checked with has_value method of result class. If this method returns true, then it's value can be accessed with the value method, otherwise it's error can be accessed with the error method.

Note: Accessing value or erronous result leads to undefined behavior and vice versa.

Usage example

#include <iostream>
#include <string>
#include <peelo/result.hpp>

struct my_error
{
  int code;
  std::string message;
};

int main()
{
  auto ok_result = peelo::result<int, my_error>::ok(15);
  auto err_result = peelo::result<int, my_error>::error({ 404, "Not Found" });

  if (ok_result)
  {
    std::cout << "OK result: " << ok_result.value() << std::endl;
  }

  if (!err_result)
  {
    std::cout << "Error code: " << err_result.error().code << std::endl;
  }

  return 0;
}