aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorRobert Hensing <robert@roberthensing.nl>2022-01-06 15:11:37 +0100
committerRobert Hensing <robert@roberthensing.nl>2022-01-19 15:21:56 +0100
commit6dd271b7b4a679ce3c2a8d84a39909bb9b60bf9f (patch)
tree47bfa34104b3e95bd72c34a072386f535dda614b /src
parent55c58580be89e0f7ec6519bd7a7f2cded4fab382 (diff)
withBuffer: avoid allocating a std::function
Diffstat (limited to 'src')
-rw-r--r--src/libstore/derivations.cc2
-rw-r--r--src/libutil/util.hh5
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<void, char>(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<class... Ts> overloaded(Ts...) -> overloaded<Ts...>;
std::string showBytes(uint64_t bytes);
-template<typename R = void, typename T = char> inline R withBuffer(size_t size, std::function<R (T[])> fun) {
+template<typename T = char, typename Fn>
+inline auto withBuffer(size_t size, Fn fun)
+ -> std::invoke_result_t<Fn, T *>
+{
if (size < 0x10000) {
T buf[size];
return fun(buf);