diff --git a/CMakeLists.txt b/CMakeLists.txt index a06624c51..86a9f6035 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -208,6 +208,7 @@ set (subset_project_sources ${PROJECT_SOURCE_DIR}/src/hb-subset.cc ${PROJECT_SOURCE_DIR}/src/hb-subset.hh ${PROJECT_SOURCE_DIR}/src/hb-repacker.hh + ${PROJECT_SOURCE_DIR}/src/hb-result.hh ${PROJECT_SOURCE_DIR}/src/graph/graph.hh ${PROJECT_SOURCE_DIR}/src/graph/gsubgpos-graph.hh ${PROJECT_SOURCE_DIR}/src/graph/gsubgpos-context.hh @@ -637,7 +638,7 @@ if (HB_BUILD_SUBSET) if (MINGW) target_compile_options(harfbuzz-subset PRIVATE "-Wa,-mbig-obj") endif() - + if (BUILD_SHARED_LIBS) if (BUILD_FRAMEWORK) set_target_properties(harfbuzz harfbuzz-subset PROPERTIES @@ -1254,7 +1255,7 @@ if (NOT SKIP_INSTALL_LIBRARIES AND NOT SKIP_INSTALL_ALL) ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} - FRAMEWORK DESTINATION ${CMAKE_INSTALL_LIBDIR} + FRAMEWORK DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT runtime OPTIONAL ) make_pkgconfig_pc_file("harfbuzz-gobject") diff --git a/src/hb-result.hh b/src/hb-result.hh new file mode 100644 index 000000000..9c85bfc9d --- /dev/null +++ b/src/hb-result.hh @@ -0,0 +1,473 @@ +/* + * Copyright © 2025 Google, Inc. + * + * This is part of HarfBuzz, a text shaping library. + * + * Permission is hereby granted, without written agreement and without + * license or royalty fees, to use, copy, modify, and distribute this + * software and its documentation for any purpose, provided that the + * above copyright notice and the following two paragraphs appear in + * all copies of this software. + * + * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR + * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES + * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN + * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS + * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO + * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. + * + * Google Author(s): Garret Rieger + */ + +#ifndef HB_RESULT_HH +#define HB_RESULT_HH + +#include "hb.hh" + +// Helper structs for Ok(...) and Err(...), not used directly +// by users of hb_result_t. +template +struct hb_result_ok_t +{ + T value; +}; + +template <> +struct hb_result_ok_t {}; + +template +struct hb_result_err_t +{ + E error; + + template + constexpr bool operator == (const hb_result_err_t& other) const { return error == other.error; } + template + constexpr bool operator != (const hb_result_err_t& other) const { return error != other.error; } +}; + +template struct _hb_is_result_tag : hb_false_type {}; +template struct _hb_is_result_tag> : hb_true_type {}; +template struct _hb_is_result_tag> : hb_true_type {}; + +template +static inline constexpr hb_result_ok_t Ok (T&& v) +{ + return hb_result_ok_t{std::forward (v)}; +} + +static inline constexpr hb_result_ok_t Ok () +{ + return hb_result_ok_t{}; +} + +template +static inline constexpr hb_result_err_t Err (E&& e) +{ + return hb_result_err_t{std::forward (e)}; +} + +template +struct hb_result_t +{ + private: + union + { + T val_; + E err_; + }; + bool is_ok_; + + void destroy () + { + if (is_ok_) + val_.~T (); + else + err_.~E (); + } + + void init(const hb_result_t& o) { + if (is_ok_) + new (std::addressof (val_)) T (o.val_); + else + new (std::addressof (err_)) E (o.err_); + } + + void init(hb_result_t&& o) { + if (is_ok_) + new (std::addressof (val_)) T (std::move (o.val_)); + else + new (std::addressof (err_)) E (std::move (o.err_)); + } + + public: + ~hb_result_t () + { + destroy (); + } + + hb_result_t () = delete; + + hb_result_t (const hb_result_t& o) : is_ok_ (o.is_ok_) + { + init (o); + } + + hb_result_t (hb_result_t&& o) noexcept (std::is_nothrow_move_constructible::value && + std::is_nothrow_move_constructible::value) + : is_ok_ (o.is_ok_) + { + init (std::move (o)); + } + + hb_result_t& operator = (const hb_result_t& o) + { + if (this == &o) return *this; + if (is_ok_ && o.is_ok_) + { + val_ = o.val_; + } + else if (!is_ok_ && !o.is_ok_) + { + err_ = o.err_; + } + else + { + destroy (); + is_ok_ = o.is_ok_; + init (o); + } + return *this; + } + + hb_result_t& operator = (hb_result_t&& o) noexcept (std::is_nothrow_move_assignable::value && + std::is_nothrow_move_assignable::value) + { + if (this == &o) return *this; + if (is_ok_ && o.is_ok_) + { + val_ = std::move (o.val_); + } + else if (!is_ok_ && !o.is_ok_) + { + err_ = std::move (o.err_); + } + else + { + destroy (); + is_ok_ = o.is_ok_; + init (std::move (o)); + } + return *this; + } + + // Construct from Ok(...) tag + template ::value))> + hb_result_t (hb_result_ok_t&& o) : is_ok_ (true) + { + new (std::addressof (val_)) T (std::move (o.value)); + } + + template ::value))> + hb_result_t (const hb_result_ok_t& o) : is_ok_ (true) + { + new (std::addressof (val_)) T (o.value); + } + + // Construct from Err(...) tag + template ::value))> + hb_result_t (hb_result_err_t&& e) : is_ok_ (false) + { + new (std::addressof (err_)) E (std::move (e.error)); + } + + template ::value))> + hb_result_t (const hb_result_err_t& e) : is_ok_ (false) + { + new (std::addressof (err_)) E (e.error); + } + + // Construct directly from error E (when T and E are not convertible into each other) + template + hb_result_t (F e) : is_ok_ (false) + { + new (std::addressof (err_)) E (std::move (e)); + } + + // Construct directly from value T (when T and E are not convertible into each other) + template ::value && + !hb_is_same (hb_decay, E) && + !hb_is_same (hb_decay, hb_result_t) && + !_hb_is_result_tag>::value && + !(hb_is_convertible (T, E) && hb_is_convertible (E, T))))> + hb_result_t (U&& v) : is_ok_ (true) + { + new (std::addressof (val_)) T (std::forward (v)); + } + + bool is_ok () const { return is_ok_; } + bool is_err () const { return !is_ok_; } + explicit operator bool () const { return is_ok (); } + + T& value () & { assert (is_ok_); return val_; } + const T& value () const & { assert (is_ok_); return val_; } + T value () && { assert (is_ok_); return std::move (val_); } + + T& operator * () & { return value (); } + const T& operator * () const & { return value (); } + T operator * () && { return std::move (*this).value (); } + + T* operator -> () { return std::addressof (value ()); } + const T* operator -> () const { return std::addressof (value ()); } + + template + T value_or (U&& default_value) const & + { + if (is_ok_) return val_; + return std::forward (default_value); + } + + template + T value_or (U&& default_value) && + { + if (is_ok_) return std::move (val_); + return std::forward (default_value); + } + + const E& error () const & { assert (!is_ok_); return err_; } + E& error () & { assert (!is_ok_); return err_; } + E error () && { assert (!is_ok_); return std::move (err_); } + + bool operator == (const hb_result_t& o) const + { + if (is_ok_ != o.is_ok_) return false; + if (is_ok_) return val_ == o.val_; + return err_ == o.err_; + } + + bool operator != (const hb_result_t& o) const + { + return !(*this == o); + } + + template + bool operator == (const hb_result_ok_t& o) const + { + return is_ok_ && val_ == o.value; + } + + template + bool operator != (const hb_result_ok_t& o) const + { + return !(*this == o); + } + + template + bool operator == (const hb_result_err_t& e) const + { + return !is_ok_ && err_ == e.error; + } + + template + bool operator != (const hb_result_err_t& e) const + { + return !(*this == e); + } + + template + bool operator == (const U&) const = delete; + + template + bool operator != (const U&) const = delete; +}; + +// Partial specialization for void +template +struct hb_result_t +{ + private: + union + { + E err_; + }; + bool is_ok_; + + template friend struct hb_result_t; + + public: + ~hb_result_t () + { + if (!is_ok_) + err_.~E (); + } + + hb_result_t () : is_ok_ (true) {} + hb_result_t (hb_result_ok_t) : is_ok_ (true) {} + + hb_result_t (const hb_result_t& o) : is_ok_ (o.is_ok_) + { + if (!is_ok_) + new (std::addressof (err_)) E (o.err_); + } + + hb_result_t (hb_result_t&& o) noexcept (std::is_nothrow_move_constructible::value) + : is_ok_ (o.is_ok_) + { + if (!is_ok_) + new (std::addressof (err_)) E (std::move (o.err_)); + } + + hb_result_t& operator = (const hb_result_t& o) + { + if (this == &o) return *this; + if (!is_ok_ && !o.is_ok_) + { + err_ = o.err_; + } + else if (!is_ok_ && o.is_ok_) + { + err_.~E (); + is_ok_ = true; + } + else if (is_ok_ && !o.is_ok_) + { + new (std::addressof (err_)) E (o.err_); + is_ok_ = false; + } + return *this; + } + + hb_result_t& operator = (hb_result_t&& o) noexcept (std::is_nothrow_move_assignable::value) + { + if (this == &o) return *this; + if (!is_ok_ && !o.is_ok_) + { + err_ = std::move (o.err_); + } + else if (!is_ok_ && o.is_ok_) + { + err_.~E (); + is_ok_ = true; + } + else if (is_ok_ && !o.is_ok_) + { + new (std::addressof (err_)) E (std::move (o.err_)); + is_ok_ = false; + } + return *this; + } + + template ::value))> + hb_result_t (hb_result_err_t&& e) : is_ok_ (false) + { + new (std::addressof (err_)) E (std::move (e.error)); + } + + template ::value))> + hb_result_t (const hb_result_err_t& e) : is_ok_ (false) + { + new (std::addressof (err_)) E (e.error); + } + + // Construct directly from error E + hb_result_t (E e) : is_ok_ (false) + { + new (std::addressof (err_)) E (std::move(e)); + } + + bool is_ok () const { return is_ok_; } + bool is_err () const { return !is_ok_; } + explicit operator bool () const { return is_ok (); } + + const E& error () const & { assert (!is_ok_); return err_; } + E& error () & { assert (!is_ok_); return err_; } + E error () && { assert (!is_ok_); return std::move (err_); } + + void operator * () & { return; } + void operator * () const & { return; } + void operator * () && { return; } + + bool operator == (const hb_result_t& o) const + { + return is_ok_ == o.is_ok_ && (is_ok_ || err_ == o.err_); + } + + bool operator != (const hb_result_t& o) const + { + return !(*this == o); + } + + bool operator == (hb_result_ok_t) const + { + return is_ok_; + } + + bool operator != (hb_result_ok_t) const + { + return !is_ok_; + } + + template + bool operator == (const hb_result_err_t& e) const + { + return !is_ok_ && err_ == e.error; + } + + template + bool operator != (const hb_result_err_t& e) const + { + return !(*this == e); + } + + template + bool operator == (const U&) const = delete; + + template + bool operator != (const U&) const = delete; +}; + +template +static inline bool operator == (const hb_result_ok_t& o, const hb_result_t& r) +{ + return r == o; +} +template +static inline bool operator != (const hb_result_ok_t& o, const hb_result_t& r) +{ + return r != o; +} +template +static inline bool operator == (const hb_result_err_t& e, const hb_result_t& r) +{ + return r == e; +} +template +static inline bool operator != (const hb_result_err_t& e, const hb_result_t& r) +{ + return r != e; +} + +#ifndef TRY +#define TRY(...) \ + ({ \ + auto res = (__VA_ARGS__); \ + if (!res.is_ok()) return Err(std::move(res).error()); \ + *std::move(res); \ + }) +#endif + +#endif /* HB_RESULT_HH */ diff --git a/src/meson.build b/src/meson.build index aa0fc5a0d..1415d064e 100644 --- a/src/meson.build +++ b/src/meson.build @@ -392,6 +392,7 @@ hb_subset_sources = files( 'hb-number.hh', 'hb-ot-cff1-table.cc', 'hb-ot-cff2-table.cc', + 'hb-result.hh', 'hb-static.cc', 'hb-subset-accelerator.hh', 'hb-subset-cff-common.cc', @@ -991,6 +992,7 @@ if get_option('tests').enabled() 'test-bimap': ['test-bimap.cc', 'hb-static.cc'], 'test-cff': ['test-cff.cc', 'hb-static.cc'], 'test-classdef-graph': ['graph/test-classdef-graph.cc', 'hb-static.cc', 'graph/gsubgpos-context.cc'], + 'test-result': ['test-result.cc', 'hb-static.cc'], 'test-decycler': ['test-decycler.cc', 'hb-static.cc'], 'test-iter': ['test-iter.cc', 'hb-static.cc'], 'test-geometry': ['test-geometry.cc', 'hb-static.cc'], diff --git a/src/test-result.cc b/src/test-result.cc new file mode 100644 index 000000000..d5d41aed3 --- /dev/null +++ b/src/test-result.cc @@ -0,0 +1,519 @@ +/* + * Copyright © 2025 Google, Inc. + * + * This is part of HarfBuzz, a text shaping library. + * + * Permission is hereby granted, without written agreement and without + * license or royalty fees, to use, copy, modify, and distribute this + * software and its documentation for any purpose, provided that the + * above copyright notice and the following two paragraphs appear in + * all copies of this software. + * + * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR + * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES + * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN + * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS + * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO + * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. + * + * Google Author(s): Garret Rieger + */ + +#include "hb-result.hh" + + +// Track constructor and destructor counts to verify leak-freedom +static int live_instances = 0; + +enum error_code_t { + ERR_A, + ERR_B, + ERR_C, +}; + +template +using result = hb_result_t; + +struct resource_t +{ + int id; + + resource_t (int id_) : id (id_) { live_instances++; } + resource_t (const resource_t& o) : id (o.id) { live_instances++; } + resource_t (resource_t&& o) : id (o.id) { o.id = -1; live_instances++; } + ~resource_t () { live_instances--; } + + resource_t& operator = (const resource_t& o) + { + id = o.id; + return *this; + } + resource_t& operator = (resource_t&& o) + { + id = o.id; + o.id = -1; + return *this; + } + + bool operator == (const resource_t& o) const { return id == o.id; } +}; + +static result returns_ok (int v) +{ + return v; +} + +static result returns_err (error_code_t e) +{ + return e; +} + +static result try_callee (bool fail, int v, error_code_t e = ERR_A) +{ + if (fail) return e; + return v; +} + +static result try_callee_void (bool fail, error_code_t e) +{ + if (fail) return e; + return Ok(); +} + +static result try_caller (bool fail1, bool fail2) +{ + int index = TRY (try_callee (fail1, 10, ERR_A)); + int index2 = TRY (try_callee (fail2, 20, ERR_B)); + return index + index2; +} + +static result try_caller_void (bool fail) +{ + TRY (try_callee_void (fail, ERR_C)); + return Ok (); +} + +static result try_caller_type_change (bool fail) +{ + int index = TRY (try_callee (fail, 100, ERR_A)); + return (float) index * 1.5f; +} + +static void test_ok_basic () +{ + result r = returns_ok (42); + hb_always_assert (r.is_ok ()); + hb_always_assert (!r.is_err ()); + hb_always_assert ((bool) r); + hb_always_assert (r.value () == 42); + hb_always_assert (*r == 42); + hb_always_assert (r.value_or (0) == 42); + hb_always_assert (r == Ok (42)); + hb_always_assert (Ok (42) == r); +} + +static void test_err_basic () +{ + result r = returns_err (ERR_A); + hb_always_assert (!r.is_ok ()); + hb_always_assert (r.is_err ()); + hb_always_assert (!r); + hb_always_assert (r.error () == ERR_A); + hb_always_assert (r.value_or (99) == 99); + hb_always_assert (r == Err (ERR_A)); + hb_always_assert (Err (ERR_A) == r); + hb_always_assert (r != Err (ERR_B)); + hb_always_assert (Err (ERR_B) != r); +} + +static void test_explicit_ok_err () +{ + result r1 = Ok (100u); + hb_always_assert (r1.is_ok ()); + hb_always_assert (r1.value () == 100u); + + result r2 = Err (ERR_A); + hb_always_assert (r2.is_err ()); + hb_always_assert (r2.error () == ERR_A); +} + +static void test_pointers () +{ + int x = 123; + result r = &x; + hb_always_assert (r.is_ok ()); + hb_always_assert (*r == &x); + hb_always_assert (**r == 123); + **r = 456; + hb_always_assert (x == 456); +} + +static void test_void () +{ + result r1 = Ok (); + hb_always_assert (r1.is_ok ()); + hb_always_assert (!r1.is_err ()); + hb_always_assert ((bool) r1); + hb_always_assert (r1 == Ok ()); + hb_always_assert (r1 != Err (ERR_A)); + hb_always_assert (Ok() == r1); + hb_always_assert (Err (ERR_A) != r1); + + result r2 = Err (ERR_A); + hb_always_assert (!r2.is_ok ()); + hb_always_assert (r2.is_err ()); + hb_always_assert (!r2); + hb_always_assert (r2.error () == ERR_A); + hb_always_assert (r2 == Err (ERR_A)); + hb_always_assert (r2 != Err (ERR_B)); + hb_always_assert (r2 != Ok ()); + hb_always_assert (Err (ERR_A) == r2); + hb_always_assert (Err (ERR_B) != r2); + hb_always_assert (Ok () != r2); +} + +static void test_resource_cleanup () +{ + hb_always_assert (live_instances == 0); + + { + result r = resource_t (1); + hb_always_assert (live_instances == 1); + hb_always_assert (r.value ().id == 1); + } + hb_always_assert (live_instances == 0); + + { + result r1 = resource_t (2); + hb_always_assert (live_instances == 1); + result r2 = r1; + hb_always_assert (live_instances == 2); + result r3 = std::move (r1); + hb_always_assert (live_instances == 3); + } + hb_always_assert (live_instances == 0); + + { + result r = resource_t (3); + hb_always_assert (live_instances == 1); + r = Err (ERR_A); + hb_always_assert (live_instances == 0); + hb_always_assert (r.is_err ()); + } + hb_always_assert (live_instances == 0); +} + +static void test_try_macro () +{ + // Test success path + result r_success = try_caller (false, false); + hb_always_assert (r_success.is_ok ()); + hb_always_assert (r_success.value () == 30); + + // Test first failure propagates + result r_fail1 = try_caller (true, false); + hb_always_assert (r_fail1.is_err ()); + hb_always_assert (r_fail1.error () == ERR_A); + + // Test second failure propagates + result r_fail2 = try_caller (false, true); + hb_always_assert (r_fail2.is_err ()); + hb_always_assert (r_fail2.error () == ERR_B); + + // Test TRY in void function + result v_ok = try_caller_void (false); + hb_always_assert (v_ok.is_ok ()); + + result v_err = try_caller_void (true); + hb_always_assert (v_err.is_err ()); + hb_always_assert (v_err.error () == ERR_C); + + // Test TRY with different return type + result f_ok = try_caller_type_change (false); + hb_always_assert (f_ok.is_ok ()); + hb_always_assert (f_ok.value () == 150.0f); + + result f_err = try_caller_type_change (true); + hb_always_assert (f_err.is_err ()); + hb_always_assert (f_err.error () == ERR_A); +} + +template +struct is_equality_comparable : std::false_type {}; + +template +struct is_equality_comparable> : std::true_type {}; + +static void test_same_or_convertible_types () +{ + // Comparisons between result and raw types are disallowed + static_assert (!is_equality_comparable, int>::value, ""); + static_assert (!is_equality_comparable, int64_t>::value, ""); + static_assert (!is_equality_comparable>::value, ""); + static_assert (!is_equality_comparable, error_code_t>::value, ""); + static_assert (!is_equality_comparable>::value, ""); + static_assert (!is_equality_comparable, error_code_t>::value, ""); + static_assert (is_equality_comparable, result>::value, ""); + static_assert (is_equality_comparable, hb_result_ok_t>::value, ""); + static_assert (is_equality_comparable, hb_result_err_t>::value, ""); + + // Types convertible into each other cannot be directly constructed without Ok/Err + static_assert (!std::is_constructible, int>::value, ""); + static_assert (!std::is_constructible, int64_t>::value, ""); + static_assert (!std::is_constructible, int32_t>::value, ""); + static_assert (std::is_constructible, hb_result_ok_t>::value, ""); + static_assert (std::is_constructible, hb_result_err_t>::value, ""); + + // Same types cannot be directly constructed without Ok/Err + static_assert (!std::is_constructible, int>::value, ""); + static_assert (std::is_constructible, hb_result_ok_t>::value, ""); + static_assert (std::is_constructible, hb_result_err_t>::value, ""); + + // Types not convertible into each other can be directly constructed + static_assert (std::is_constructible, int>::value, ""); + static_assert (std::is_constructible, error_code_t>::value, ""); + + hb_result_t r_ok = Ok (1); + hb_always_assert (r_ok.is_ok ()); + hb_always_assert (r_ok.value () == 1); + + hb_result_t r_err = Err (2); + hb_always_assert (r_err.is_err ()); + hb_always_assert (r_err.error () == 2); + + hb_result_t same_ok = Ok (42); + hb_always_assert (same_ok.is_ok ()); + hb_always_assert (same_ok.value () == 42); + + hb_result_t same_err = Err (99); + hb_always_assert (same_err.is_err ()); + hb_always_assert (same_err.error () == 99); + + // Check that enum, int type works as expected + { + hb_result_t r = 1; + hb_always_assert (r == Ok (1)); + r = ERR_A; + hb_always_assert (r == Err (ERR_A)); + } + + { + hb_result_t r = 1; + hb_always_assert (r == Err (1)); + r = ERR_A; + hb_always_assert (r == Ok (ERR_A)); + } +} + +struct any_value_t +{ + template + any_value_t (X&&) {} +}; + +static void test_error_interception() { + // Checks that U&& constructor does not intercept mismatched error tags as a success value. + static_assert (!std::is_constructible, hb_result_err_t>::value, ""); + + hb_result_t r = Err (ERR_A); + hb_always_assert (r.is_err ()); + + hb_result_t r_ok = Ok (123); + hb_always_assert (r_ok.is_ok ()); +} + +struct move_only_err_t +{ + int code; + + move_only_err_t (int c) : code (c) {} + move_only_err_t (const move_only_err_t&) = delete; + move_only_err_t (move_only_err_t&& o) : code (o.code) { o.code = -1; } + move_only_err_t& operator = (const move_only_err_t&) = delete; + move_only_err_t& operator = (move_only_err_t&& o) { code = o.code; o.code = -1; return *this; } + ~move_only_err_t () = default; + + bool operator == (const move_only_err_t& o) const { return code == o.code; } +}; + +struct move_only_val_t +{ + int val; + + move_only_val_t (int v) : val (v) {} + move_only_val_t (const move_only_val_t&) = delete; + move_only_val_t (move_only_val_t&& o) : val (o.val) { o.val = -1; } + move_only_val_t& operator = (const move_only_val_t&) = delete; + move_only_val_t& operator = (move_only_val_t&& o) { val = o.val; o.val = -1; return *this; } + ~move_only_val_t () = default; + + bool operator == (const move_only_val_t& o) const { return val == o.val; } +}; + +static void test_move_only_types () +{ + // Test hb_result_t with Err(...) + { + hb_result_t r = Err (move_only_err_t (123)); + hb_always_assert (r.is_err ()); + hb_always_assert (r.error ().code == 123); + + move_only_err_t err = std::move (r).error (); + hb_always_assert (err.code == 123); + } + + // Test direct construction from move_only_err_t + { + hb_result_t r = move_only_err_t (456); + hb_always_assert (r.is_err ()); + hb_always_assert (r.error ().code == 456); + } + + // Test hb_result_t with Err(...) and direct construction + { + hb_result_t v1 = Err (move_only_err_t (789)); + hb_always_assert (v1.is_err ()); + hb_always_assert (v1.error ().code == 789); + + hb_result_t v2 = move_only_err_t (999); + hb_always_assert (v2.is_err ()); + hb_always_assert (v2.error ().code == 999); + } + + // Test value_or with move-only value types + { + hb_result_t r_ok = Ok (move_only_val_t (42)); + move_only_val_t v = std::move (r_ok).value_or (move_only_val_t (0)); + hb_always_assert (v.val == 42); + + hb_result_t r_err = Err (ERR_A); + move_only_val_t v_fallback = std::move (r_err).value_or (move_only_val_t (99)); + hb_always_assert (v_fallback.val == 99); + } +} + +struct copy_move_counter_t +{ + static int copy_count; + static int move_count; + + static void reset () + { + copy_count = 0; + move_count = 0; + } + + int val; + + copy_move_counter_t (int v) : val (v) {} + copy_move_counter_t (const copy_move_counter_t& o) : val (o.val) { copy_count++; } + copy_move_counter_t (copy_move_counter_t&& o) : val (o.val) { o.val = -1; move_count++; } + ~copy_move_counter_t () = default; +}; + +int copy_move_counter_t::copy_count = 0; +int copy_move_counter_t::move_count = 0; + +static result return_tracked_from_local () +{ + copy_move_counter_t out (42); + return out; +} + +static result return_tracked_from_local_with_tag () +{ + copy_move_counter_t out (42); + return Ok(out); +} + +static hb_result_t return_tracked_error_from_local () +{ + copy_move_counter_t out (42); + return out; +} + +static hb_result_t return_tracked_error_from_local_with_tag () +{ + copy_move_counter_t out (42); + return Err(out); +} + +static result return_move_only_from_local () +{ + move_only_val_t out (42); + return out; +} + +static void test_return_moves_from_local () +{ + // Verify 0 copies and only moves occur when returning local value directly + { + copy_move_counter_t::reset (); + result r = return_tracked_from_local (); + hb_always_assert (r.is_ok ()); + hb_always_assert (r.value ().val == 42); + hb_always_assert (copy_move_counter_t::copy_count == 0); + hb_always_assert (copy_move_counter_t::move_count >= 1); + } + + // Verify 0 copies and only moves occur when returning local value directly + { + copy_move_counter_t::reset (); + result r = return_tracked_from_local_with_tag (); + hb_always_assert (r.is_ok ()); + hb_always_assert (r.value ().val == 42); + hb_always_assert (copy_move_counter_t::copy_count == 0); + hb_always_assert (copy_move_counter_t::move_count >= 1); + } + + + // Verify 0 copies and only moves occur when returning local error directly + { + copy_move_counter_t::reset (); + hb_result_t r = return_tracked_error_from_local (); + hb_always_assert (r.is_err ()); + hb_always_assert (r.error().val == 42); + hb_always_assert (copy_move_counter_t::copy_count == 0); + hb_always_assert (copy_move_counter_t::move_count >= 1); + } + + // Verify 0 copies and only moves occur when returning local error directly + { + copy_move_counter_t::reset (); + hb_result_t r = return_tracked_error_from_local_with_tag (); + hb_always_assert (r.is_err ()); + hb_always_assert (r.error().val == 42); + hb_always_assert (copy_move_counter_t::copy_count == 0); + hb_always_assert (copy_move_counter_t::move_count >= 1); + } + + + // Test returning move-only type (statically proves no copy is attempted) + { + result r = return_move_only_from_local (); + hb_always_assert (r.is_ok ()); + hb_always_assert (r.value ().val == 42); + } +} + +int main () +{ + test_ok_basic (); + test_err_basic (); + test_explicit_ok_err (); + test_pointers (); + test_void (); + test_resource_cleanup (); + test_try_macro (); + test_same_or_convertible_types (); + test_error_interception (); + test_move_only_types (); + test_return_moves_from_local (); + + return 0; +}