To expand on the previous answer:
pub const PacketField = struct {
name: []const u8,
real_type: type,
repr_type: ReprType,
};
pub fn Packet(comptime fields: []const PacketSpect) type {
return @Struct(.{});
}
// Decouple implementation from the data
pub fn serializer(ptr: anytype) PacketSerializer(@TypeOf(ptr)) {
return .{ .ptr = ptr };
}
fn PacketSerializer(comptime Ptr: type) type {
return struct {
ptr: Ptr,
const P = @TypeInfo(Ptr).pointer.child;
pub fn serialize(self: @This(), writer: *std.Io.Writer) !void {
try writer.writeStruct(self.ptr.*, .little);
}
pub fn deserialize(self: @This(), reader: *std.Io.Reader) !void {
self.ptr.* = try reader.takeStruct(P, .little);
}
};
}
// ...
pub const Intention = Packet(intention_fields);
pub fn processIntentions(socket: *std.Io.Writer, intentions: []const Intention) !void {
for (intentions) |*intention| intention.version = PROTO_VERSION;
for (intentions) |*intention| serializer(intention).serialize(socket);
}
Regarding system design advice - I too used to be like “I want to have a method”, and then fight the compiler and the universe. That is a valid approach, but it has trade-offs. That will reflect onto your library. In real projects. You can try to make a wrapper type that builds on this foundation, but you can hardly detach a tightly coupled implementation to a data type. Zig is very deliberate and well designed in what and how it allows you to do things. Even this wrapper thing is one of multiple approaches (e.g. free functions, …), and honestly you might find that creating a custom reader/writer interface for your library and not supplying a serialization method might prove as a better call (might eliminate the need to have one in the first place. Another, in my opinion more elegant is to have the @Struct embedded inside the non-dump type, and accept that in order to get the fields one would go through intermediary field access:
pub const PacketField = struct {
name: []const u8,
real_type: type,
repr_type: ReprType,
};
pub fn Packet(comptime fields: []const PacketSpect) type {
return struct {
data: Data,
const Data = @Struct(.{});
pub fn serialize(self: *const @This(), writer: *std.Io.Writer) !void {
try writer.writeStruct(self.data, .little);
}
pub fn deserialize(self: *@This(), reader: *std.Io.Reader) !void {
self.data = try reader.takeStruct(Data, .little);
}
};
}
// ...
pub const Intention = Packet(intention_fields);
pub fn processIntentions(socket: *std.Io.Writer, intentions: []Intention) !void {
for (intentions) |*intention| intention.data.version = PROTO_VERSION;
for (intentions) |*intention|intention.serialize(socket);
}
In the end what you should focus on is that your library is maintainable, flexible, performant and safe, not spend days to get it to having a “cool way” to do a thing just because you stubbornly refuse to relax your syntactic requirements (e.g.I WILL NEVER ACCEPT HAVING TO TYPE intention.data.version INSTEAD OF intention.version, IF I CAN’T ALSO TYPE intention.serialize() instead of socket.writeStruct(intention)!!!). These types of requirements are added complexity into a system that should pour all its complexity into solving the actual core of the problems, e.g. how you organize and transform the data. Having a “single struct serialize” function reeks of object oriented design and will likely make you write code that will operate on single intention instead of what is the more common case, e.g. I have a bunch of these things and I should serialize a bunch of them at once to help the CPU and memory work most efficiently. Coming from a guy who spend years trying to do things like that, and years before that C++ template metaprogramming pointless overengineered bs that could have just been free functions, or generated in some other manner; which I will never get back, and in the end made the experience worse.
Edit:
Fixed code in first example.