forked from lix-project/lix
withBuffer: avoid allocating a std::function
This commit is contained in:
parent
55c58580be
commit
6dd271b7b4
|
@ -273,7 +273,7 @@ Derivation parseDerivation(const Store & store, std::string && s, std::string_vi
|
||||||
static void printString(string & res, std::string_view s)
|
static void printString(string & res, std::string_view s)
|
||||||
{
|
{
|
||||||
size_t bufSize = s.size() * 2 + 2;
|
size_t bufSize = s.size() * 2 + 2;
|
||||||
withBuffer<void, char>(bufSize, [&](char buf[]) {
|
withBuffer(bufSize, [&](char *buf) {
|
||||||
char * p = buf;
|
char * p = buf;
|
||||||
*p++ = '"';
|
*p++ = '"';
|
||||||
for (auto c : s)
|
for (auto c : s)
|
||||||
|
|
|
@ -671,7 +671,10 @@ template<class... Ts> overloaded(Ts...) -> overloaded<Ts...>;
|
||||||
|
|
||||||
std::string showBytes(uint64_t bytes);
|
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) {
|
if (size < 0x10000) {
|
||||||
T buf[size];
|
T buf[size];
|
||||||
return fun(buf);
|
return fun(buf);
|
||||||
|
|
Loading…
Reference in a new issue