Add hb_result_t<OkVal, ErrVal>

Error handling in repacker is in rough shape. Many of the methods are fallible, but often the return values aren't being checked for error state.

To help address this I thought it might be useful to introduce a result type similar to Result<> in rust (or std::expected) that would force callers to deal with errors in the returns.

This PR introduces hb_result_t<OkVal, ErrVal>.

Assisted by: Gemini
This commit is contained in:
Garret Rieger committed 2026-09-02 16:30:59 -06:00
1 parent 46644f885d
commit eca9f9f910
4 files changed
+997 -2

No files matched your search

+3 -2
View File
@@ -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")
+473
View File
@@ -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 <typename T>
struct hb_result_ok_t
{
T value;
};
template <>
struct hb_result_ok_t<void> {};
template <typename E>
struct hb_result_err_t
{
E error;
template <typename OtherE>
constexpr bool operator == (const hb_result_err_t<OtherE>& other) const { return error == other.error; }
template <typename OtherE>
constexpr bool operator != (const hb_result_err_t<OtherE>& other) const { return error != other.error; }
};
template <typename X> struct _hb_is_result_tag : hb_false_type {};
template <typename X> struct _hb_is_result_tag<hb_result_ok_t<X>> : hb_true_type {};
template <typename X> struct _hb_is_result_tag<hb_result_err_t<X>> : hb_true_type {};
template <typename T>
static inline constexpr hb_result_ok_t<T&&> Ok (T&& v)
{
return hb_result_ok_t<T&&>{std::forward<T> (v)};
}
static inline constexpr hb_result_ok_t<void> Ok ()
{
return hb_result_ok_t<void>{};
}
template <typename E>
static inline constexpr hb_result_err_t<E&&> Err (E&& e)
{
return hb_result_err_t<E&&>{std::forward<E> (e)};
}
template <typename T, typename E>
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<T>::value &&
std::is_nothrow_move_constructible<E>::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<T>::value &&
std::is_nothrow_move_assignable<E>::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 <typename U,
hb_enable_if ((std::is_constructible<T, U>::value))>
hb_result_t (hb_result_ok_t<U>&& o) : is_ok_ (true)
{
new (std::addressof (val_)) T (std::move (o.value));
}
template <typename U,
hb_enable_if ((std::is_constructible<T, const U&>::value))>
hb_result_t (const hb_result_ok_t<U>& o) : is_ok_ (true)
{
new (std::addressof (val_)) T (o.value);
}
// Construct from Err(...) tag
template <typename F,
hb_enable_if ((std::is_constructible<E, F>::value))>
hb_result_t (hb_result_err_t<F>&& e) : is_ok_ (false)
{
new (std::addressof (err_)) E (std::move (e.error));
}
template <typename F,
hb_enable_if ((std::is_constructible<E, const F&>::value))>
hb_result_t (const hb_result_err_t<F>& 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 <typename F = E,
hb_enable_if ((hb_is_same (F, E) &&
!(hb_is_convertible (T, E) && hb_is_convertible (E, T))))>
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 <typename U = T,
hb_enable_if ((std::is_constructible<T, U>::value &&
!hb_is_same (hb_decay<U>, E) &&
!hb_is_same (hb_decay<U>, hb_result_t) &&
!_hb_is_result_tag<hb_decay<U>>::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<U> (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 <typename U>
T value_or (U&& default_value) const &
{
if (is_ok_) return val_;
return std::forward<U> (default_value);
}
template <typename U>
T value_or (U&& default_value) &&
{
if (is_ok_) return std::move (val_);
return std::forward<U> (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 <typename U>
bool operator == (const hb_result_ok_t<U>& o) const
{
return is_ok_ && val_ == o.value;
}
template <typename U>
bool operator != (const hb_result_ok_t<U>& o) const
{
return !(*this == o);
}
template <typename F>
bool operator == (const hb_result_err_t<F>& e) const
{
return !is_ok_ && err_ == e.error;
}
template <typename F>
bool operator != (const hb_result_err_t<F>& e) const
{
return !(*this == e);
}
template <typename U>
bool operator == (const U&) const = delete;
template <typename U>
bool operator != (const U&) const = delete;
};
// Partial specialization for void
template <typename E>
struct hb_result_t<void, E>
{
private:
union
{
E err_;
};
bool is_ok_;
template <typename, typename> 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<void>) : 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<E>::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<E>::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 <typename F,
hb_enable_if ((std::is_constructible<E, F>::value))>
hb_result_t (hb_result_err_t<F>&& e) : is_ok_ (false)
{
new (std::addressof (err_)) E (std::move (e.error));
}
template <typename F,
hb_enable_if ((std::is_constructible<E, const F&>::value))>
hb_result_t (const hb_result_err_t<F>& 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<void, E>& o) const
{
return is_ok_ == o.is_ok_ && (is_ok_ || err_ == o.err_);
}
bool operator != (const hb_result_t<void, E>& o) const
{
return !(*this == o);
}
bool operator == (hb_result_ok_t<void>) const
{
return is_ok_;
}
bool operator != (hb_result_ok_t<void>) const
{
return !is_ok_;
}
template <typename F>
bool operator == (const hb_result_err_t<F>& e) const
{
return !is_ok_ && err_ == e.error;
}
template <typename F>
bool operator != (const hb_result_err_t<F>& e) const
{
return !(*this == e);
}
template <typename U>
bool operator == (const U&) const = delete;
template <typename U>
bool operator != (const U&) const = delete;
};
template <typename U, typename T, typename E>
static inline bool operator == (const hb_result_ok_t<U>& o, const hb_result_t<T, E>& r)
{
return r == o;
}
template <typename U, typename T, typename E>
static inline bool operator != (const hb_result_ok_t<U>& o, const hb_result_t<T, E>& r)
{
return r != o;
}
template <typename F, typename T, typename E>
static inline bool operator == (const hb_result_err_t<F>& e, const hb_result_t<T, E>& r)
{
return r == e;
}
template <typename F, typename T, typename E>
static inline bool operator != (const hb_result_err_t<F>& e, const hb_result_t<T, E>& 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 */
+2
View File
@@ -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'],
+519
View File
@@ -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 <typename T>
using result = hb_result_t<T, error_code_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<int> returns_ok (int v)
{
return v;
}
static result<int> returns_err (error_code_t e)
{
return e;
}
static result<int> try_callee (bool fail, int v, error_code_t e = ERR_A)
{
if (fail) return e;
return v;
}
static result<void> try_callee_void (bool fail, error_code_t e)
{
if (fail) return e;
return Ok();
}
static result<int> 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<void> try_caller_void (bool fail)
{
TRY (try_callee_void (fail, ERR_C));
return Ok ();
}
static result<float> 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<int> 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<int> 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<unsigned> r1 = Ok (100u);
hb_always_assert (r1.is_ok ());
hb_always_assert (r1.value () == 100u);
result<unsigned> 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<int*> 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<void> 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<void> 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<resource_t> r = resource_t (1);
hb_always_assert (live_instances == 1);
hb_always_assert (r.value ().id == 1);
}
hb_always_assert (live_instances == 0);
{
result<resource_t> r1 = resource_t (2);
hb_always_assert (live_instances == 1);
result<resource_t> r2 = r1;
hb_always_assert (live_instances == 2);
result<resource_t> r3 = std::move (r1);
hb_always_assert (live_instances == 3);
}
hb_always_assert (live_instances == 0);
{
result<resource_t> 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<int> 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<int> 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<int> 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<void> v_ok = try_caller_void (false);
hb_always_assert (v_ok.is_ok ());
result<void> 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<float> f_ok = try_caller_type_change (false);
hb_always_assert (f_ok.is_ok ());
hb_always_assert (f_ok.value () == 150.0f);
result<float> f_err = try_caller_type_change (true);
hb_always_assert (f_err.is_err ());
hb_always_assert (f_err.error () == ERR_A);
}
template <typename T, typename U, typename = void>
struct is_equality_comparable : std::false_type {};
template <typename T, typename U>
struct is_equality_comparable<T, U, hb_void_t<decltype (hb_declval (T) == hb_declval (U))>> : std::true_type {};
static void test_same_or_convertible_types ()
{
// Comparisons between result and raw types are disallowed
static_assert (!is_equality_comparable<result<int>, int>::value, "");
static_assert (!is_equality_comparable<result<int>, int64_t>::value, "");
static_assert (!is_equality_comparable<int, result<int>>::value, "");
static_assert (!is_equality_comparable<result<int>, error_code_t>::value, "");
static_assert (!is_equality_comparable<error_code_t, result<int>>::value, "");
static_assert (!is_equality_comparable<result<void>, error_code_t>::value, "");
static_assert (is_equality_comparable<result<int>, result<int>>::value, "");
static_assert (is_equality_comparable<result<int>, hb_result_ok_t<int>>::value, "");
static_assert (is_equality_comparable<result<int>, hb_result_err_t<error_code_t>>::value, "");
// Types convertible into each other cannot be directly constructed without Ok/Err
static_assert (!std::is_constructible<hb_result_t<int64_t, int32_t>, int>::value, "");
static_assert (!std::is_constructible<hb_result_t<int64_t, int32_t>, int64_t>::value, "");
static_assert (!std::is_constructible<hb_result_t<int64_t, int32_t>, int32_t>::value, "");
static_assert (std::is_constructible<hb_result_t<int64_t, int32_t>, hb_result_ok_t<int>>::value, "");
static_assert (std::is_constructible<hb_result_t<int64_t, int32_t>, hb_result_err_t<int>>::value, "");
// Same types cannot be directly constructed without Ok/Err
static_assert (!std::is_constructible<hb_result_t<int, int>, int>::value, "");
static_assert (std::is_constructible<hb_result_t<int, int>, hb_result_ok_t<int>>::value, "");
static_assert (std::is_constructible<hb_result_t<int, int>, hb_result_err_t<int>>::value, "");
// Types not convertible into each other can be directly constructed
static_assert (std::is_constructible<hb_result_t<int, error_code_t>, int>::value, "");
static_assert (std::is_constructible<hb_result_t<int, error_code_t>, error_code_t>::value, "");
hb_result_t<int64_t, int32_t> r_ok = Ok (1);
hb_always_assert (r_ok.is_ok ());
hb_always_assert (r_ok.value () == 1);
hb_result_t<int64_t, int32_t> r_err = Err (2);
hb_always_assert (r_err.is_err ());
hb_always_assert (r_err.error () == 2);
hb_result_t<int, int> same_ok = Ok (42);
hb_always_assert (same_ok.is_ok ());
hb_always_assert (same_ok.value () == 42);
hb_result_t<int, int> 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<int, error_code_t> r = 1;
hb_always_assert (r == Ok (1));
r = ERR_A;
hb_always_assert (r == Err (ERR_A));
}
{
hb_result_t<error_code_t, int> r = 1;
hb_always_assert (r == Err (1));
r = ERR_A;
hb_always_assert (r == Ok (ERR_A));
}
}
struct any_value_t
{
template <typename X>
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_t<any_value_t, error_code_t>, hb_result_err_t<const char*>>::value, "");
hb_result_t<any_value_t, error_code_t> r = Err (ERR_A);
hb_always_assert (r.is_err ());
hb_result_t<any_value_t, error_code_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<T, move_only_err_t> with Err(...)
{
hb_result_t<int, move_only_err_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<int, move_only_err_t> r = move_only_err_t (456);
hb_always_assert (r.is_err ());
hb_always_assert (r.error ().code == 456);
}
// Test hb_result_t<void, move_only_err_t> with Err(...) and direct construction
{
hb_result_t<void, move_only_err_t> v1 = Err (move_only_err_t (789));
hb_always_assert (v1.is_err ());
hb_always_assert (v1.error ().code == 789);
hb_result_t<void, move_only_err_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<move_only_val_t, error_code_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<move_only_val_t, error_code_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<copy_move_counter_t> return_tracked_from_local ()
{
copy_move_counter_t out (42);
return out;
}
static result<copy_move_counter_t> return_tracked_from_local_with_tag ()
{
copy_move_counter_t out (42);
return Ok(out);
}
static hb_result_t<int, copy_move_counter_t> return_tracked_error_from_local ()
{
copy_move_counter_t out (42);
return out;
}
static hb_result_t<int, copy_move_counter_t> return_tracked_error_from_local_with_tag ()
{
copy_move_counter_t out (42);
return Err(out);
}
static result<move_only_val_t> 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<copy_move_counter_t> 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<copy_move_counter_t> 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<int, copy_move_counter_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<int, copy_move_counter_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<move_only_val_t> 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;
}