From 67ebb00bd3025298a5d04e4f60fa0990b0cedcd4 Mon Sep 17 00:00:00 2001 From: apio Date: Wed, 11 Jan 2023 17:06:17 +0100 Subject: [PATCH] Option, Result: Introduce try_move_value(), which is the release_value() equivalent of try_set_value() --- luna/include/luna/Option.h | 8 ++++++++ luna/include/luna/Result.h | 6 ++++++ 2 files changed, 14 insertions(+) diff --git a/luna/include/luna/Option.h b/luna/include/luna/Option.h index db68653b..552e72b2 100644 --- a/luna/include/luna/Option.h +++ b/luna/include/luna/Option.h @@ -101,6 +101,14 @@ template class Option return true; } + bool try_move_value(T& ref) const + { + if (!has_value()) return false; + m_has_value = false; + ref = move(m_storage.fetch_reference()); + return true; + } + ~Option() { if (has_value()) m_storage.destroy(); diff --git a/luna/include/luna/Result.h b/luna/include/luna/Result.h index 1bfb1cf4..66a9185a 100644 --- a/luna/include/luna/Result.h +++ b/luna/include/luna/Result.h @@ -114,6 +114,12 @@ template class Result return m_value.try_set_value(ref); } + bool try_move_value(T& ref) const + { + return m_value.try_move_value(ref); + } + + // FIXME: Please remove this. Result try_set_value_with_specific_error(T& ref, int error) { if (has_error() && m_error != error) return release_error();