withBuffer: avoid allocating a std::function

This commit is contained in:
Robert Hensing 2022-01-06 15:11:37 +01:00
parent 55c58580be
commit 6dd271b7b4
2 changed files with 5 additions and 2 deletions

View file

@ -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)

View file

@ -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);