From 7d0c4eaf1b83480695cee9206dce19b539cb3f12 Mon Sep 17 00:00:00 2001 From: dragonmux Date: Wed, 3 Jan 2024 15:04:29 +0000 Subject: [PATCH] rust: Reworked `npnr_context_get_wires_leak()` using std::accumulate() and fixed an accidental copy problem --- rust/rust.cc | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/rust/rust.cc b/rust/rust.cc index 25aa63e1..d7032b74 100644 --- a/rust/rust.cc +++ b/rust/rust.cc @@ -16,6 +16,7 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ +#include #include "log.h" #include "nextpnr.h" @@ -117,15 +118,15 @@ extern "C" { return size; } - uint64_t npnr_context_get_wires_leak(const Context *ctx, uint64_t **wires) { - auto size = size_t{}; - for (auto _wire : ctx->getWires()) { - NPNR_UNUSED(_wire); - size++; - } + uint64_t npnr_context_get_wires_leak(const Context *const ctx, uint64_t **const wires) { + const auto ctx_wires{ctx->getWires()}; + const auto size{ + std::accumulate(ctx_wires.begin(), ctx_wires.end(), /*initial value*/ size_t{}, + [](size_t value, const auto &/*wire*/) { return value + 1U; } + )}; *wires = new uint64_t[size]; auto idx = 0; - for (auto wire : ctx->getWires()) { + for (const auto &wire : ctx_wires) { (*wires)[idx] = wrap(wire); idx++; }