Skip to content
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

Add util::ResultPtr class #26022

Draft
wants to merge 9 commits into
base: master
Choose a base branch
from
Draft

Conversation

ryanofsky
Copy link
Contributor

@ryanofsky ryanofsky commented Sep 6, 2022

This is based on #25665. The non-base commits are:


The util::ResultPtr class is a wrapper for util::Result providing syntax sugar to make it less awkward to use with returned pointers. Instead of needing to be deferenced twice with **result or (*result)->member, it only needs to be dereferenced once with *result or result->member. Also when it is used in a boolean context, instead of evaluating to true when there is no error and the pointer is null, it evaluates to false so it is straightforward to determine whether the result points to something.

This PR is only partial a solution to #26004 because while it makes it easier to check for null pointer values, it does not make it impossible to return a null pointer value inside a successful result. It would be possible to enforce that successful results always contain non-null pointers by using a not_null type (#24423) in combination with ResultPtr, though.

@ryanofsky
Copy link
Contributor Author

In draft state because it's a partial solution to #26004 and could be discussed more

@DrahtBot
Copy link
Contributor

DrahtBot commented Sep 6, 2022

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Code Coverage & Benchmarks

For details see: https://corecheck.dev/bitcoin/bitcoin/pulls/26022.

Reviews

See the guideline for information on the review process.
A summary of reviews will appear here.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #31616 (init,log: Unify block index log line by l0rinc)
  • #31483 (kernel: Move kernel-related cache constants to kernel cache by TheCharlatan)
  • #31061 (refactor: Check translatable format strings at compile-time by maflcko)
  • #30965 (kernel: Move block tree db open to block manager by TheCharlatan)
  • #29641 (scripted-diff: Use LogInfo over LogPrintf [WIP, NOMERGE, DRAFT] by maflcko)
  • #19461 (multiprocess: Add bitcoin-gui -ipcconnect option by ryanofsky)
  • #19460 (multiprocess: Add bitcoin-wallet -ipcconnect option by ryanofsky)
  • #10102 (Multiprocess bitcoin by ryanofsky)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

@ryanofsky
Copy link
Contributor Author

Rebased 4b8ee99 -> 123788c (pr/bresult-ptr.1 -> pr/bresult-ptr.2, compare) due to conflict with #26005

@ryanofsky
Copy link
Contributor Author

Rebased a1dddc5 -> 8ecac08 (pr/bresult-ptr.3 -> pr/bresult-ptr.4, compare) on top of #25665.

I also rewrote the commit and PR description. I think this is in a good state to review, despite being based on another PR.

@ryanofsky
Copy link
Contributor Author

ryanofsky commented Nov 4, 2024

Rebased 5f6f91c -> c7e7309 (pr/bresult-ptr.8 -> pr/bresult-ptr.9, compare) on top of updated base PR (no changes or conflicts)
Rebased c7e7309 -> f8254e7 (pr/bresult-ptr.9 -> pr/bresult-ptr.10, compare) on top of updated base PR (no changes or conflicts)
Rebased f8254e7 -> f44cb24 (pr/bresult-ptr.10 -> pr/bresult-ptr.11, compare) on top of updated base PR (no changes or conflicts)

ryanofsky and others added 9 commits December 6, 2024 07:38
Add util::Result support for returning more error information. For better error
handling, adds the ability to return a value on failure, not just a value on
success. This is a key missing feature that made the result class not useful
for functions like LoadChainstate() which produce different errors that need to
be handled differently.

This change also makes some more minor improvements:

- Smaller type size. util::Result<int> is 16 bytes, and util::Result<void> is 8
  bytes. Previously util::Result<int> and util::Result<void> were 72 bytes.

- More documentation and tests.
Previous commit causes the warning to be triggered, but the suboptimal return
from const statement was added earlier in 517e204.

Warning was causing CI failure https://cirrus-ci.com/task/6159914970644480
Add util::Result Update method to make it possible to change the value of an
existing Result object. This will be useful for functions that can return
multiple error and warning messages, and want to be able to change the result
value while keeping existing messages.
Add util::Result support for returning warning messages and multiple errors,
not just a single error string. This provides a way for functions to report
errors and warnings in a standard way, and simplifies interfaces.

The functionality is unit tested here, and put to use in followup PR
bitcoin#25722
Suggested by Martin Leitner-Ankerl <[email protected]>
bitcoin#25722 (comment)

Co-authored-by: Martin Leitner-Ankerl <[email protected]>
The util::ResultPtr class is a wrapper for util::Result providing syntax sugar
to make it less awkward to use with returned pointers. Instead of needing to be
deferenced twice with **result or (*result)->member, it only needs to be
dereferenced once with *result or result->member. Also when it is used in a
boolean context, instead of evaluating to true when there is no error and the
pointer is null, it evaluates to false so it is straightforward to determine
whether the result points to something.

This PR is only partial a solution to bitcoin#26004 because while it makes it easier
to check for null pointer values, it does not make it impossible to return a
null pointer value inside a successful result. It would be possible to enforce
that successful results always contain non-null pointers by using a not_null
type (bitcoin#24423) in combination with
ResultPtr, though.
@DrahtBot
Copy link
Contributor

🐙 This pull request conflicts with the target branch and needs rebase.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants