Repository navigation
Adjust result.h error condition behavior #128
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
Changes from 3 commits
ee06de6
ad9971d
2a79301
6da3346
f49c1ca
91f4014
ea0f528
8bbe2d9
0ae8cf3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,8 +16,11 @@ | |
|
|
||
| #pragma once | ||
|
|
||
| // TODO (AEG) This include (cassert) leaked into downstream cpp examples, removing it here breaks builds. | ||
| // Remove it once examples include it themselves | ||
| #include <cassert> | ||
| #include <optional> | ||
| #include <stdexcept> | ||
| #include <type_traits> | ||
| #include <utility> | ||
| #include <variant> | ||
|
|
@@ -34,9 +37,9 @@ namespace livekit { | |
| * - a success value of type `T`, or | ||
| * - an error value of type `E` | ||
| * | ||
| * Accessors are intentionally non-throwing. Calling `value()` on an error | ||
| * result, or `error()` on a success result, is a programmer error and will | ||
| * trip the debug assertion. | ||
| * Accessors validate their preconditions before returning. Calling `value()` | ||
| * on an error result, or `error()` on a success result, throws `std::logic_error`. | ||
| * Avoid this by checking `ok()` / `has_error()` / if (result) before calling `value()` or `error()`. | ||
| */ | ||
| template <typename T, typename E> | ||
| class [[nodiscard]] Result { | ||
|
|
@@ -60,64 +63,83 @@ class [[nodiscard]] Result { | |
| /// Allows `if (result)` style success checks. | ||
| explicit operator bool() const noexcept { return ok(); } | ||
|
|
||
| // TODO (AEG): clang-tidy flagged these accessors because the signatures are | ||
| // marked noexcept, but std::get can throw a std::bad_variant_access exception | ||
| // on std::variant specifically. Investigate if this is actually a concern or | ||
| // if the types are safe within this class (unit test ideal). | ||
|
|
||
| /// Access the success value. Requires `ok() == true`. | ||
| // NOLINTNEXTLINE(bugprone-exception-escape) | ||
| T& value() & noexcept { | ||
| assert(ok()); | ||
| /// Access the success value. | ||
| /// | ||
| /// @throws std::logic_error if `ok() == false`. | ||
| T& value() & { | ||
| if (!ok()) { | ||
| throw std::logic_error("Result::value() called on an error result"); | ||
| } | ||
| return std::get<0>(storage_); | ||
| } | ||
|
|
||
| /// Access the success value. Requires `ok() == true`. | ||
| // NOLINTNEXTLINE(bugprone-exception-escape) | ||
| const T& value() const& noexcept { | ||
| assert(ok()); | ||
| /// Access the success value. | ||
| /// | ||
| /// @throws std::logic_error if `ok() == false`. | ||
| const T& value() const& { | ||
| if (!ok()) { | ||
| throw std::logic_error("Result::value() called on an error result"); | ||
| } | ||
| return std::get<0>(storage_); | ||
| } | ||
|
|
||
| /// Move the success value out. Requires `ok() == true`. | ||
| // NOLINTNEXTLINE(bugprone-exception-escape) | ||
| T&& value() && noexcept { | ||
| assert(ok()); | ||
| /// Move the success value out. | ||
| /// | ||
| /// @throws std::logic_error if `ok() == false`. | ||
| T&& value() && { | ||
| if (!ok()) { | ||
| throw std::logic_error("Result::value() called on an error result"); | ||
| } | ||
| return std::get<0>(std::move(storage_)); | ||
| } | ||
|
|
||
| /// Move the success value out. Requires `ok() == true`. | ||
| // NOLINTNEXTLINE(bugprone-exception-escape) | ||
| const T&& value() const&& noexcept { | ||
| assert(ok()); | ||
| /// Move the success value out. | ||
| /// | ||
| /// @throws std::logic_error if `ok() == false`. | ||
| const T&& value() const&& { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Question: I wonder if we should just remove the const T&& / const E&& overloads since they do not bring much practical benefit
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @stephen-derosa any insight here? I agree not a ton of value for such a small data structure. Gonna assume this was agentic thorough-ness and not intentional
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this was agentic as future proofing -- but i agree i think its over-engineered
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I dug into this a bit and |
||
| if (!ok()) { | ||
| throw std::logic_error("Result::value() called on an error result"); | ||
| } | ||
| return std::get<0>(std::move(storage_)); | ||
| } | ||
|
|
||
| /// Access the error value. Requires `has_error() == true`. | ||
| // NOLINTNEXTLINE(bugprone-exception-escape) | ||
| E& error() & noexcept { | ||
| assert(has_error()); | ||
| /// Access the error value. | ||
| /// | ||
| /// @throws std::logic_error if `has_error() == false`. | ||
| E& error() & { | ||
| if (!has_error()) { | ||
| throw std::logic_error("Result::error() called on a success result"); | ||
| } | ||
| return std::get<1>(storage_); | ||
| } | ||
|
|
||
| /// Access the error value. Requires `has_error() == true`. | ||
| // NOLINTNEXTLINE(bugprone-exception-escape) | ||
| const E& error() const& noexcept { | ||
| assert(has_error()); | ||
| /// Access the error value. | ||
| /// | ||
| /// @throws std::logic_error if `has_error() == false`. | ||
| const E& error() const& { | ||
| if (!has_error()) { | ||
| throw std::logic_error("Result::error() called on a success result"); | ||
| } | ||
| return std::get<1>(storage_); | ||
| } | ||
|
|
||
| /// Move the error value out. Requires `has_error() == true`. | ||
| // NOLINTNEXTLINE(bugprone-exception-escape) | ||
| E&& error() && noexcept { | ||
| assert(has_error()); | ||
| /// Move the error value out. | ||
| /// | ||
| /// @throws std::logic_error if `has_error() == false`. | ||
| E&& error() && { | ||
| if (!has_error()) { | ||
| throw std::logic_error("Result::error() called on a success result"); | ||
| } | ||
| return std::get<1>(std::move(storage_)); | ||
| } | ||
|
|
||
| /// Move the error value out. Requires `has_error() == true`. | ||
| // NOLINTNEXTLINE(bugprone-exception-escape) | ||
| const E&& error() const&& noexcept { | ||
| assert(has_error()); | ||
| /// Move the error value out. | ||
| /// | ||
| /// @throws std::logic_error if `has_error() == false`. | ||
| const E&& error() const&& { | ||
| if (!has_error()) { | ||
| throw std::logic_error("Result::error() called on a success result"); | ||
| } | ||
| return std::get<1>(std::move(storage_)); | ||
| } | ||
|
|
||
|
|
@@ -152,30 +174,53 @@ class [[nodiscard]] Result<void, E> { | |
| /// Allows `if (result)` style success checks. | ||
| explicit operator bool() const noexcept { return ok(); } | ||
|
|
||
| /// Validates success in debug builds. Mirrors the `value()` API shape. | ||
| void value() const noexcept { assert(ok()); } | ||
| /// Validates success. Mirrors the `value()` API shape on the primary | ||
| /// template so generic code can use the same form for both. | ||
| /// | ||
| /// @throws std::logic_error if `ok() == false`. | ||
| void value() const { | ||
| if (!ok()) { | ||
| throw std::logic_error("Result::value() called on an error result"); | ||
| } | ||
| } | ||
|
|
||
| /// Access the error value. Requires `has_error() == true`. | ||
| E& error() & noexcept { | ||
| assert(has_error()); | ||
| /// Access the error value. | ||
| /// | ||
| /// @throws std::logic_error if `has_error() == false`. | ||
| E& error() & { | ||
| if (!error_.has_value()) { | ||
| throw std::logic_error("Result::error() called on a success result"); | ||
| } | ||
| return *error_; | ||
| } | ||
|
|
||
| /// Access the error value. Requires `has_error() == true`. | ||
| const E& error() const& noexcept { | ||
| assert(has_error()); | ||
| /// Access the error value. | ||
| /// | ||
| /// @throws std::logic_error if `has_error() == false`. | ||
| const E& error() const& { | ||
| if (!error_.has_value()) { | ||
| throw std::logic_error("Result::error() called on a success result"); | ||
| } | ||
| return *error_; | ||
| } | ||
|
|
||
| /// Move the error value out. Requires `has_error() == true`. | ||
| E&& error() && noexcept { | ||
| assert(has_error()); | ||
| /// Move the error value out. | ||
| /// | ||
| /// @throws std::logic_error if `has_error() == false`. | ||
| E&& error() && { | ||
| if (!error_.has_value()) { | ||
| throw std::logic_error("Result::error() called on a success result"); | ||
| } | ||
| return std::move(*error_); | ||
| } | ||
|
|
||
| /// Move the error value out. Requires `has_error() == true`. | ||
| const E&& error() const&& noexcept { | ||
| assert(has_error()); | ||
| /// Move the error value out. | ||
| /// | ||
| /// @throws std::logic_error if `has_error() == false`. | ||
| const E&& error() const&& { | ||
| if (!error_.has_value()) { | ||
| throw std::logic_error("Result::error() called on a success result"); | ||
| } | ||
| return std::move(*error_); | ||
| } | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.