Skip to content

Commit fadf02e

Browse files
author
MarcoFalke
committed
refactor: Return std::span from MakeUCharSpan
This is possible and safe, because std::span can implicitly convert into Span, if needed.
1 parent fa720b9 commit fadf02e

File tree

2 files changed

+6
-5
lines changed

2 files changed

+6
-5
lines changed

src/span.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,9 +290,10 @@ template <typename B>
290290
concept BasicByte = requires { UCharCast(std::span<B>{}.data()); };
291291

292292
// Helper function to safely convert a Span to a Span<[const] unsigned char>.
293-
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()}; }
293+
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()}; }
294294

295295
/** Like the Span constructor, but for (const) unsigned char member types only. Only works for (un)signed char containers. */
296-
template <typename V> constexpr auto MakeUCharSpan(V&& v) -> decltype(UCharSpanCast(Span{std::forward<V>(v)})) { return UCharSpanCast(Span{std::forward<V>(v)}); }
296+
template <typename V> constexpr auto MakeUCharSpan(const V& v) -> decltype(UCharSpanCast(std::span{v})) { return UCharSpanCast(std::span{v}); }
297+
template <typename V> constexpr auto MakeWritableUCharSpan(V&& v) -> decltype(UCharSpanCast(std::span{std::forward<V>(v)})) { return UCharSpanCast(std::span{std::forward<V>(v)}); }
297298

298299
#endif // BITCOIN_SPAN_H

src/test/util_tests.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,8 @@ BOOST_AUTO_TEST_CASE(parse_hex)
160160
BOOST_CHECK_EQUAL_COLLECTIONS(hex_literal_span.begin(), hex_literal_span.end(), expected.begin(), expected.end());
161161

162162
const std::vector<std::byte> hex_literal_vector{operator""_hex_v<util::detail::Hex(HEX_PARSE_INPUT)>()};
163-
hex_literal_span = MakeUCharSpan(hex_literal_vector);
164-
BOOST_CHECK_EQUAL_COLLECTIONS(hex_literal_span.begin(), hex_literal_span.end(), expected.begin(), expected.end());
163+
auto hex_literal_vec_span = MakeUCharSpan(hex_literal_vector);
164+
BOOST_CHECK_EQUAL_COLLECTIONS(hex_literal_vec_span.begin(), hex_literal_vec_span.end(), expected.begin(), expected.end());
165165

166166
constexpr std::array<uint8_t, 65> hex_literal_array_uint8{operator""_hex_u8<util::detail::Hex(HEX_PARSE_INPUT)>()};
167167
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)
242242
{
243243
constexpr std::string_view out_exp{"04678afdb0"};
244244
constexpr std::span in_s{HEX_PARSE_OUTPUT, out_exp.size() / 2};
245-
const Span<const uint8_t> in_u{MakeUCharSpan(in_s)};
245+
const std::span<const uint8_t> in_u{MakeUCharSpan(in_s)};
246246
const std::span<const std::byte> in_b{MakeByteSpan(in_s)};
247247

248248
BOOST_CHECK_EQUAL(HexStr(in_u), out_exp);

0 commit comments

Comments
 (0)