mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-07-15 15:53:04 +02:00
refactor: Drop unsafe AsBytePtr function
Replace calls to AsBytePtr with direct calls to AsBytes or reinterpret_cast. AsBytePtr is just a wrapper around reinterpret_cast. It accepts any type of pointer as an argument and uses reinterpret_cast to cast the argument to a std::byte pointer. Despite taking any type of pointer as an argument, it is not useful to call AsBytePtr on most types of pointers, because byte representations of most types will be implmentation-specific. Also, because it is named similarly to the AsBytes function, AsBytePtr looks safer than it actually is. Both AsBytes and AsBytePtr call reinterpret_cast internally and may be unsafe to use with certain types, but AsBytes at least has some type checking and can only be called on Span objects, while AsBytePtr can be called on any pointer argument. Co-authored-by: Pieter Wuille <pieter@wuille.net>
This commit is contained in:
@ -243,21 +243,16 @@ T& SpanPopBack(Span<T>& span)
|
||||
return back;
|
||||
}
|
||||
|
||||
//! Convert a data pointer to a std::byte data pointer.
|
||||
//! Where possible, please use the safer AsBytes helpers.
|
||||
inline const std::byte* AsBytePtr(const void* data) { return reinterpret_cast<const std::byte*>(data); }
|
||||
inline std::byte* AsBytePtr(void* data) { return reinterpret_cast<std::byte*>(data); }
|
||||
|
||||
// From C++20 as_bytes and as_writeable_bytes
|
||||
template <typename T>
|
||||
Span<const std::byte> AsBytes(Span<T> s) noexcept
|
||||
{
|
||||
return {AsBytePtr(s.data()), s.size_bytes()};
|
||||
return {reinterpret_cast<const std::byte*>(s.data()), s.size_bytes()};
|
||||
}
|
||||
template <typename T>
|
||||
Span<std::byte> AsWritableBytes(Span<T> s) noexcept
|
||||
{
|
||||
return {AsBytePtr(s.data()), s.size_bytes()};
|
||||
return {reinterpret_cast<std::byte*>(s.data()), s.size_bytes()};
|
||||
}
|
||||
|
||||
template <typename V>
|
||||
|
Reference in New Issue
Block a user