diff --git a/include/etl/expected.h b/include/etl/expected.h index 90a94cb9d..168155af9 100644 --- a/include/etl/expected.h +++ b/include/etl/expected.h @@ -605,6 +605,17 @@ namespace etl return etl::move(etl::get(storage)); } + + //******************************************* + /// Swap with another etl::expected. + //******************************************* + void swap(this_type& other) + { + using ETL_OR_STD::swap; + + swap(storage, other.storage); + } + //******************************************* /// //******************************************* @@ -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 @@ -1063,6 +1084,24 @@ bool operator !=(const etl::unexpected& lhs, const etl::unexpected +ETL_CONSTEXPR14 +void swap(etl::expected& lhs, etl::expected& rhs) +{ + lhs.swap(rhs); +} + +//******************************************* +template +ETL_CONSTEXPR14 +void swap(etl::expected& lhs, etl::expected& rhs) +{ + lhs.swap(rhs); +} + //******************************************* /// Swap etl::unexpected. //******************************************* diff --git a/test/test_expected.cpp b/test/test_expected.cpp index cc3aabbd6..11f2d8b3d 100644 --- a/test/test_expected.cpp +++ b/test/test_expected.cpp @@ -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 test_exp_1 = 1; + etl::expected test_exp_2 = 2; + etl::expected test_unexp_1 = etl::unexpected(1); + etl::expected test_unexp_2 = etl::unexpected(2); + + etl::expected test_exp_1_swap = test_exp_1; + etl::expected 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 test_unexp_1_swap = test_unexp_1; + etl::expected 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 test_exp_swap = test_exp_1; + etl::expected 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 test_exp; + etl::expected test_unexp = etl::unexpected(1); + + etl::expected test_exp_swap = test_exp; + etl::expected 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 test_unexp_1 = etl::unexpected(1); + etl::unexpected test_unexp_2 = etl::unexpected(2); + + etl::unexpected test_unexp_1_swap = test_unexp_1; + etl::unexpected 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); + } }; }