#Can you concat a string literal and a [_][]const u8?

1 messages · Page 1 of 1 (latest)

timber sandal
#

something like this:

fn query(comptime T: type, allocator: Allocator, args: []const []const u8) !T {
    const command = "query";
    const stream = try sendAndGetStream(&[_][]const u8{ command, args });
gentle jasper
#

string literals can implicitly coerce to []const u8, yes
but thats not how to concatenate slices

#

since you don't know the length of args you'll have to use std.mem.concat

timber sandal
#

and allocate, right

gentle jasper
#

std.mem.concat allocates,yes

timber sandal
#

meh I might as well pass "query" in args

#

or append to a bounded array

#

which is becoming my fav collection

#

it would be awesome to have a bounded array that allocates if it exceeds the comptime size

#

like SmallVec in rust

digital marsh
#

StackFallbackAllocator allocates into a fixed buffer, and if you exceed the buffer size, then starts using a fallback allocator

#

You could use one something like this:

var sfb = std.heap.stackFallback(1024, allocator); // reserves a 1024 byte stack buffer
const sfba = sfb.get();
var bytes = std.ArrayList(u8).init(sfba);
defer bytes.deinit();
// do stuff with bytes
timber sandal
#

nice! didn't know about StackFallbackAllocator

#

this solves for CompactString(kinda) and SmallVec

#

thank you