I have the following zig code (simplified):
const allocator = ...;
const Entry = struct {
name: []u8,
};
fn list() ![]u8 {
var entries = try std.json.parse([]Entry, ...);
defer std.json.parseFree([]Entry, entries, ...);
return entries[0];
}
fn main() !void {
const e = try list();
...
}
Everything is (de)allocated with allocator
.
I'd like to return entries[0]
and recycle everything else. The problem is that parseFree
here recycles everything including entries[0]
so I can't seemingly use this function.
So what's the most effective way to do that without copying? What if Entry
is a big structure and I want to return just one its field, say Entry.name
(and, again, recycle everything else)?