From 6dd271b7b4a679ce3c2a8d84a39909bb9b60bf9f Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Thu, 6 Jan 2022 15:11:37 +0100 Subject: [PATCH] withBuffer: avoid allocating a std::function --- src/libstore/derivations.cc | 2 +- src/libutil/util.hh | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/libstore/derivations.cc b/src/libstore/derivations.cc index 21ea84dbf..f8fe6a59c 100644 --- a/src/libstore/derivations.cc +++ b/src/libstore/derivations.cc @@ -273,7 +273,7 @@ Derivation parseDerivation(const Store & store, std::string && s, std::string_vi static void printString(string & res, std::string_view s) { size_t bufSize = s.size() * 2 + 2; - withBuffer(bufSize, [&](char buf[]) { + withBuffer(bufSize, [&](char *buf) { char * p = buf; *p++ = '"'; for (auto c : s) diff --git a/src/libutil/util.hh b/src/libutil/util.hh index 840acd67b..407505be9 100644 --- a/src/libutil/util.hh +++ b/src/libutil/util.hh @@ -671,7 +671,10 @@ template overloaded(Ts...) -> overloaded; std::string showBytes(uint64_t bytes); -template inline R withBuffer(size_t size, std::function fun) { +template +inline auto withBuffer(size_t size, Fn fun) + -> std::invoke_result_t +{ if (size < 0x10000) { T buf[size]; return fun(buf);