Arwalk / zig-protobuf

a protobuf 3 implementation for zig.
MIT License
188 stars 20 forks source link

Maps deinit logic for complex key/value types #8

Closed Arwalk closed 11 months ago

Arwalk commented 2 years ago

Complex map keys/value deinit are not an easy feat to manage. If a key/value type requires a deinit call to be properly freed, it is currently not managed in any way.

Arwalk commented 2 years ago

Probably can use hasDecl or something like that to know if a deinit "method" exists.

Arwalk commented 2 years ago

This example works, so all our problems are solved.

const Demo2 = struct {
    a : ?u32,
    b : ?u32,

    pub const _desc_table = .{
        .a = fd(1, .{.Varint = .ZigZagOptimized}),
        .b = fd(2, .{.Varint = .ZigZagOptimized}),
    };

    pub fn encode(self: Demo2, allocator: *mem.Allocator) ![]u8 {
        return pb_encode(self, allocator);
    }

    pub fn deinit(self: Demo2) void {
        pb_deinit(self);
    }
};

test "hasdecl" {
    try testing.expect(@hasDecl(Demo2, "deinit"));
}