Skip to content

Commit 5553699

Browse files
committed
refactor(msgpack): simplify integer read/write methods
- introduce generic methods for integer value reading and writing - reduce code duplication in integer handling functions - improve type safety and readability for integer operations - standardize integer read/write patterns across different integer types
1 parent 7b01763 commit 5553699

File tree

1 file changed

+51
-33
lines changed

1 file changed

+51
-33
lines changed

src/msgpack.zig

Lines changed: 51 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,21 @@ pub fn Pack(
608608
try self.writeData(data);
609609
}
610610

611+
/// Generic integer value write (without marker)
612+
inline fn writeIntValue(self: Self, comptime T: type, val: T) !void {
613+
if (T == u8 or T == i8) {
614+
try self.writeByte(@bitCast(val));
615+
} else {
616+
try self.writeIntRaw(T, val);
617+
}
618+
}
619+
620+
/// Generic integer write with marker
621+
inline fn writeIntWithMarker(self: Self, comptime T: type, marker: Markers, val: T) !void {
622+
try self.writeTypeMarker(marker);
623+
try self.writeIntValue(T, val);
624+
}
625+
611626
/// write type marker
612627
inline fn writeTypeMarker(self: Self, comptime marker: Markers) !void {
613628
switch (marker) {
@@ -644,43 +659,39 @@ pub fn Pack(
644659
}
645660

646661
inline fn writeU8Value(self: Self, val: u8) !void {
647-
try self.writeByte(val);
662+
try self.writeIntValue(u8, val);
648663
}
649664

650665
/// write u8 int
651666
fn writeU8(self: Self, val: u8) !void {
652-
try self.writeTypeMarker(.UINT8);
653-
try self.writeU8Value(val);
667+
try self.writeIntWithMarker(u8, .UINT8, val);
654668
}
655669

656670
inline fn writeU16Value(self: Self, val: u16) !void {
657-
try self.writeIntRaw(u16, val);
671+
try self.writeIntValue(u16, val);
658672
}
659673

660674
/// write u16 int
661675
fn writeU16(self: Self, val: u16) !void {
662-
try self.writeTypeMarker(.UINT16);
663-
try self.writeU16Value(val);
676+
try self.writeIntWithMarker(u16, .UINT16, val);
664677
}
665678

666679
inline fn writeU32Value(self: Self, val: u32) !void {
667-
try self.writeIntRaw(u32, val);
680+
try self.writeIntValue(u32, val);
668681
}
669682

670683
/// write u32 int
671684
fn writeU32(self: Self, val: u32) !void {
672-
try self.writeTypeMarker(.UINT32);
673-
try self.writeU32Value(val);
685+
try self.writeIntWithMarker(u32, .UINT32, val);
674686
}
675687

676688
inline fn writeU64Value(self: Self, val: u64) !void {
677-
try self.writeIntRaw(u64, val);
689+
try self.writeIntValue(u64, val);
678690
}
679691

680692
/// write u64 int
681693
fn writeU64(self: Self, val: u64) !void {
682-
try self.writeTypeMarker(.UINT64);
683-
try self.writeU64Value(val);
694+
try self.writeIntWithMarker(u64, .UINT64, val);
684695
}
685696

686697
/// write negative fix int
@@ -693,43 +704,39 @@ pub fn Pack(
693704
}
694705

695706
inline fn writeI8Value(self: Self, val: i8) !void {
696-
try self.writeByte(@bitCast(val));
707+
try self.writeIntValue(i8, val);
697708
}
698709

699710
/// write i8 int
700711
fn writeI8(self: Self, val: i8) !void {
701-
try self.writeTypeMarker(.INT8);
702-
try self.writeI8Value(val);
712+
try self.writeIntWithMarker(i8, .INT8, val);
703713
}
704714

705715
inline fn writeI16Value(self: Self, val: i16) !void {
706-
try self.writeIntRaw(i16, val);
716+
try self.writeIntValue(i16, val);
707717
}
708718

709719
/// write i16 int
710720
fn writeI16(self: Self, val: i16) !void {
711-
try self.writeTypeMarker(.INT16);
712-
try self.writeI16Value(val);
721+
try self.writeIntWithMarker(i16, .INT16, val);
713722
}
714723

715724
inline fn writeI32Value(self: Self, val: i32) !void {
716-
try self.writeIntRaw(i32, val);
725+
try self.writeIntValue(i32, val);
717726
}
718727

719728
/// write i32 int
720729
fn writeI32(self: Self, val: i32) !void {
721-
try self.writeTypeMarker(.INT32);
722-
try self.writeI32Value(val);
730+
try self.writeIntWithMarker(i32, .INT32, val);
723731
}
724732

725733
inline fn writeI64Value(self: Self, val: i64) !void {
726-
try self.writeIntRaw(i64, val);
734+
try self.writeIntValue(i64, val);
727735
}
728736

729737
/// write i64 int
730738
fn writeI64(self: Self, val: i64) !void {
731-
try self.writeTypeMarker(.INT64);
732-
try self.writeI64Value(val);
739+
try self.writeIntWithMarker(i64, .INT64, val);
733740
}
734741

735742
/// write uint
@@ -1169,6 +1176,18 @@ pub fn Pack(
11691176
return std.mem.readInt(T, &buffer, big_endian);
11701177
}
11711178

1179+
/// Generic integer value read
1180+
inline fn readTypedInt(self: Self, comptime T: type) !T {
1181+
if (T == u8) {
1182+
return self.readByte();
1183+
} else if (T == i8) {
1184+
const val = try self.readByte();
1185+
return @bitCast(val);
1186+
} else {
1187+
return self.readIntRaw(T);
1188+
}
1189+
}
1190+
11721191
fn readTypeMarkerU8(self: Self) !u8 {
11731192
const val = try self.readByte();
11741193
return val;
@@ -1239,36 +1258,35 @@ pub fn Pack(
12391258
}
12401259

12411260
inline fn readI8Value(self: Self) !i8 {
1242-
const val = try self.readByte();
1243-
return @bitCast(val);
1261+
return self.readTypedInt(i8);
12441262
}
12451263

12461264
inline fn readV8Value(self: Self) !u8 {
1247-
return self.readByte();
1265+
return self.readTypedInt(u8);
12481266
}
12491267

12501268
inline fn readI16Value(self: Self) !i16 {
1251-
return self.readIntRaw(i16);
1269+
return self.readTypedInt(i16);
12521270
}
12531271

12541272
inline fn readU16Value(self: Self) !u16 {
1255-
return self.readIntRaw(u16);
1273+
return self.readTypedInt(u16);
12561274
}
12571275

12581276
inline fn readI32Value(self: Self) !i32 {
1259-
return self.readIntRaw(i32);
1277+
return self.readTypedInt(i32);
12601278
}
12611279

12621280
inline fn readU32Value(self: Self) !u32 {
1263-
return self.readIntRaw(u32);
1281+
return self.readTypedInt(u32);
12641282
}
12651283

12661284
inline fn readI64Value(self: Self) !i64 {
1267-
return self.readIntRaw(i64);
1285+
return self.readTypedInt(i64);
12681286
}
12691287

12701288
inline fn readU64Value(self: Self) !u64 {
1271-
return self.readIntRaw(u64);
1289+
return self.readTypedInt(u64);
12721290
}
12731291

12741292
fn readIntValue(self: Self, marker_u8: u8) !i64 {

0 commit comments

Comments
 (0)