Skip to content

Commit

Permalink
Add type assertions for bool/int/float serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
lalinsky committed Nov 16, 2024
1 parent df0b7da commit 6314094
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 17 deletions.
12 changes: 12 additions & 0 deletions lib/msgpack/bool.zig
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,25 @@ pub fn getBoolSize() usize {
return 1;
}

inline fn assertBoolType(comptime T: type) void {
switch (@typeInfo(T)) {
.Bool => return,
.Optional => |opt_info| {
assertBoolType(opt_info.child);
},
else => @compileError("Expected bool, got " ++ @typeName(T)),
}
}

pub fn packBool(writer: anytype, comptime T: type, value_or_maybe_null: T) !void {
assertBoolType(T);
const value = try maybePackNull(writer, T, value_or_maybe_null) orelse return;

try writer.writeByte(if (value) c.MSG_TRUE else c.MSG_FALSE);
}

pub fn unpackBool(reader: anytype, comptime T: type) !T {
assertBoolType(T);
const header = try reader.readByte();
switch (header) {
c.MSG_TRUE => return true,
Expand Down
27 changes: 20 additions & 7 deletions lib/msgpack/float.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,33 @@ const NonOptional = @import("utils.zig").NonOptional;
const maybePackNull = @import("null.zig").maybePackNull;
const maybeUnpackNull = @import("null.zig").maybeUnpackNull;

inline fn assertFloatType(comptime T: type) type {
switch (@typeInfo(T)) {
.Float => return T,
.Optional => |opt_info| {
return assertFloatType(opt_info.child);
},
else => @compileError("Expected float, got " ++ @typeName(T)),
}
}

pub fn getMaxFloatSize(comptime T: type) usize {
return 1 + @sizeOf(T);
const Type = assertFloatType(T);
return 1 + @sizeOf(Type);
}

pub fn getFloatSize(comptime T: type, value: T) usize {
const Type = assertFloatType(T);
_ = value;
return getMaxFloatSize(T);
return getMaxFloatSize(Type);
}

pub fn packFloat(writer: anytype, comptime T: type, value_or_maybe_null: T) !void {
const value = try maybePackNull(writer, T, value_or_maybe_null) orelse return;
const Type = @TypeOf(value);
const type_info = @typeInfo(Type);
const Type = assertFloatType(T);
const value: Type = try maybePackNull(writer, T, value_or_maybe_null) orelse return;

comptime var TargetType: type = undefined;
const type_info = @typeInfo(Type);
switch (type_info.Float.bits) {
0...32 => {
try writer.writeByte(c.MSG_FLOAT32);
Expand Down Expand Up @@ -55,10 +67,11 @@ pub fn readFloatValue(reader: anytype, comptime SourceFloat: type, comptime Targ
}

pub fn unpackFloat(reader: anytype, comptime T: type) !T {
const Type = assertFloatType(T);
const header = try reader.readByte();
switch (header) {
c.MSG_FLOAT32 => return try readFloatValue(reader, f32, NonOptional(T)),
c.MSG_FLOAT64 => return try readFloatValue(reader, f64, NonOptional(T)),
c.MSG_FLOAT32 => return try readFloatValue(reader, f32, Type),
c.MSG_FLOAT64 => return try readFloatValue(reader, f64, Type),
else => return maybeUnpackNull(header, T),
}
}
Expand Down
24 changes: 18 additions & 6 deletions lib/msgpack/int.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,24 @@ const NonOptional = @import("utils.zig").NonOptional;
const maybePackNull = @import("null.zig").maybePackNull;
const maybeUnpackNull = @import("null.zig").maybeUnpackNull;

inline fn assertIntType(comptime T: type) type {
switch (@typeInfo(T)) {
.Int => return T,
.Optional => |opt_info| {
return assertIntType(opt_info.child);
},
else => @compileError("Expected int, got " ++ @typeName(T)),
}
}

pub fn getMaxIntSize(comptime T: type) usize {
return 1 + @sizeOf(T);
const Type = assertIntType(T);
return 1 + @sizeOf(Type);
}

pub fn getIntSize(comptime T: type, value: T) usize {
const type_info = @typeInfo(T);
const Type = assertIntType(T);
const type_info = @typeInfo(Type);

const is_signed = type_info.Int.signedness == .signed;
const bits = type_info.Int.bits;
Expand Down Expand Up @@ -61,10 +73,10 @@ pub fn packIntValue(writer: anytype, comptime T: type, value: T) !void {
}

pub fn packInt(writer: anytype, comptime T: type, value_or_maybe_null: T) !void {
const value = try maybePackNull(writer, T, value_or_maybe_null) orelse return;
const Type = @TypeOf(value);
const type_info = @typeInfo(Type);
const Type = assertIntType(T);
const value: Type = try maybePackNull(writer, T, value_or_maybe_null) orelse return;

const type_info = @typeInfo(Type);
const is_signed = type_info.Int.signedness == .signed;
const bits = type_info.Int.bits;

Expand Down Expand Up @@ -170,7 +182,7 @@ pub fn unpackIntValue(reader: anytype, comptime SourceType: type, comptime Targe
}

pub fn unpackInt(reader: anytype, comptime T: type) !T {
const Type = NonOptional(T);
const Type = assertIntType(T);
const type_info = @typeInfo(Type);

const header = try reader.readByte();
Expand Down
8 changes: 4 additions & 4 deletions lib/msgpack/msgpack.zig
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ pub fn Unpacker(comptime Reader: type) type {
};
}

pub fn readNil(self: Self) !void {
pub fn readNull(self: Self) !void {
try unpackNull(self.reader);
}

Expand Down Expand Up @@ -241,8 +241,8 @@ pub fn packer(writer: anytype) Packer(@TypeOf(writer)) {
return Packer(@TypeOf(writer)).init(writer);
}

pub fn unpacker(reader: anytype, allocator: Allocator) Unpacker(@TypeOf(reader)) {
return Unpacker(@TypeOf(reader)).init(reader, allocator);
pub fn unpacker(reader: anytype, allocator: ?Allocator) Unpacker(@TypeOf(reader)) {
return Unpacker(@TypeOf(reader)).init(reader, allocator orelse NoAllocator.allocator());
}

pub fn unpackerNoAlloc(reader: anytype) Unpacker(@TypeOf(reader)) {
Expand All @@ -254,7 +254,7 @@ pub fn encode(comptime T: type, writer: anytype, value: anytype) !void {
}

pub fn decode(comptime T: type, reader: anytype, allocator: ?Allocator) !T {
return try unpacker(reader, allocator orelse NoAllocator.allocator()).read(T);
return try unpacker(reader, allocator).read(T);
}

test {
Expand Down

0 comments on commit 6314094

Please sign in to comment.