mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-06-30 10:42:23 +02:00
Merge bitcoin/bitcoin#23451: span: Add std::byte helpers
faa3ec2304
span: Add std::byte helpers (MarcoFalke)fa18038f51
refactor: Use ignore helper when unserializing an invalid pubkey (MarcoFalke)fabe18d0b3
Use value_type in CDataStream where possible (MarcoFalke) Pull request description: This adds (currently unused) span std::byte helpers, so that they can be used in new code. The refactors are also required for https://github.com/bitcoin/bitcoin/pull/23438, but they are split up because the other pull doesn't compile with msvc right now. The third commit is not needed for the other pull, but still nice. ACKs for top commit: klementtan: reACKfaa3ec2
. Verified that all the new `std::byte` helper functions are tested. laanwj: Code review ACKfaa3ec2304
Tree-SHA512: b1f6af39f03ea4dfebf20d4a8538fa993a6104e7fc92ddf0c4606a7efc3ca9a8c1a4741d98a1418569c11bb9ce9258bf0c0c06d93d85ed7e208902a2db04e407
This commit is contained in:
25
src/span.h
25
src/span.h
@ -184,6 +184,7 @@ public:
|
||||
return m_data[m_size - 1];
|
||||
}
|
||||
constexpr std::size_t size() const noexcept { return m_size; }
|
||||
constexpr std::size_t size_bytes() const noexcept { return sizeof(C) * m_size; }
|
||||
constexpr bool empty() const noexcept { return size() == 0; }
|
||||
CONSTEXPR_IF_NOT_DEBUG C& operator[](std::size_t pos) const noexcept
|
||||
{
|
||||
@ -240,11 +241,35 @@ T& SpanPopBack(Span<T>& span)
|
||||
return back;
|
||||
}
|
||||
|
||||
// From C++20 as_bytes and as_writeable_bytes
|
||||
template <typename T>
|
||||
Span<const std::byte> AsBytes(Span<T> s) noexcept
|
||||
{
|
||||
return {reinterpret_cast<const std::byte*>(s.data()), s.size_bytes()};
|
||||
}
|
||||
template <typename T>
|
||||
Span<std::byte> AsWritableBytes(Span<T> s) noexcept
|
||||
{
|
||||
return {reinterpret_cast<std::byte*>(s.data()), s.size_bytes()};
|
||||
}
|
||||
|
||||
template <typename V>
|
||||
Span<const std::byte> MakeByteSpan(V&& v) noexcept
|
||||
{
|
||||
return AsBytes(MakeSpan(std::forward<V>(v)));
|
||||
}
|
||||
template <typename V>
|
||||
Span<std::byte> MakeWritableByteSpan(V&& v) noexcept
|
||||
{
|
||||
return AsWritableBytes(MakeSpan(std::forward<V>(v)));
|
||||
}
|
||||
|
||||
// Helper functions to safely cast to unsigned char pointers.
|
||||
inline unsigned char* UCharCast(char* c) { return (unsigned char*)c; }
|
||||
inline unsigned char* UCharCast(unsigned char* c) { return c; }
|
||||
inline const unsigned char* UCharCast(const char* c) { return (unsigned char*)c; }
|
||||
inline const unsigned char* UCharCast(const unsigned char* c) { return c; }
|
||||
inline const unsigned char* UCharCast(const std::byte* c) { return reinterpret_cast<const unsigned char*>(c); }
|
||||
|
||||
// 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()}; }
|
||||
|
Reference in New Issue
Block a user