How service code generation works with the VTable pattern
masterzig-protobuf generates service interfaces using a VTable (virtual table) pattern. Instead of a single fixed implementation, the generator produces a function that takes two comptime parameters: UserDataType (your server's context/state) and ErrorSet (the errors your service can return).
This function returns a struct type containing function pointers for each RPC method defined in your .proto file. This approach provides type safety for your custom state and errors while ensuring zero runtime overhead because polymorphism is resolved at compile time.
Note: This only generates the service interface. You must implement your own transport layer (e.g., gRPC over HTTP/2) and server logic.
// The generated function signature pattern
pub fn MyService(comptime UserDataType: type, comptime ErrorSet: type) type {
return struct {
pub const service_name = "MyService";
// Example method signature
UnaryCall: *const fn(userdata: *UserDataType, request: Request) ErrorSet!Response,
};
}