From 44de4d163b32eddad427f007bea7d01021dd05e5 Mon Sep 17 00:00:00 2001 From: Nico Weber Date: Mon, 23 Jan 2023 19:22:31 -0500 Subject: [PATCH] AK: Make HashMap::try_ensure work with a fallible construction callback Co-authored-by: Timothy Flynn --- AK/HashMap.h | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/AK/HashMap.h b/AK/HashMap.h index d4d56968e2..979740ac49 100644 --- a/AK/HashMap.h +++ b/AK/HashMap.h @@ -228,8 +228,13 @@ public: auto it = find(key); if (it != end()) return it->value; - auto result = TRY(try_set(key, initialization_callback())); - VERIFY(result == HashSetResult::InsertedNewEntry); + if constexpr (FallibleFunction) { + auto result = TRY(try_set(key, TRY(initialization_callback()))); + VERIFY(result == HashSetResult::InsertedNewEntry); + } else { + auto result = TRY(try_set(key, initialization_callback())); + VERIFY(result == HashSetResult::InsertedNewEntry); + } return find(key)->value; }