about summary refs log tree commit diff
path: root/third_party/nix/src/libutil
diff options
context:
space:
mode:
authorKane York <kanepyork@gmail.com>2020-07-28T02·57-0700
committerkanepyork <rikingcoding@gmail.com>2020-08-01T01·15+0000
commit1cbffe21f3284fbad10b4ca27b0d8381bd554ff3 (patch)
tree921c90af406f009a660bb82eb40324a459bca7b7 /third_party/nix/src/libutil
parent2a292c71f40f3592e33534ee6d63eb2ca5221d5e (diff)
chore(3p/nix/hash): prefer StatusOr over throwing constructor r/1515
The use of `unwrap_throw` can be used as a later grep target.

Change-Id: I8c54ed90c4289f07aecb8a1393dd10204c8bce4e
Reviewed-on: https://cl.tvl.fyi/c/depot/+/1493
Reviewed-by: glittershark <grfn@gws.fyi>
Reviewed-by: tazjin <mail@tazj.in>
Tested-by: BuildkiteCI
Diffstat (limited to 'third_party/nix/src/libutil')
-rw-r--r--third_party/nix/src/libutil/hash.cc18
-rw-r--r--third_party/nix/src/libutil/hash.hh7
2 files changed, 16 insertions, 9 deletions
diff --git a/third_party/nix/src/libutil/hash.cc b/third_party/nix/src/libutil/hash.cc
index 4c6eef0e6d9b..44fc4323b337 100644
--- a/third_party/nix/src/libutil/hash.cc
+++ b/third_party/nix/src/libutil/hash.cc
@@ -177,16 +177,12 @@ std::string Hash::to_string(Base base, bool includeType) const {
   return s;
 }
 
-Hash::Hash(const std::string& s, HashType type) : type(type) {
+Hash::Hash(std::string_view s, HashType type) : type(type) {
   absl::StatusOr<Hash> result = deserialize(s, type);
-  if (result.ok()) {
-    *this = *result;
-  } else {
-    throw BadHash(result.status().message());
-  }
+  *this = unwrap_throw(result);
 }
 
-absl::StatusOr<Hash> Hash::deserialize(const std::string& s, HashType type) {
+absl::StatusOr<Hash> Hash::deserialize(std::string_view s, HashType type) {
   size_t pos = 0;
   bool isSRI = false;
 
@@ -280,6 +276,14 @@ absl::StatusOr<Hash> Hash::deserialize(const std::string& s, HashType type) {
   return dest;
 }
 
+Hash Hash::unwrap_throw(absl::StatusOr<Hash> hash) {
+  if (hash.ok()) {
+    return *hash;
+  } else {
+    throw BadHash(hash.status().message());
+  }
+}
+
 namespace hash {
 
 union Ctx {
diff --git a/third_party/nix/src/libutil/hash.hh b/third_party/nix/src/libutil/hash.hh
index 208615f67bc8..4ad4ef6ada02 100644
--- a/third_party/nix/src/libutil/hash.hh
+++ b/third_party/nix/src/libutil/hash.hh
@@ -36,12 +36,15 @@ struct Hash {
      Subresource Integrity hash expression). If the 'type' argument
      is htUnknown, then the hash type must be specified in the
      string. */
-  Hash(const std::string& s, HashType type = htUnknown);
+  Hash(std::string_view s, HashType type = htUnknown);
 
   /* Status-returning version of above constructor */
-  static absl::StatusOr<Hash> deserialize(const std::string& s,
+  static absl::StatusOr<Hash> deserialize(std::string_view s,
                                           HashType type = htUnknown);
 
+  // Legacy unwrapper for StatusOr. Throws BadHash.
+  static Hash unwrap_throw(absl::StatusOr<Hash> hash) noexcept(false);
+
   void init();
 
   /* Check whether a hash is set. */