https://github.com/peelonet/peelo-result
C++ implementation of Rust's result type
https://github.com/peelonet/peelo-result
cpp-library header-only result-type
Last synced: about 1 year ago
JSON representation
C++ implementation of Rust's result type
- Host: GitHub
- URL: https://github.com/peelonet/peelo-result
- Owner: peelonet
- License: bsd-2-clause
- Created: 2020-05-22T13:07:30.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2024-11-30T14:14:24.000Z (over 1 year ago)
- Last Synced: 2025-02-13T09:32:11.627Z (over 1 year ago)
- Topics: cpp-library, header-only, result-type
- Language: C++
- Size: 221 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# peelo-result

Header only C++11 implementation of [Rust's Result type].
[Doxygen generated API documentation.][API]
[travis-image]: https://travis-ci.com/peelonet/peelo-result.svg?branch=master
[travis-url]: https://travis-ci.com/peelonet/peelo-result
[Rust's Result type]: https://doc.rust-lang.org/std/result/
[API]: https://peelonet.github.io/peelo-result/index.html
## 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
```C++
#include
#include
#include
struct my_error
{
int code;
std::string message;
};
int main()
{
auto ok_result = peelo::result::ok(15);
auto err_result = peelo::result::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;
}
```