Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 25 additions & 25 deletions src/any.zig
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ const unpackUnion = @import("union.zig").unpackUnion;

inline fn isString(comptime T: type) bool {
switch (@typeInfo(T)) {
.Pointer => |ptr_info| {
.pointer => |ptr_info| {
if (ptr_info.size == .Slice) {
if (ptr_info.child == u8) {
return true;
}
}
},
.Optional => |opt_info| {
.optional => |opt_info| {
return isString(opt_info.child);
},
else => {},
Expand All @@ -53,10 +53,10 @@ inline fn isString(comptime T: type) bool {

pub fn sizeOfPackedAny(comptime T: type, value: T) usize {
switch (@typeInfo(NonOptional(T))) {
.Bool => return getBoolSize(),
.Int => return getIntSize(T, value),
.Float => return getFloatSize(T, value),
.Pointer => |ptr_info| {
.bool => return getBoolSize(),
.int => return getIntSize(T, value),
.float => return getFloatSize(T, value),
.pointer => |ptr_info| {
if (ptr_info.size == .Slice) {
if (isString(T)) {
return sizeOfPackedString(value.len);
Expand All @@ -73,13 +73,13 @@ pub fn sizeOfPackedAny(comptime T: type, value: T) usize {
pub fn packAny(writer: anytype, value: anytype) !void {
const T = @TypeOf(value);
switch (@typeInfo(T)) {
.Void => return packNull(writer),
.Bool => return packBool(writer, value),
.Int => return packInt(writer, T, value),
.Float => return packFloat(writer, T, value),
.ComptimeInt => return packInt(writer, i64, @intCast(value)),
.ComptimeFloat => return packFloat(writer, f64, @floatCast(value)),
.Array => |arr_info| {
.void => return packNull(writer),
.bool => return packBool(writer, value),
.int => return packInt(writer, T, value),
.float => return packFloat(writer, T, value),
.comptime_int => return packInt(writer, i64, @intCast(value)),
.comptime_float => return packFloat(writer, f64, @floatCast(value)),
.array => |arr_info| {
switch (arr_info.child) {
u8 => {
return packString(writer, &value);
Expand All @@ -89,7 +89,7 @@ pub fn packAny(writer: anytype, value: anytype) !void {
},
}
},
.Pointer => |ptr_info| {
.pointer => |ptr_info| {
if (ptr_info.size == .Slice) {
switch (ptr_info.child) {
u8 => {
Expand All @@ -103,9 +103,9 @@ pub fn packAny(writer: anytype, value: anytype) !void {
return packAny(writer, value.*);
}
},
.Struct => return packStruct(writer, T, value),
.Union => return packUnion(writer, T, value),
.Optional => {
.@"struct" => return packStruct(writer, T, value),
.@"union" => return packUnion(writer, T, value),
.optional => {
if (value) |val| {
return packAny(writer, val);
} else {
Expand All @@ -119,13 +119,13 @@ pub fn packAny(writer: anytype, value: anytype) !void {

pub fn unpackAny(reader: anytype, allocator: std.mem.Allocator, comptime T: type) !T {
switch (@typeInfo(T)) {
.Void => return unpackNull(reader),
.Bool => return unpackBool(reader, T),
.Int => return unpackInt(reader, T),
.Float => return unpackFloat(reader, T),
.Struct => return unpackStruct(reader, allocator, T),
.Union => return unpackUnion(reader, allocator, T),
.Pointer => |ptr_info| {
.void => return unpackNull(reader),
.bool => return unpackBool(reader, T),
.int => return unpackInt(reader, T),
.float => return unpackFloat(reader, T),
.@"struct" => return unpackStruct(reader, allocator, T),
.@"union" => return unpackUnion(reader, allocator, T),
.pointer => |ptr_info| {
if (ptr_info.size == .Slice) {
if (isString(T)) {
return unpackString(reader, allocator);
Expand All @@ -134,7 +134,7 @@ pub fn unpackAny(reader: anytype, allocator: std.mem.Allocator, comptime T: type
}
}
},
.Optional => |opt_info| {
.optional => |opt_info| {
return unpackAny(reader, allocator, opt_info.child) catch |err| {
if (isNullError(err)) {
return null;
Expand Down
6 changes: 3 additions & 3 deletions src/bool.zig
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub fn getBoolSize() usize {

inline fn forceBoolType(value: anytype) type {
const T = @TypeOf(value);
if (@typeInfo(T) == .Null) {
if (@typeInfo(T) == .null) {
return ?bool;
}
assertBoolType(T);
Expand All @@ -19,8 +19,8 @@ inline fn forceBoolType(value: anytype) type {

inline fn assertBoolType(T: type) void {
switch (@typeInfo(T)) {
.Bool => return,
.Optional => |opt_info| {
.bool => return,
.optional => |opt_info| {
return assertBoolType(opt_info.child);
},
else => @compileError("Expected bool, got " ++ @typeName(T)),
Expand Down
18 changes: 9 additions & 9 deletions src/float.zig
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ const maybeUnpackNull = @import("null.zig").maybeUnpackNull;

inline fn assertFloatType(comptime T: type) type {
switch (@typeInfo(T)) {
.Float => return T,
.Optional => |opt_info| {
.float => return T,
.optional => |opt_info| {
return assertFloatType(opt_info.child);
},
else => @compileError("Expected float, got " ++ @typeName(T)),
Expand All @@ -32,7 +32,7 @@ pub fn packFloat(writer: anytype, comptime T: type, value_or_maybe_null: T) !voi

comptime var TargetType: type = undefined;
const type_info = @typeInfo(Type);
switch (type_info.Float.bits) {
switch (type_info.float.bits) {
0...32 => {
try writer.writeByte(hdrs.FLOAT32);
TargetType = f32;
Expand Down Expand Up @@ -88,10 +88,10 @@ const packed_float64_inf = [_]u8{ 0xcb, 0x7f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00

const float_types = [_]type{ f16, f32, f64 };

fn minFloatType(comptime T1: type, comptime T2: type) type {
fn MinFloatType(comptime T1: type, comptime T2: type) type {
const ti1 = @typeInfo(T1);
const ti2 = @typeInfo(T2);
return std.meta.Float(@min(ti1.Float.bits, ti2.Float.bits));
return std.meta.Float(@min(ti1.float.bits, ti2.float.bits));
}

test "readFloat: null" {
Expand All @@ -105,31 +105,31 @@ test "readFloat: float32 (zero)" {
inline for (float_types) |T| {
var stream = std.io.fixedBufferStream(&packed_float32_zero);
const value = try unpackFloat(stream.reader(), T);
try std.testing.expectApproxEqAbs(0.0, value, std.math.floatEpsAt(minFloatType(T, f32), @floatCast(value)));
try std.testing.expectApproxEqAbs(0.0, value, std.math.floatEpsAt(MinFloatType(T, f32), @floatCast(value)));
}
}

test "readFloat: float64 (zero)" {
inline for (float_types) |T| {
var stream = std.io.fixedBufferStream(&packed_float64_zero);
const value = try unpackFloat(stream.reader(), T);
try std.testing.expectApproxEqAbs(0.0, value, std.math.floatEpsAt(minFloatType(T, f64), @floatCast(value)));
try std.testing.expectApproxEqAbs(0.0, value, std.math.floatEpsAt(MinFloatType(T, f64), @floatCast(value)));
}
}

test "readFloat: float32 (pi)" {
inline for (float_types) |T| {
var stream = std.io.fixedBufferStream(&packed_float32_pi);
const value = try unpackFloat(stream.reader(), T);
try std.testing.expectApproxEqAbs(std.math.pi, value, std.math.floatEpsAt(minFloatType(T, f32), @floatCast(value)));
try std.testing.expectApproxEqAbs(std.math.pi, value, std.math.floatEpsAt(MinFloatType(T, f32), @floatCast(value)));
}
}

test "readFloat: float64 (pi)" {
inline for (float_types) |T| {
var stream = std.io.fixedBufferStream(&packed_float64_pi);
const value = try unpackFloat(stream.reader(), T);
try std.testing.expectApproxEqAbs(std.math.pi, value, std.math.floatEpsAt(minFloatType(T, f64), @floatCast(value)));
try std.testing.expectApproxEqAbs(std.math.pi, value, std.math.floatEpsAt(MinFloatType(T, f64), @floatCast(value)));
}
}

Expand Down
Loading
Loading