Skip to content

Commit

Permalink
Update to Allocgate + anyopaque changes
Browse files Browse the repository at this point in the history
Some tests are still failing though
  • Loading branch information
joachimschmidt557 committed Jan 4, 2022
1 parent 1a929a4 commit 55e2c8f
Show file tree
Hide file tree
Showing 17 changed files with 36 additions and 36 deletions.
6 changes: 3 additions & 3 deletions src/client.zig
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ pub fn RedisClient(buffering: Buffering, _: Logging) type {
}

/// Like `send`, can allocate memory.
pub fn sendAlloc(self: *Self, comptime T: type, allocator: *Allocator, cmd: anytype) !T {
pub fn sendAlloc(self: *Self, comptime T: type, allocator: Allocator, cmd: anytype) !T {
return self.pipelineImpl(T, cmd, .{ .one = {}, .ptr = allocator });
}

Expand All @@ -105,7 +105,7 @@ pub fn RedisClient(buffering: Buffering, _: Logging) type {
}

/// Like `trans`, but can allocate memory.
pub fn transAlloc(self: *Self, comptime Ts: type, allocator: *Allocator, cmds: anytype) !Ts {
pub fn transAlloc(self: *Self, comptime Ts: type, allocator: Allocator, cmds: anytype) !Ts {
return transactionImpl(self, Ts, cmds, .{ .ptr = allocator });
}

Expand All @@ -128,7 +128,7 @@ pub fn RedisClient(buffering: Buffering, _: Logging) type {
}

/// Like `pipe`, but can allocate memory.
pub fn pipeAlloc(self: *Self, comptime Ts: type, allocator: *Allocator, cmds: anytype) !Ts {
pub fn pipeAlloc(self: *Self, comptime Ts: type, allocator: Allocator, cmds: anytype) !Ts {
return pipelineImpl(self, Ts, cmds, .{ .ptr = allocator });
}

Expand Down
6 changes: 3 additions & 3 deletions src/parser.zig
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ pub const RESP3Parser = struct {
return parseImpl(T, tag, .{}, msg);
}

pub fn parseAlloc(comptime T: type, allocator: *Allocator, msg: anytype) !T {
pub fn parseAlloc(comptime T: type, allocator: Allocator, msg: anytype) !T {
const tag = try msg.readByte();
return try parseImpl(T, tag, .{ .ptr = allocator }, msg);
}

pub fn parseAllocFromTag(comptime T: type, tag: u8, allocator: *Allocator, msg: anytype) !T {
pub fn parseAllocFromTag(comptime T: type, tag: u8, allocator: Allocator, msg: anytype) !T {
return parseImpl(T, tag, .{ .ptr = allocator }, msg);
}

Expand Down Expand Up @@ -203,7 +203,7 @@ pub const RESP3Parser = struct {
// Frees values created by `sendAlloc`.
// If the top value is a pointer, it frees that too.
// TODO: free stdlib types!
pub fn freeReply(val: anytype, allocator: *Allocator) void {
pub fn freeReply(val: anytype, allocator: Allocator) void {
const T = @TypeOf(val);

switch (@typeInfo(T)) {
Expand Down
2 changes: 1 addition & 1 deletion src/parser/t_bignum.zig
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub const BigNumParser = struct {
return T == std.math.big.int.Managed or T == []u8;
}

pub fn parseAlloc(comptime T: type, comptime _: type, allocator: *std.mem.Allocator, msg: anytype) !T {
pub fn parseAlloc(comptime T: type, comptime _: type, allocator: std.mem.Allocator, msg: anytype) !T {
// TODO: find a better max_size limit than a random 1k value
const bigSlice = try msg.readUntilDelimiterAlloc(allocator, '\r', 1000);
errdefer allocator.free(bigSlice);
Expand Down
4 changes: 2 additions & 2 deletions src/parser/t_bool.zig
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ pub const BoolParser = struct {
return isSupported(T);
}

pub fn parseAlloc(comptime T: type, comptime rootParser: type, allocator: *std.mem.Allocator, msg: anytype) !T {
pub fn parseAlloc(comptime T: type, comptime rootParser: type, allocator: std.mem.Allocator, msg: anytype) !T {
_ = allocator;

return parse(T, rootParser, msg);
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/parser/t_double.zig
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub const DoubleParser = struct {
return isSupported(T);
}

pub fn parseAlloc(comptime T: type, comptime rootParser: type, allocator: *std.mem.Allocator, msg: anytype) !T {
pub fn parseAlloc(comptime T: type, comptime rootParser: type, allocator: std.mem.Allocator, msg: anytype) !T {
_ = allocator;
return parse(T, rootParser, msg);
}
Expand Down
2 changes: 1 addition & 1 deletion src/parser/t_list.zig
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub const ListParser = struct {
pub fn parse(comptime T: type, comptime rootParser: type, msg: anytype) !T {
return parseImpl(T, rootParser, .{}, msg);
}
pub fn parseAlloc(comptime T: type, comptime rootParser: type, allocator: *std.mem.Allocator, msg: anytype) !T {
pub fn parseAlloc(comptime T: type, comptime rootParser: type, allocator: std.mem.Allocator, msg: anytype) !T {
return parseImpl(T, rootParser, .{ .ptr = allocator }, msg);
}

Expand Down
2 changes: 1 addition & 1 deletion src/parser/t_map.zig
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub const MapParser = struct {
pub fn parse(comptime T: type, comptime rootParser: type, msg: anytype) !T {
return parseImpl(T, rootParser, .{}, msg);
}
pub fn parseAlloc(comptime T: type, comptime rootParser: type, allocator: *std.mem.Allocator, msg: anytype) !T {
pub fn parseAlloc(comptime T: type, comptime rootParser: type, allocator: std.mem.Allocator, msg: anytype) !T {
return parseImpl(T, rootParser, .{ .ptr = allocator }, msg);
}

Expand Down
2 changes: 1 addition & 1 deletion src/parser/t_number.zig
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub const NumberParser = struct {
return isSupported(T);
}

pub fn parseAlloc(comptime T: type, comptime rootParser: type, _: *std.mem.Allocator, msg: anytype) !T {
pub fn parseAlloc(comptime T: type, comptime rootParser: type, _: std.mem.Allocator, msg: anytype) !T {
return parse(T, rootParser, msg); // TODO: before I passed down an empty struct type. Was I insane? Did I have a plan?
}
};
2 changes: 1 addition & 1 deletion src/parser/t_set.zig
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub const SetParser = struct {
return parseImpl(T, rootParser, .{}, msg);
}

pub fn parseAlloc(comptime T: type, comptime rootParser: type, allocator: *std.mem.Allocator, msg: anytype) !T {
pub fn parseAlloc(comptime T: type, comptime rootParser: type, allocator: std.mem.Allocator, msg: anytype) !T {
// HASHMAP
if (@typeInfo(T) == .Struct and @hasDecl(T, "Entry")) {
const isManaged = @typeInfo(@TypeOf(T.deinit)).Fn.args.len == 1;
Expand Down
2 changes: 1 addition & 1 deletion src/parser/t_string_blob.zig
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ pub const BlobStringParser = struct {
};
}

pub fn parseAlloc(comptime T: type, comptime _: type, allocator: *std.mem.Allocator, msg: anytype) !T {
pub fn parseAlloc(comptime T: type, comptime _: type, allocator: std.mem.Allocator, msg: anytype) !T {
// @compileLog(@typeInfo(T));
// std.debug.print("\n\nTYPE={}\n\n", .{@typeInfo(T)});
switch (@typeInfo(T)) {
Expand Down
2 changes: 1 addition & 1 deletion src/parser/t_string_simple.zig
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ pub const SimpleStringParser = struct {
};
}

pub fn parseAlloc(comptime T: type, comptime _: type, allocator: *std.mem.Allocator, msg: anytype) !T {
pub fn parseAlloc(comptime T: type, comptime _: type, allocator: std.mem.Allocator, msg: anytype) !T {
switch (@typeInfo(T)) {
.Pointer => |ptr| {
switch (ptr.size) {
Expand Down
8 changes: 4 additions & 4 deletions src/traits.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
/// It's expected to implement three functions:
/// ```
/// fn parse(tag: u8, comptime rootParser: type, msg: var) !Self
/// fn parseAlloc(tag: u8, comptime rootParser: type, allocator: *Allocator, msg: var) !Self
/// fn destroy(self: Self, comptime rootParser: type, allocator: *Allocator) void
/// fn parseAlloc(tag: u8, comptime rootParser: type, allocator: Allocator, msg: var) !Self
/// fn destroy(self: Self, comptime rootParser: type, allocator: Allocator) void
/// ```
/// `rootParser` is a reference to the RESP3Parser, which contains the main
/// parsing logic. It's passed to the type in order to allow it to recursively
Expand All @@ -30,14 +30,14 @@ pub fn isParserType(comptime T: type) bool {
if (!@hasDecl(T.Redis.Parser, "parseAlloc"))
@compileError(
\\`Redis.Parser` trait requires implementing:
\\ fn parseAlloc(tag: u8, comptime rootParser: type, allocator: *Allocator, msg: var) !Self
\\ fn parseAlloc(tag: u8, comptime rootParser: type, allocator: Allocator, msg: var) !Self
\\
);

if (!@hasDecl(T.Redis.Parser, "destroy"))
@compileError(
\\`Redis.Parser` trait requires implementing:
\\ fn destroy(self: *Self, comptime rootParser: type, allocator: *Allocator) void
\\ fn destroy(self: *Self, comptime rootParser: type, allocator: Allocator) void
\\
);

Expand Down
8 changes: 4 additions & 4 deletions src/types/attributes.zig
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ pub fn WithAttribs(comptime T: type) type {
@compileError("WithAttribs requires an allocator. Use `sendAlloc`.");
}

pub fn destroy(self: Self, comptime rootParser: type, allocator: *Allocator) void {
pub fn destroy(self: Self, comptime rootParser: type, allocator: Allocator) void {
rootParser.freeReply(self.attribs, allocator);
rootParser.freeReply(self.data, allocator);
}

pub fn parseAlloc(tag: u8, comptime rootParser: type, allocator: *Allocator, msg: anytype) !Self {
pub fn parseAlloc(tag: u8, comptime rootParser: type, allocator: Allocator, msg: anytype) !Self {
var itemTag = tag;

var res: Self = undefined;
Expand Down Expand Up @@ -101,12 +101,12 @@ fn MakeComplexListWithAttributes() std.io.FixedBufferStream([]const u8) {
":20\r\n" ++
"|1\r\n" ++
"+ttl\r\n" ++
":128\r\n" ++
":128\r\n" ++
":100\r\n" ++
"*2\r\n" ++
"|1\r\n" ++
"+Banana\r\n" ++
"#t\r\n" ++
"#t\r\n" ++
":123\r\n" ++
":99\r\n"
)[0..]);
Expand Down
10 changes: 5 additions & 5 deletions src/types/error.zig
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ pub fn OrErr(comptime T: type) type {
};
}

pub fn destroy(self: Self, comptime rootParser: type, allocator: *Allocator) void {
pub fn destroy(self: Self, comptime rootParser: type, allocator: Allocator) void {
switch (self) {
.Ok => |ok| rootParser.freeReply(ok, allocator),
else => {},
}
}

pub fn parseAlloc(tag: u8, comptime rootParser: type, allocator: *Allocator, msg: anytype) !Self {
pub fn parseAlloc(tag: u8, comptime rootParser: type, allocator: Allocator, msg: anytype) !Self {
return switch (tag) {
'_', '-', '!' => internalParse(tag, rootParser, msg),
else => return Self{ .Ok = try rootParser.parseAllocFromTag(T, tag, allocator, msg) },
Expand Down Expand Up @@ -174,15 +174,15 @@ pub fn OrFullErr(comptime T: type) type {
@compileError("OrFullErr requires an allocator, use `OrErr` to parse just the error code without the need of an allocator.");
}

pub fn destroy(self: Self, comptime rootParser: type, allocator: *Allocator) void {
pub fn destroy(self: Self, comptime rootParser: type, allocator: Allocator) void {
switch (self) {
.Ok => |ok| rootParser.freeReply(ok, allocator),
.Err => |err| allocator.free(err.message),
.Nil => {},
}
}

pub fn parseAlloc(tag: u8, comptime rootParser: type, allocator: *Allocator, msg: anytype) !Self {
pub fn parseAlloc(tag: u8, comptime rootParser: type, allocator: Allocator, msg: anytype) !Self {
switch (tag) {
else => return Self{ .Ok = try rootParser.parseAllocFromTag(T, tag, allocator, msg) },
'_' => {
Expand Down Expand Up @@ -337,7 +337,7 @@ const fakeParser = struct {
_ = rootParser;
return error.Errror;
}
pub inline fn parseAllocFromTag(comptime T: type, tag: u8, allocator: *Allocator, rootParser: anytype) !T {
pub inline fn parseAllocFromTag(comptime T: type, tag: u8, allocator: Allocator, rootParser: anytype) !T {
_ = T;
_ = rootParser;
_ = tag;
Expand Down
2 changes: 1 addition & 1 deletion src/types/fixbuf.zig
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ pub fn FixBuf(comptime size: usize) type {
}
}

pub fn destroy(_: Self, comptime _: type, _: *std.mem.Allocator) void {}
pub fn destroy(_: Self, comptime _: type, _: std.mem.Allocator) void {}

pub fn parseAlloc(tag: u8, comptime rootParser: type, msg: anytype) !Self {
return parse(tag, rootParser, msg);
Expand Down
8 changes: 4 additions & 4 deletions src/types/reply.zig
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub const DynamicReply = struct {
@compileError("DynamicReply requires an allocator. Use `sendAlloc`!");
}

pub fn destroy(self: DynamicReply, comptime rootParser: type, allocator: *Allocator) void {
pub fn destroy(self: DynamicReply, comptime rootParser: type, allocator: Allocator) void {
rootParser.freeReply(self.attribs, allocator);
switch (self.data) {
.Nil, .Bool, .Number, .Double => {},
Expand All @@ -56,7 +56,7 @@ pub const DynamicReply = struct {
}
}

pub fn parseAlloc(tag: u8, comptime rootParser: type, allocator: *Allocator, msg: anytype) error{DynamicReplyError}!DynamicReply {
pub fn parseAlloc(tag: u8, comptime rootParser: type, allocator: Allocator, msg: anytype) error{DynamicReplyError}!DynamicReply {
var itemTag = tag;

var res: DynamicReply = undefined;
Expand Down Expand Up @@ -167,12 +167,12 @@ fn MakeComplexListWithAttributes() std.io.FixedBufferStream([]const u8) {
"+Hello\r\n" ++
"|1\r\n" ++
"+ttl\r\n" ++
":100\r\n" ++
":100\r\n" ++
"#t\r\n" ++
"*3\r\n" ++
"|1\r\n" ++
"+Banana\r\n" ++
"#t\r\n" ++
"#t\r\n" ++
":123\r\n" ++
"(424242\r\n" ++
",12.34\r\n")
Expand Down
4 changes: 2 additions & 2 deletions src/types/verbatim.zig
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ pub const Verbatim = struct {
@compileError("Verbatim requires an allocator, use `parseAlloc`.");
}

pub fn destroy(self: Verbatim, comptime _: type, allocator: *Allocator) void {
pub fn destroy(self: Verbatim, comptime _: type, allocator: Allocator) void {
allocator.free(self.string);
}

pub fn parseAlloc(tag: u8, comptime rootParser: type, allocator: *Allocator, msg: anytype) !Verbatim {
pub fn parseAlloc(tag: u8, comptime rootParser: type, allocator: Allocator, msg: anytype) !Verbatim {
switch (tag) {
else => return error.DecodingError,
'-', '!' => {
Expand Down

0 comments on commit 55e2c8f

Please sign in to comment.