Skip to content

Add ReadList helper#285

Open
ViniciusCestarii wants to merge 2 commits into
bitcoin-core:masterfrom
ViniciusCestarii:add-readlist-helper
Open

Add ReadList helper#285
ViniciusCestarii wants to merge 2 commits into
bitcoin-core:masterfrom
ViniciusCestarii:add-readlist-helper

Conversation

@ViniciusCestarii

@ViniciusCestarii ViniciusCestarii commented Jun 3, 2026

Copy link
Copy Markdown
Contributor

Follow up of #277 (review). This adds ReadList helper and uses it to dedup the CustomReadField implementations for std::map, std::set, std::unordered_set, and std::vector, mirroring the existing BuildList helper on the build side.

I took the liberty of adding the commit f2a734a "type: reserve first when reading std::unordered_set" so it reserves the correct capacity first and then emplaces the values.

@DrahtBot

DrahtBot commented Jun 3, 2026

Copy link
Copy Markdown

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Reviews

See the guideline for information on the review process.

Type Reviewers
ACK xyzconstant
Concept ACK ryanofsky

If your review is incorrectly listed, please copy-paste <!--meta-tag:bot-skip--> into the comment that the bot should ignore.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #288 (Create support branch for CI scripts, documentation, and examples by ryanofsky)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

@ryanofsky ryanofsky left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Concept ACK f2a734a. Thanks for the followup. Plan to review more after #277 is merged

Comment thread include/mp/type-map.h Outdated
@ViniciusCestarii

Copy link
Copy Markdown
Contributor Author

Forced push 245f792 to just rebase with master

@ViniciusCestarii

Copy link
Copy Markdown
Contributor Author

Forced push 5d23f83 applying @ryanofsky suggestion. It reads cleaner and dedup more code.

@ViniciusCestarii ViniciusCestarii marked this pull request as ready for review June 11, 2026 12:55
Comment thread include/mp/type-vector.h
Comment on lines 46 to 62
template <typename Input, typename ReadDest>
decltype(auto) CustomReadField(TypeList<std::vector<bool>>,
Priority<1>,
InvokeContext& invoke_context,
Input&& input,
ReadDest&& read_dest)
{
return read_dest.update([&](auto& value) {
auto data = input.get();
value.clear();
value.reserve(data.size());
for (auto item : data) {
value.push_back(ReadField(TypeList<bool>(), invoke_context, Make<ValueField>(item), ReadDestTemp<bool>()));
}
});
}
} // namespace mp

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this work here?

diff --git a/include/mp/type-vector.h b/include/mp/type-vector.h
index 25e74a6..cd269cb 100644
--- a/include/mp/type-vector.h
+++ b/include/mp/type-vector.h
@@ -50,14 +50,16 @@ decltype(auto) CustomReadField(TypeList<std::vector<bool>>,
                                Input&& input,
                                ReadDest&& read_dest)
 {
-    return read_dest.update([&](auto& value) {
-        auto data = input.get();
-        value.clear();
-        value.reserve(data.size());
-        for (auto item : data) {
-            value.push_back(ReadField(TypeList<bool>(), invoke_context, Make<ValueField>(item), ReadDestTemp<bool>()));
-        }
-    });
+    return ReadList(
+        TypeList<bool>(), invoke_context, input, read_dest,
+        [&](auto& value, size_t size) {
+            value.clear();
+            value.reserve(size);
+        },
+        [&](auto& value, auto&& item) {
+            value.push_back(item);
+            return value.back();
+        });
 }
 } // namespace mp

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh I missed this case, thanks for pointing!

I tested your approach and it does compile and passes the tests but I think it is better to not add return value.back(); because bool is read via construct(), so the return value is always unused.

Also I think it is a good opportunity to add a comment to explain why there's this exception for std::vector<bool>.

What do you think?

diff --git a/include/mp/type-vector.h b/include/mp/type-vector.h
index 25e74a6..49ca3c2 100644
--- a/include/mp/type-vector.h
+++ b/include/mp/type-vector.h
@@ -43,6 +43,11 @@ decltype(auto) CustomReadField(TypeList<std::vector<LocalType>>,
         });
 }
 
+//! Overload CustomReadField for std::vector<bool>, which needs its own handler
+//! because its back() returns a proxy by value (std::vector<bool>::reference)
+//! rather than a reference, so it can't use the generic vector in-place emplace path
+//! that returns auto&. Not returning a reference is fine here: bool is read via
+//! construct(), not update(), so the return value is never used.
 template <typename Input, typename ReadDest>
 decltype(auto) CustomReadField(TypeList<std::vector<bool>>,
                                Priority<1>,
@@ -50,14 +55,15 @@ decltype(auto) CustomReadField(TypeList<std::vector<bool>>,
                                Input&& input,
                                ReadDest&& read_dest)
 {
-    return read_dest.update([&](auto& value) {
-        auto data = input.get();
-        value.clear();
-        value.reserve(data.size());
-        for (auto item : data) {
-            value.push_back(ReadField(TypeList<bool>(), invoke_context, Make<ValueField>(item), ReadDestTemp<bool>()));
-        }
-    });
+    return ReadList(
+        TypeList<bool>(), invoke_context, input, read_dest,
+        [&](auto& value, size_t size) {
+            value.clear();
+            value.reserve(size);
+        },
+        [&](auto& value, auto&& item) {
+            value.push_back(item);
+        });
 }
 } // namespace mp
 

@xyzconstant xyzconstant Jun 22, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great! This is definitely better, and the comment is on point.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done on 1bfc594.

@ViniciusCestarii

Copy link
Copy Markdown
Contributor Author

Forced push 043b8a8 applying @xyzconstant suggestion

@xyzconstant

Copy link
Copy Markdown
Contributor

tACK 043b8a8

Thanks for the updates @ViniciusCestarii.

This is a simple refactor that moves common code to ReadList, consistent with existing BuildList helper.

The changes are clean and can be merged as-is.

@DrahtBot DrahtBot requested a review from ryanofsky June 23, 2026 14:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants