about summary refs log tree commit diff
path: root/third_party/nix/src/libutil/hash.cc
diff options
context:
space:
mode:
Diffstat (limited to 'third_party/nix/src/libutil/hash.cc')
-rw-r--r--third_party/nix/src/libutil/hash.cc18
1 files changed, 11 insertions, 7 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 {