In your previous approach (c6d532414406cff5d31819e10adbf04042a65746) you, just used a Ctx wrapper. Any reason you changed it to this overload? I thought the wrapper was quite elegant? The new approach is a lot of unnecessary duplication.
(nit: if you update, Item seems like a more helpful name than Ctx.)
<details>
<summary>git diff on fa221030ac</summary>
diff --git a/src/kernel/bitcoinkernel_wrapper.h b/src/kernel/bitcoinkernel_wrapper.h
index d6b06d3c13..3bfd4a48ac 100644
--- a/src/kernel/bitcoinkernel_wrapper.h
+++ b/src/kernel/bitcoinkernel_wrapper.h
@@ -320,36 +320,6 @@ std::vector<std::byte> write_bytes(const T* object, int (*to_bytes)(const T*, bt
return bytes;
}
-template <typename T>
-std::vector<std::byte> write_bytes(const T* object, size_t index, int (*to_bytes)(const T*, size_t, btck_WriteBytes, void*))
-{
- std::vector<std::byte> bytes;
- struct UserData {
- std::vector<std::byte>* bytes;
- std::exception_ptr exception;
- };
- UserData user_data = UserData{.bytes = &bytes, .exception = nullptr};
-
- constexpr auto const write = +[](const void* buffer, size_t len, void* user_data) -> int {
- auto& data = *reinterpret_cast<UserData*>(user_data);
- auto& bytes = *data.bytes;
- try {
- auto const* first = static_cast<const std::byte*>(buffer);
- auto const* last = first + len;
- bytes.insert(bytes.end(), first, last);
- return 0;
- } catch (...) {
- data.exception = std::current_exception();
- return -1;
- }
- };
-
- if (to_bytes(object, index, write, &user_data) != 0) {
- std::rethrow_exception(user_data.exception);
- }
- return bytes;
-}
-
template <typename CType>
class View
{
@@ -614,7 +584,11 @@ public:
std::vector<std::byte> Item(size_t index) const
{
- return write_bytes(impl(), index, btck_witness_stack_get_item);
+ struct Item { const btck_WitnessStack* stack; size_t index; };
+ Item ctx{impl(), index};
+ return write_bytes(&ctx, +[](const Item* item, btck_WriteBytes w, void* ud) {
+ return btck_witness_stack_get_item(item->stack, item->index, w, ud);
+ });
}
MAKE_RANGE_METHOD(Items, Derived, &WitnessStackApi<Derived>::Count, &WitnessStackApi<Derived>::Item, *static_cast<const Derived*>(this))
</details>