-
Notifications
You must be signed in to change notification settings - Fork 14
feat: ✨ Result class #63
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
Merged
Merged
Changes from 11 commits
Commits
Show all changes
46 commits
Select commit
Hold shift + click to select a range
66ce386
start work on Result class
SizzinSeal 5878600
remove const qualifier from Result member variables
SizzinSeal e0c4824
move ResultError type member to the end of the struct
SizzinSeal 69bb041
add const qualifier to parse member function of ResultError formatter
SizzinSeal bdecefa
make << operator use std::format for ResultError
SizzinSeal 641d235
add some "flair" to result stacktrace formatting
SizzinSeal 326b332
more consistent template parameter typing in result.hpp
SizzinSeal 2fba9c7
remove Result move constructor, as it's equivalent to the implicit mo…
SizzinSeal 76b7c67
add option for forcing explicit unwrapping of Result
SizzinSeal f8cdc43
fix meson language server ramblings
SizzinSeal ac4609e
use error types in Result class
SizzinSeal ac561fe
mark Result constructor as explicit
SizzinSeal 1f2ac91
improve sentinel trait naming
SizzinSeal 3d2fdd2
clean up Result constraints
SizzinSeal edf7646
improve get member function of Result
SizzinSeal 1033540
specialize Result with void type
SizzinSeal d83b85f
require at least one error type in the Result class
SizzinSeal 15eb38d
fix Result constructor warnings
SizzinSeal 67f6c8a
add comparison operator overload to Result
SizzinSeal 83328b2
Merge branch 'main' into feat/better-errors
SizzinSeal 84f3e7e
Merge branch 'feat/better-errors' of https://github.com/ZestCommunity…
SizzinSeal 99398cf
improve Result comparison operator
SizzinSeal dc99edf
fix SentinelValue specialization for integral types
SizzinSeal 41c3d29
fix Result ctor order warnings
SizzinSeal 7d4eea4
add time point to ResultError
SizzinSeal 3628c58
improve Result error type constraints
SizzinSeal d5a4999
use infinity as a sentinel value if possible
SizzinSeal 479f1c4
fix bad variant access in Result::get
SizzinSeal 953874f
fix SentinelValue floating-point values
SizzinSeal c0408dc
use doxygen documentation in result.hpp
SizzinSeal f397080
use std::remove_cvref instead of std::remove_cvref_t in result.hpp
SizzinSeal 7d1792e
fix ambiguous Result comparison operator
SizzinSeal a930af2
add comparison operator tests for Result
SizzinSeal 7a86847
add Result void specialization test
SizzinSeal ce756f7
remove `constexpr` qualifiers for certain `Result` member functions
SizzinSeal 11d3b0d
support compile-time ResultError
SizzinSeal 5f1ff0b
use proper move semantics for Result conversion operators
SizzinSeal d41d766
remove Result conversion operators returning r-value references
SizzinSeal 255afd4
fix Result conversion operator qualifiers again
SizzinSeal b329187
add const-rvalue reference overload for Result::get
SizzinSeal 29c8fa6
rename Result.cpp to result.cpp
SizzinSeal 36f16db
fix Result::get return types
SizzinSeal 44b326e
fix meson language server tweaking
SizzinSeal 3a07636
fix Result::get return type
SizzinSeal b6a854a
add more Result compile-time tests
SizzinSeal 92bdad1
add second Result test function
SizzinSeal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,146 @@ | ||
| #pragma once | ||
|
|
||
| #include <concepts> | ||
| #include <limits> | ||
| #include <optional> | ||
| #include <stacktrace> | ||
| #include <type_traits> | ||
| #include <variant> | ||
|
|
||
| namespace zest { | ||
|
|
||
| // custom error types inherit from the ResultError class, which enforces that custom error types | ||
| // have some shared functionality. | ||
| class ResultError { | ||
| public: | ||
| // since this constructor has no arguments, it'll be called implicitly by the constructor of any | ||
| // child class. | ||
| ResultError() | ||
| : stacktrace(std::stacktrace::current()) {} | ||
|
|
||
| std::stacktrace stacktrace; | ||
| }; | ||
|
|
||
| // concept used to enforce that error types inherit from the ResultError class | ||
| template<typename T> | ||
| concept CustomError = std::derived_from<T, ResultError>; | ||
|
|
||
| // Some primitive types such as float, double, etc may have a standardized "sentinel" value. | ||
| // For example, a function that returns a float may return INFINITY if an error occurs. | ||
| // Usually the maximum value of the type is returned if there is an error. | ||
| // This trait helps simplify DX. | ||
| // This trait can also be specialized for custom types (e.g unitized types) | ||
| template<typename T> | ||
| class HasSentinel; | ||
SizzinSeal marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // concept which can be used to determine if a type has a sentinel value | ||
| template<typename T> | ||
| concept Sentinel = requires(const T& val) { HasSentinel<T>::value; }; | ||
|
|
||
| // templated variable which can be used to simplify usage of HasSentinel | ||
| template<Sentinel T> | ||
| constexpr T sentinel_v = HasSentinel<T>::value; | ||
|
|
||
| // partial specialization for HasSentinel. | ||
| // any integral type (e.g double, int, uint8, etc) has a sentinel value equal to its maximum value | ||
| template<std::integral T> | ||
| class HasSentinel<T> { | ||
| static constexpr T value = std::numeric_limits<T>::max(); | ||
| }; | ||
|
|
||
SizzinSeal marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // Result class. | ||
| // An alternative to std::expected, where an expected value can be contained alongside an unexpected | ||
| // error value. | ||
| // This means that Result will always contain an expected value. | ||
| // Therefore, if the user does not check if a function returned an error, an exception will NOT be | ||
| // thrown, and therefore the thread won't crash. | ||
| template<typename T, CustomError... Errs> | ||
| class Result { | ||
| public: | ||
| // Construct a Result with a value and no error value. | ||
| // Constraint: the value member variable must be able to be constructed with the value | ||
| // parameter. | ||
| template<typename U> | ||
| requires std::constructible_from<T, U> | ||
| constexpr Result(U&& value) | ||
| : value(std::forward<U>(value)) {} | ||
|
|
||
| // Construct a Result with a value and an error value. | ||
| // Constraint: the value member variable must be able to be constructed with the value | ||
| // parameter. | ||
| // Constraint: the error parameter must be of a type that may be contained in the error member | ||
| // variable. | ||
| template<typename U, CustomError E> | ||
| requires std::constructible_from<T, U> | ||
| && (std::is_same_v<Errs, std::remove_cvref<E>> || ...) | ||
| constexpr Result(U&& value, E&& error) | ||
| : value(std::forward<U>(value)), | ||
| error(std::forward<E>(error)) {} | ||
|
|
||
| // Construct a Result with an error, initializing the normal value to its sentinel value. | ||
| // Constraint: the type of the normal value must have a specialized sentinel value. | ||
| // Constraint: the error parameter must be of a type that may be contained in the error member | ||
| // variable. | ||
| template<CustomError U> | ||
| requires Sentinel<T> && (std::is_same_v<Errs, std::remove_cvref<U>> || ...) | ||
| constexpr Result(U&& error) | ||
SizzinSeal marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| : value(sentinel_v<T>), | ||
| error(std::forward<U>(error)) {} | ||
SizzinSeal marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // Check whether the result contains an error of the given type | ||
| // Constraint: the custom error type to check for must be able to be contained in the error | ||
| // member variable. | ||
| template<CustomError U> | ||
| requires(std::is_same_v<Errs, std::remove_cvref<U>> || ...) | ||
| constexpr U has() const& { | ||
| if (!error.has_value()) { | ||
| return false; | ||
| } else { | ||
| return std::holds_alternative<U>(error); | ||
| } | ||
| }; | ||
|
|
||
| // return an optional wrapping the given error type. | ||
| // Constraint: the custom error type to get must be able to be contained in the error | ||
| // member variable. | ||
| template<CustomError U> | ||
| requires(std::is_same_v<Errs, std::remove_cvref<U>> || ...) | ||
| constexpr std::optional<U> get() const& { | ||
| if (this->has<U>()) { | ||
| return std::get<U>(error.value()); | ||
| } else { | ||
| return std::nullopt; | ||
| } | ||
| } | ||
SizzinSeal marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // return an optional wrapping the given error type. | ||
| // Constraint: the custom error type to get must be able to be contained in the error | ||
| // member variable. | ||
| template<CustomError U> | ||
| requires(std::is_same_v<Errs, std::remove_cvref<U>> || ...) | ||
| constexpr std::optional<U> get() && { | ||
| if (this->has<U>()) { | ||
| return std::get<U>(std::move(error.value())); | ||
| } else { | ||
| return std::nullopt; | ||
| } | ||
| } | ||
|
|
||
| // implicit conversion operator, on an l-value | ||
| constexpr operator T() const& { | ||
| return value; | ||
| }; | ||
|
|
||
| // implicit conversion operator, on an r-value | ||
| constexpr operator T() && { | ||
| return std::move(value); | ||
| } | ||
|
|
||
| // the optional error value. | ||
| // a variant that could contain any of the specified error types. | ||
| std::optional<std::variant<Errs...>> error; | ||
SizzinSeal marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // the normal value | ||
| T value; | ||
| }; | ||
|
|
||
| } // namespace zest | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.