Skip to content

Commit 6f9a1e5

Browse files
ryanofskysipa
authored andcommitted
Extend CustomUintFormatter to support enums
Extracted by Pieter Wuille from a comment by Russ Yanofsky, see bitcoin/bitcoin#18317 (comment).
1 parent 769ee5f commit 6f9a1e5

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

src/serialize.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -538,14 +538,15 @@ struct CustomUintFormatter
538538

539539
template <typename Stream, typename I> void Unser(Stream& s, I& v)
540540
{
541-
static_assert(std::numeric_limits<I>::max() >= MAX && std::numeric_limits<I>::min() <= 0, "CustomUintFormatter type too small");
541+
using U = typename std::conditional<std::is_enum<I>::value, std::underlying_type<I>, std::common_type<I>>::type::type;
542+
static_assert(std::numeric_limits<U>::max() >= MAX && std::numeric_limits<U>::min() <= 0, "Assigned type too small");
542543
uint64_t raw = 0;
543544
if (BigEndian) {
544545
s.read(((char*)&raw) + 8 - Bytes, Bytes);
545-
v = be64toh(raw);
546+
v = static_cast<I>(be64toh(raw));
546547
} else {
547548
s.read((char*)&raw, Bytes);
548-
v = le64toh(raw);
549+
v = static_cast<I>(le64toh(raw));
549550
}
550551
}
551552
};

0 commit comments

Comments
 (0)