diff options
author | MÃ¥rten Kongstad <marten.kongstad@sony.com> | 2018-11-19 14:14:37 +0100 |
---|---|---|
committer | Todd Kennedy <toddke@google.com> | 2018-12-17 15:45:20 -0800 |
commit | 0f76311c1db25cac791a56ef9a3da66f6cacecbb (patch) | |
tree | 3539550edc6e6c90cf294a5a8d24bb3feab64f84 /cmds/idmap2/libidmap2/ZipFile.cpp | |
parent | b57794af2875e234f253df0196c583560476cf06 (diff) |
idmap2: replace std::pair<bool, T> with Result<T>
Introduce a new type Result<T> to indicate if an operation succeeded or
not, and if it did, to hold the return value of the operation. This is
the same as how std::pair<bool, T> is already used in the codebase, so
replace all instances with Result<T> to improve clarity.
Result<T> is simply an alias for std::optional<T>. The difference is
semantic: use Result<T> as the return value for functions that can fail,
use std::optional<T> when values are truly optional. This is modelled
after Rust's std::result and std::option.
A future change may graduate Result<T> to a proper class which can hold
additional details on why an operation failed, such as a string or an
error code. As a special case, continue to use std::unique_ptr<T>
instead of Result<std::unique_ptr<T>> for now: the latter would increase
code complexity without added benefit.
Test: make idmap2_tests
Change-Id: I2a8355107ed2b6485409e5e655a84cf1e20b9911
Diffstat (limited to 'cmds/idmap2/libidmap2/ZipFile.cpp')
-rw-r--r-- | cmds/idmap2/libidmap2/ZipFile.cpp | 6 |
1 files changed, 3 insertions, 3 deletions
diff --git a/cmds/idmap2/libidmap2/ZipFile.cpp b/cmds/idmap2/libidmap2/ZipFile.cpp index 3f2079a380d6..9fb611dd8e8d 100644 --- a/cmds/idmap2/libidmap2/ZipFile.cpp +++ b/cmds/idmap2/libidmap2/ZipFile.cpp @@ -16,8 +16,8 @@ #include <memory> #include <string> -#include <utility> +#include "idmap2/Result.h" #include "idmap2/ZipFile.h" namespace android { @@ -57,10 +57,10 @@ std::unique_ptr<const MemoryChunk> ZipFile::Uncompress(const std::string& entryP return chunk; } -std::pair<bool, uint32_t> ZipFile::Crc(const std::string& entryPath) const { +Result<uint32_t> ZipFile::Crc(const std::string& entryPath) const { ::ZipEntry entry; int32_t status = ::FindEntry(handle_, ::ZipString(entryPath.c_str()), &entry); - return std::make_pair(status == 0, entry.crc32); + return status == 0 ? Result<uint32_t>(entry.crc32) : kResultError; } } // namespace idmap2 |