refactor: Return std::span from MakeUCharSpan

This is possible and safe, because std::span can implicitly convert into
Span, if needed.
This commit is contained in:
MarcoFalke 2024-12-17 21:34:39 +01:00
parent fa720b94be
commit fadf02ef8b
No known key found for this signature in database
2 changed files with 6 additions and 5 deletions

View file

@ -290,9 +290,10 @@ template <typename B>
concept BasicByte = requires { UCharCast(std::span<B>{}.data()); }; concept BasicByte = requires { UCharCast(std::span<B>{}.data()); };
// Helper function to safely convert a Span to a Span<[const] unsigned char>. // Helper function to safely convert a Span to a Span<[const] unsigned char>.
template <typename T> constexpr auto UCharSpanCast(Span<T> s) -> Span<typename std::remove_pointer<decltype(UCharCast(s.data()))>::type> { return {UCharCast(s.data()), s.size()}; } template <typename T, size_t N> constexpr auto UCharSpanCast(std::span<T, N> s) { return std::span<std::remove_pointer_t<decltype(UCharCast(s.data()))>, N>{UCharCast(s.data()), s.size()}; }
/** Like the Span constructor, but for (const) unsigned char member types only. Only works for (un)signed char containers. */ /** Like the Span constructor, but for (const) unsigned char member types only. Only works for (un)signed char containers. */
template <typename V> constexpr auto MakeUCharSpan(V&& v) -> decltype(UCharSpanCast(Span{std::forward<V>(v)})) { return UCharSpanCast(Span{std::forward<V>(v)}); } template <typename V> constexpr auto MakeUCharSpan(const V& v) -> decltype(UCharSpanCast(std::span{v})) { return UCharSpanCast(std::span{v}); }
template <typename V> constexpr auto MakeWritableUCharSpan(V&& v) -> decltype(UCharSpanCast(std::span{std::forward<V>(v)})) { return UCharSpanCast(std::span{std::forward<V>(v)}); }
#endif // BITCOIN_SPAN_H #endif // BITCOIN_SPAN_H

View file

@ -160,8 +160,8 @@ BOOST_AUTO_TEST_CASE(parse_hex)
BOOST_CHECK_EQUAL_COLLECTIONS(hex_literal_span.begin(), hex_literal_span.end(), expected.begin(), expected.end()); BOOST_CHECK_EQUAL_COLLECTIONS(hex_literal_span.begin(), hex_literal_span.end(), expected.begin(), expected.end());
const std::vector<std::byte> hex_literal_vector{operator""_hex_v<util::detail::Hex(HEX_PARSE_INPUT)>()}; const std::vector<std::byte> hex_literal_vector{operator""_hex_v<util::detail::Hex(HEX_PARSE_INPUT)>()};
hex_literal_span = MakeUCharSpan(hex_literal_vector); auto hex_literal_vec_span = MakeUCharSpan(hex_literal_vector);
BOOST_CHECK_EQUAL_COLLECTIONS(hex_literal_span.begin(), hex_literal_span.end(), expected.begin(), expected.end()); BOOST_CHECK_EQUAL_COLLECTIONS(hex_literal_vec_span.begin(), hex_literal_vec_span.end(), expected.begin(), expected.end());
constexpr std::array<uint8_t, 65> hex_literal_array_uint8{operator""_hex_u8<util::detail::Hex(HEX_PARSE_INPUT)>()}; constexpr std::array<uint8_t, 65> hex_literal_array_uint8{operator""_hex_u8<util::detail::Hex(HEX_PARSE_INPUT)>()};
BOOST_CHECK_EQUAL_COLLECTIONS(hex_literal_array_uint8.begin(), hex_literal_array_uint8.end(), expected.begin(), expected.end()); BOOST_CHECK_EQUAL_COLLECTIONS(hex_literal_array_uint8.begin(), hex_literal_array_uint8.end(), expected.begin(), expected.end());
@ -242,7 +242,7 @@ BOOST_AUTO_TEST_CASE(util_HexStr)
{ {
constexpr std::string_view out_exp{"04678afdb0"}; constexpr std::string_view out_exp{"04678afdb0"};
constexpr std::span in_s{HEX_PARSE_OUTPUT, out_exp.size() / 2}; constexpr std::span in_s{HEX_PARSE_OUTPUT, out_exp.size() / 2};
const Span<const uint8_t> in_u{MakeUCharSpan(in_s)}; const std::span<const uint8_t> in_u{MakeUCharSpan(in_s)};
const std::span<const std::byte> in_b{MakeByteSpan(in_s)}; const std::span<const std::byte> in_b{MakeByteSpan(in_s)};
BOOST_CHECK_EQUAL(HexStr(in_u), out_exp); BOOST_CHECK_EQUAL(HexStr(in_u), out_exp);