Skip to content

Commit

Permalink
Add missing swap implementations for expected (#928)
Browse files Browse the repository at this point in the history
* Fix missing partial specialisation equal operators

* Add missing swap implementations for expected
  • Loading branch information
Chiraffollo authored Jul 20, 2024
1 parent b030054 commit 2d38509
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
39 changes: 39 additions & 0 deletions include/etl/expected.h
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,17 @@ namespace etl
return etl::move(etl::get<Error_Type>(storage));
}


//*******************************************
/// Swap with another etl::expected.
//*******************************************
void swap(this_type& other)
{
using ETL_OR_STD::swap;

swap(storage, other.storage);
}

//*******************************************
///
//*******************************************
Expand Down Expand Up @@ -925,6 +936,16 @@ namespace etl
}
#endif

//*******************************************
/// Swap with another etl::expected.
//*******************************************
void swap(this_type& other)
{
using ETL_OR_STD::swap;

swap(storage, other.storage);
}

private:

enum
Expand Down Expand Up @@ -1063,6 +1084,24 @@ bool operator !=(const etl::unexpected<TError>& lhs, const etl::unexpected<TErro
return !(lhs == rhs);
}

//*******************************************
/// Swap etl::expected.
//*******************************************
template <typename TValue, typename TError>
ETL_CONSTEXPR14
void swap(etl::expected<TValue, TError>& lhs, etl::expected<TValue, TError>& rhs)
{
lhs.swap(rhs);
}

//*******************************************
template <typename TError>
ETL_CONSTEXPR14
void swap(etl::expected<void, TError>& lhs, etl::expected<void, TError>& rhs)
{
lhs.swap(rhs);
}

//*******************************************
/// Swap etl::unexpected.
//*******************************************
Expand Down
63 changes: 63 additions & 0 deletions test/test_expected.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -696,5 +696,68 @@ namespace
CHECK_FALSE(test_unexp == test_unexp_unequal);
CHECK_TRUE(test_unexp != test_unexp_unequal);
}

//*************************************************************************
TEST(test_expected_swap)
{
etl::expected<int, int> test_exp_1 = 1;
etl::expected<int, int> test_exp_2 = 2;
etl::expected<int, int> test_unexp_1 = etl::unexpected<int>(1);
etl::expected<int, int> test_unexp_2 = etl::unexpected<int>(2);

etl::expected<int, int> test_exp_1_swap = test_exp_1;
etl::expected<int, int> test_exp_2_swap = test_exp_2;

swap(test_exp_1_swap, test_exp_2_swap);

CHECK_TRUE(test_exp_1_swap == test_exp_2);
CHECK_TRUE(test_exp_2_swap == test_exp_1);

etl::expected<int, int> test_unexp_1_swap = test_unexp_1;
etl::expected<int, int> test_unexp_2_swap = test_unexp_2;

swap(test_unexp_1_swap, test_unexp_2_swap);

CHECK_TRUE(test_unexp_1_swap == test_unexp_2);
CHECK_TRUE(test_unexp_2_swap == test_unexp_1);

etl::expected<int, int> test_exp_swap = test_exp_1;
etl::expected<int, int> test_unexp_swap = test_unexp_1;

swap(test_exp_swap, test_unexp_swap);

CHECK_TRUE(test_exp_swap == test_unexp_1);
CHECK_TRUE(test_unexp_swap == test_exp_1);
}

//*************************************************************************
TEST(test_expected_void_swap)
{
etl::expected<void, int> test_exp;
etl::expected<void, int> test_unexp = etl::unexpected<int>(1);

etl::expected<void, int> test_exp_swap = test_exp;
etl::expected<void, int> test_unexp_swap = test_unexp;

swap(test_exp_swap, test_unexp_swap);

CHECK_TRUE(test_exp_swap == test_unexp);
CHECK_TRUE(test_unexp_swap == test_exp);
}

//*************************************************************************
TEST(test_unexpected_swap)
{
etl::unexpected<int> test_unexp_1 = etl::unexpected<int>(1);
etl::unexpected<int> test_unexp_2 = etl::unexpected<int>(2);

etl::unexpected<int> test_unexp_1_swap = test_unexp_1;
etl::unexpected<int> test_unexp_2_swap = test_unexp_2;

swap(test_unexp_1_swap, test_unexp_2_swap);

CHECK_TRUE(test_unexp_1_swap == test_unexp_2);
CHECK_TRUE(test_unexp_2_swap == test_unexp_1);
}
};
}

0 comments on commit 2d38509

Please sign in to comment.