I have the following code (part of it) for Bencode BitTorrent decoder. I looked through other questions and learned that function arguments are immutable, hence the *const. How to fix this code?
pub const Bencoder = struct {
arena_allocator: std.heap.ArenaAllocator,
pub fn init(arena_child_allocator: std.mem.Allocator) @This() {
return .{ .arena_allocator = std.heap.ArenaAllocator.init(arena_child_allocator) };
}
pub fn deinit(self: @This()) void {
log.debug("Freeing memory from arena allocator for Bencoder", .{});
self.arena_allocator.deinit();
}
...
I want to have an arena allocator to be able to free all memory at once when decoding is done. Passing child allocator is done in the init() function. Below is the code when I pass allocator to ArrayList:
var list = std.ArrayList(Value).init(self.arena_allocator.allocator());
Test code:
test "should decode integers" {
const b = Bencoder.init(std.testing.allocator);
defer b.deinit();
const fourty_two = b.decode("i42e") catch unreachable;
const minus_fourty_two = b.decode("i-42e") catch unreachable;
const zero = b.decode("i0e") catch unreachable;
try std.testing.expect(fourty_two.value.integer == 42);
try std.testing.expect(fourty_two.read == 4);
try std.testing.expect(minus_fourty_two.value.integer == -42);
try std.testing.expect(minus_fourty_two.read == 5);
try std.testing.expect(zero.value.integer == 0);
try std.testing.expect(zero.read == 3);
}
And the error I am getting:
src\encoding\bencode.zig:77:74: error: expected type '*heap.arena_allocator.ArenaAllocator', found '*const heap.arena_allocator.ArenaAllocator'
var list = std.ArrayList(Value).init(self.arena_allocator.allocator());
~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~
src\encoding\bencode.zig:77:74: note: cast discards const qualifier
C:\Users\Johnny\scoop\persist\zigup\zig\0.14.0-dev.224+95d9292a7\files\lib\std\heap\arena_allocator.zig:26:28: note: parameter type declared here
pub fn allocator(self: *ArenaAllocator) Allocator {
^~~~~~~~~~~~~~~
referenced by:
test.should decode integers: src\encoding\bencode.zig:133:32
Process finished with exit code 1
