Skip to content

Commit

Permalink
[base] Add base::expected
Browse files Browse the repository at this point in the history
This change introduces base::expected, a C++17 backport of C++23's
std::expected. Similarly to other STL backports in Chromium, this
port tries to replace UB with deterministic program termination where
possible, and does not support exceptions.

base::expected<T, E> can be thought of as a variant of the value type
`T` and error type `E`, with an API similar to absl::optional<T>.

Prior Art:
* llvm::Expected: https://llvm.org/doxygen/classllvm_1_1Expected.html
* WTF::Expected:
    https://github.com/WebKit/webkit/blob/main/Source/WTF/wtf/Expected.h
* tl::expected: https://github.com/TartanLlama/expected
* Rust's std::result::Result:
    https://doc.rust-lang.org/stable/std/result/enum.Result.html

References:
* Proposal: https://wg21.link/P0323
* C++ Draft: https://eel.is/c++draft/expected
* C++ Ref: https://en.cppreference.com/w/cpp/header/expected

Change-Id: I1e18de1fbec5e621805c90f1b49324db128a4681
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3453963
Reviewed-by: Giovanni Ortuno Urquidi <ortuno@chromium.org>
Reviewed-by: danakj <danakj@chromium.org>
Commit-Queue: Jan Wilken Dörrie <jdoerrie@chromium.org>
Reviewed-by: Daniel Cheng <dcheng@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1002594}
  • Loading branch information
jdoerrie authored and Chromium LUCI CQ committed May 12, 2022
1 parent c73aa3e commit 7251bf9
Show file tree
Hide file tree
Showing 5 changed files with 1,642 additions and 10 deletions.
3 changes: 3 additions & 0 deletions base/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -918,6 +918,8 @@ mixed_component("base") {
"trace_event/trace_id_helper.h",
"traits_bag.h",
"tuple.h",
"types/expected.h",
"types/expected_internal.h",
"types/id_type.h",
"types/pass_key.h",
"types/strong_alias.h",
Expand Down Expand Up @@ -3307,6 +3309,7 @@ test("base_unittests") {
"tools_sanity_unittest.cc",
"traits_bag_unittest.cc",
"tuple_unittest.cc",
"types/expected_unittest.cc",
"types/id_type_unittest.cc",
"types/pass_key_unittest.cc",
"types/strong_alias_unittest.cc",
Expand Down

0 comments on commit 7251bf9

Please sign in to comment.