// Copyright (c) 2025 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #ifndef MP_PROXY_TYPE_DATA_H #define MP_PROXY_TYPE_DATA_H #include namespace mp { template concept IsSpanOf = std::convertible_to> && std::constructible_from; template concept IsByteSpan = IsSpanOf || IsSpanOf || IsSpanOf || IsSpanOf; //! Generic ::capnp::Data field builder for any C++ type that can be converted //! to a span of bytes, like std::vector or std::array, or custom //! blob types like uint256 or PKHash with data() and size() methods pointing to //! bytes. template void CustomBuildField(TypeList, Priority<2>, InvokeContext& invoke_context, Value&& value, Output&& output) requires (std::is_same_v && IsByteSpan) { auto data = std::span{value}; auto result = output.init(data.size()); memcpy(result.begin(), data.data(), data.size()); } template decltype(auto) CustomReadField(TypeList, Priority<2>, InvokeContext& invoke_context, Input&& input, ReadDest&& read_dest) requires (std::is_same_v && IsByteSpan) { using ByteType = decltype(std::span{std::declval().begin(), std::declval().end()})::element_type; const kj::byte *begin{input.get().begin()}, *end{input.get().end()}; return read_dest.construct(reinterpret_cast(begin), reinterpret_cast(end)); } } // namespace mp #endif // MP_PROXY_TYPE_DATA_H