Skip to content

Commit e5792b1

Browse files
committed
refactor: assert for abnormal aggregate
Signed-off-by: Dmitriy Khaustov aka xDimon <[email protected]>
1 parent 07b926e commit e5792b1

File tree

3 files changed

+20
-35
lines changed

3 files changed

+20
-35
lines changed

include/scale/detail/decomposable.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ namespace scale {
6666
template <typename F>
6767
decltype(auto) decompose_and_apply(DecomposableAggregate auto &&v,
6868
const F &f) {
69+
using T = std::remove_cvref_t<decltype(v)>;
70+
static_assert(
71+
std::is_empty_v<T> or field_number_of<T> > 0,
72+
"Unusual decomposition of non empty aggregate; Use macro "
73+
"'SCALE_CUSTOM_DECOMPOSITION' to define custom decomposition way");
74+
6975
return decompose_and_apply<field_number_of<decltype(v)>>(
7076
std::forward<decltype(v)>(v), f);
7177
}

include/scale/detail/decomposable_type_traits.hpp

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -84,16 +84,6 @@ namespace scale::detail::decomposable {
8484
template <typename T>
8585
concept DecomposableArray = not custom::CustomDecomposable<T>
8686
and ArrayWithMaxSize<T, MAX_FIELD_NUM>;
87-
88-
/**
89-
* @brief Decomposes an array and applies a given function.
90-
*/
91-
template <typename F>
92-
decltype(auto) decompose_and_apply(DecomposableArray auto &&v, const F &f) {
93-
return decompose_and_apply<array_size<decltype(v)>>(
94-
std::forward<decltype(v)>(v), f);
95-
}
96-
9787
} // namespace array
9888

9989
namespace structurally_bindable {
@@ -139,18 +129,6 @@ namespace scale::detail::decomposable {
139129
(not custom::CustomDecomposable<T>)
140130
and (not array::DecomposableArray<T>)
141131
and StructurallyBindableWithMaxSize<T>;
142-
143-
/**
144-
* @brief Decomposes a structurally bindable type and applies a given
145-
* function.
146-
*/
147-
template <typename F>
148-
decltype(auto) decompose_and_apply(
149-
DecomposableStructurallyBindable auto &&v, const F &f) {
150-
return decompose_and_apply<structured_binding_size_v<decltype(v)>>(
151-
std::forward<decltype(v)>(v), f);
152-
}
153-
154132
} // namespace structurally_bindable
155133

156134
namespace aggregate {
@@ -199,17 +177,6 @@ namespace scale::detail::decomposable {
199177
and (not array::DecomposableArray<T>)
200178
and (not structurally_bindable::DecomposableStructurallyBindable<T>)
201179
and (not collections::Collection<T>) and AggregateWithMaxSize<T>;
202-
203-
/**
204-
* @brief Decomposes an aggregate and applies a given function.
205-
*/
206-
template <typename F>
207-
decltype(auto) decompose_and_apply(DecomposableAggregate auto &&v,
208-
const F &f) {
209-
return decompose_and_apply<field_number_of<decltype(v)>>(
210-
std::forward<decltype(v)>(v), f);
211-
}
212-
213180
} // namespace aggregate
214181

215182
using aggregate::DecomposableAggregate;

test/scale_decomposable_test.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
*/
66

77
/**
8-
* @brief Unit tests for verifying the correctness of SCALE encoding and decoding
9-
* for various decomposable data structures.
8+
* @brief Unit tests for verifying the correctness of SCALE encoding and
9+
* decoding for various decomposable data structures.
1010
*
1111
* This file contains test cases that check the encoding and decoding of
1212
* fundamental types, arrays, tuples, and user-defined structures using
@@ -17,6 +17,7 @@
1717

1818
#include <set>
1919

20+
#include <qtils/empty.hpp>
2021
#include <qtils/test/outcome.hpp>
2122
#include <scale/bitvec.hpp>
2223
#include <scale/scale.hpp>
@@ -31,6 +32,17 @@ using scale::impl::memory::encode;
3132
using Encoder = scale::backend::ToBytes;
3233
using Decoder = scale::backend::FromBytes;
3334

35+
TEST(Decomposable, Empty) {
36+
using Testee = qtils::Empty;
37+
38+
Testee value{};
39+
40+
ASSERT_OUTCOME_SUCCESS(encoded, encode(value));
41+
ASSERT_OUTCOME_SUCCESS(decode<Testee>(encoded));
42+
43+
ASSERT_TRUE(encoded.empty());
44+
}
45+
3446
TEST(Decomposable, C_Style_array) {
3547
using Testee = const uint16_t[3];
3648

0 commit comments

Comments
 (0)