-
Notifications
You must be signed in to change notification settings - Fork 1.2k
refactor: convert maybe_error into generic Result template class
#5109
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My concern, that class Result always allocate memory for both type Ok and E.
Ok<T> val_;
E err_;
So far as I can see all Ok<> and E<> requires default construct that will be called whatever Result succeed case or not.
Also it always consume memory for both Ok and E even if it is only one needed.
That's fine if it is an implementation for small and rarely used component.
But if the Result is supposed to be general util component it will spread someday all over code base and will effect both performance and memory consumption and it is far from production ready.
I think wrapper over std::variant will work better.
I am worried about this. I think that we could add some static asserts which ensure the types are trivially constructible. |
|
Variant seems to work fine, how does that look? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please, check this branch: develop...knst:dash:refac/result
|
This pull request has conflicts, please rebase. |
3fc0960 to
2120a87
Compare
|
This pull request has conflicts, please rebase. |
## Issue being fixed or feature implemented This refactoring helps to make code more specific and clear. There's using syntax feature from modern C++ such as 'enum class', structure bindings in loops, declaration variables inside if/switch statements, etc. ## What was done? This PR is based on @PastaPastaPasta 's PR #4472 There excluded changes related to using std::optional. Let's decide firstly about `Result` class: #5109 ## How Has This Been Tested? Run unit/functional tests ## Breaking Changes No breaking changes ## Checklist: - [x] I have performed a self-review of my own code - [x] I have assigned this pull request to a milestone --------- Co-authored-by: Pasta <[email protected]>
2120a87 to
111a675
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
utACK
…lementation Since last refactoring bitcoin#15921 and bitcoin#17004 the benefits of using maybe_error are less significants because the interface became simpler. In future it can be considered using universal `maybe_error` (Result) as proposed in PR dashpay#5109 instead this particular implementation
…lementation Since last refactoring bitcoin#15921 and bitcoin#17004 the benefits of using maybe_error are less significants because the interface became simpler. In future it can be considered using universal `maybe_error` (Result) as proposed in PR dashpay#5109 instead this particular implementation
…lementation Since last refactoring bitcoin#15921 and bitcoin#17004 the benefits of using maybe_error are less significants because the interface became simpler. In future it can be considered using universal `maybe_error` (Result) as proposed in PR dashpay#5109 instead this particular implementation
111a675 to
2a9c11b
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
utACK
…lementation Since last refactoring bitcoin#15921 and bitcoin#17004 the benefits of using maybe_error are less significants because the interface became simpler. In future it can be considered using universal `maybe_error` (Result) as proposed in PR dashpay#5109 instead this particular implementation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
…lementation Since last refactoring bitcoin#15921 and bitcoin#17004 the benefits of using maybe_error are less significants because the interface became simpler. In future it can be considered using universal `maybe_error` (Result) as proposed in PR dashpay#5109 instead this particular implementation
…lementation Since last refactoring bitcoin#15921 and bitcoin#17004 the benefits of using maybe_error are less significants because the interface became simpler. In future it can be considered using universal `maybe_error` (Result) as proposed in PR dashpay#5109 instead this particular implementation
|
This pull request has conflicts, please rebase. |
…lementation Since last refactoring bitcoin#15921 and bitcoin#17004 the benefits of using maybe_error are less significants because the interface became simpler. In future it can be considered using universal `maybe_error` (Result) as proposed in PR dashpay#5109 instead this particular implementation
7f6a3fe to
c10833a
Compare
…h bitcoin implementation (#5335) ## Issue being fixed or feature implemented The benefits of using custom struct `maybe_error` is less significant since the interface `TxValidationState` became simpler after refactorings from bitcoin#15921 and bitcoin#17004 The refactoring of `maybe_error` as a class result from PR #5109 is still useful but not for case of TxValidationState. ## What was done? Unified using TxValidationState in dash's related code. ## How Has This Been Tested? Run unit/functional tests ## Breaking Changes No breaking changes, logic are same. ## Checklist: - [x] I have performed a self-review of my own code - [x] I have commented my code, particularly in hard-to-understand areas - [x] I have added or updated relevant unit/integration/functional/e2e tests - [ ] I have made corresponding changes to the documentation - [x] I have assigned this pull request to a milestone
|
This pull request has conflicts, please rebase. |
|
Let's get this one merged! @PastaPastaPasta p.s. The usages from validation.cpp and evo/ has been replaced to BlockValidationState and TxValidationState to follow bitcoin's changes. |
This is a simple implementation of the Result type in C++. The Result type is a type that can either be an Ok value, which represents a successful operation and contains a value, or an Err value, which represents a failed operation and contains an error value.
The Result class is a template class that takes two types: T, which is the type of the value that an Ok result will contain, and E, which is the type of the value that an Err result will contain.
The Ok and Err structs are also templates, and are used to construct Ok and Err values. The Ok struct has an optional single member val_ of type T, and the Err struct has a single member val_ of type E.
The Result class has a constructor that takes either an Ok value or an Err value, and stores the value internally. It also has several member functions that can be used to access the value of the Result and check whether it is an Ok or Err value.
The is_ok() and is_err() member functions can be used to check whether the Result is an Ok or Err value, respectively. The operator bool() function allows the Result to be used in a boolean context, such as an if statement. The unwrap() and err() member functions can be used to get the value contained in the Result, but will assert if the Result is not of the expected type.
For parametrizing maybe_error with Ok without value, should be used template Ok<void>, you can return just {} in this case as success.
Co-Authored-By: Konstantin Akimov <[email protected]>
7803828 to
5c345e1
Compare
|
Reviving this PR, currently with no usage; I would like to point towards an implementation of std::expected (which operated similar to Result) here: https://github.com/TartanLlama/expected please provide conceptual review, especially on if we want to use a Result type class or tl::expected (https://en.cppreference.com/w/cpp/utility/expected) |
Super-seeded by https://github.com/dashpay/dash/blob/develop/src/util/expected.h from #5782 |
Issue being fixed or feature implemented
Removed non-generic maybe_error and replaced with a generic
Resultclass paired withOkandErrstructsWhat was done?
This is a simple implementation of the Result type in C++. The Result type is a type that can either be an Ok value, which represents a successful operation and contains a value, or an Err value, which represents a failed operation and contains an error value.
The Result class is a template class that takes two types: T, which is the type of the value that an Ok result will contain, and E, which is the type of the value that an Err result will contain.
The Ok and Err structs are also templates, and are used to construct Ok and Err values. The Ok struct has a single member val_ of type T, and the Err struct has a single member val_ of type E.
The Result class has a constructor that takes either an Ok value or an Err value, and stores the value internally. It also has several member functions that can be used to access the value of the Result and check whether it is an Ok or Err value.
The is_ok() and is_err() member functions can be used to check whether the Result is an Ok or Err value, respectively. The operator bool() function allows the Result to be used in a boolean context, such as an if statement. The unwrap() and unwrap_err() member functions can be used to get the value contained in the Result, but will assert if the Result is not of the expected type.
How Has This Been Tested?
building, unit tests
Breaking Changes
Should be none
Checklist:
For repository code-owners and collaborators only