aboutsummaryrefslogtreecommitdiff
path: root/src/span.h
diff options
context:
space:
mode:
authorMarcoFalke <falke.marco@gmail.com>2021-11-24 11:03:43 +0100
committerMarcoFalke <falke.marco@gmail.com>2021-11-24 11:04:37 +0100
commit9394964f6b9d1cf1220a4eca17ba18dc49ae876d (patch)
tree4d34bd6c992aa8fb2b37aef1bea43c9477cf73c6 /src/span.h
parent73ac195e29cc3991cb97c6c3dce8d64eaad0e57a (diff)
parentfaa3ec2304051be7cfbe301cfbfbda3faf7514fc (diff)
downloadbitcoin-9394964f6b9d1cf1220a4eca17ba18dc49ae876d.tar.xz
Merge bitcoin/bitcoin#23451: span: Add std::byte helpers
faa3ec2304051be7cfbe301cfbfbda3faf7514fc span: Add std::byte helpers (MarcoFalke) fa18038f519db76befb9a7bd0b1540143bfeb12b refactor: Use ignore helper when unserializing an invalid pubkey (MarcoFalke) fabe18d0b39b4b918bf60e3a313eaa36fb4067f2 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: reACK faa3ec2. Verified that all the new `std::byte` helper functions are tested. laanwj: Code review ACK faa3ec2304051be7cfbe301cfbfbda3faf7514fc Tree-SHA512: b1f6af39f03ea4dfebf20d4a8538fa993a6104e7fc92ddf0c4606a7efc3ca9a8c1a4741d98a1418569c11bb9ce9258bf0c0c06d93d85ed7e208902a2db04e407
Diffstat (limited to 'src/span.h')
-rw-r--r--src/span.h25
1 files changed, 25 insertions, 0 deletions
diff --git a/src/span.h b/src/span.h
index 1e347846d8..746e41f2f4 100644
--- a/src/span.h
+++ b/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()}; }