Sorry, noob question. I have a simple struct
const KV = struct {
key: []const u8,
val: []const u8
};
A function is deserializing a []u8 into it. When I print it out inside the function all the data looks great, then outside, no bueno. Deserializing function. Pardon if the aloc makes legit no sense, I was just trying stuff to get it to work...
fn deserializeCaskEntry(aloc: *const mem.Allocator, cask_bytes: []u8) !*KV {
var kv = try aloc.create(KV);
std.debug.print("Deserializing slice: {any}\n", .{cask_bytes});
const size_len = @sizeOf(usize);
const key_size = std.mem.bytesToValue(usize, cask_bytes[0..size_len]);
const val_size = std.mem.bytesToValue(usize, cask_bytes[size_len..size_len*2]);
std.debug.print("Got key size [{}] and val size [{}]\n", .{key_size, val_size});
const key_bytes = cask_bytes[size_len*2..size_len*2 + key_size];
const val_bytes = cask_bytes[size_len*2 + key_size..size_len*2 + key_size + val_size];
kv.* = KV{.key = key_bytes, .val = val_bytes};
std.debug.print("{any}\n", .{kv});
return kv;
}
This is called in
pub fn get(key: []const u8) !*KV {
... <setting up seek and printing stuff>
_ = try current_file.read(bytes);
const kv = deserializeCaskEntry(allocator, cask_bytes) catch |err| {
std.debug.print("Encountered unknown error while deserializing bitcask: {any}\n", .{err});
return BitcaskFileError.Unknown;
};
return kv;
}
And this get is being called in a test in the file
test "Bitcask spec implementation: get" {
const bc = BitCask;
try bc.open("data");
defer bc.close();
try bc.put(.{.key = "2", .val = "secret"});
const kv = try bc.get("2");
std.debug.print("Got kv back\nKey: {any}\tVal: {any}", .{kv.key, kv.val});
}