Skip to content

Commit a2df7d7

Browse files
mstdokumacijinzhongjia
authored andcommitted
feat(Payload): add deepClone method for Payload struct
1 parent fe3a4a0 commit a2df7d7

2 files changed

Lines changed: 10 additions & 0 deletions

File tree

src/msgpack.zig

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1143,6 +1143,11 @@ pub const Payload = union(enum) {
11431143
try self.map.put(key, val);
11441144
}
11451145

1146+
/// deep clone a Payload, allocating owned copies for dynamic data
1147+
pub fn deepClone(self: Payload, allocator: Allocator) !Payload {
1148+
return clonePayload(self, allocator);
1149+
}
1150+
11461151
/// get a NIL payload
11471152
pub inline fn nilToPayload() Payload {
11481153
return Payload{

src/test.zig

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1184,6 +1184,11 @@ test "payload utility methods" {
11841184
try expect(ext_payload.ext.type == 1);
11851185
try expect(u8eql("extdata", ext_payload.ext.data));
11861186

1187+
const cloned_str_payload = try str_payload.deepClone(allocator);
1188+
defer cloned_str_payload.free(allocator);
1189+
try expect(u8eql("test", cloned_str_payload.str.value()));
1190+
try expect(@ptrToInt(str_payload.str.value().ptr) != @ptrToInt(cloned_str_payload.str.value().ptr));
1191+
11871192
const arr_payload = try Payload.arrPayload(3, allocator);
11881193
defer arr_payload.free(allocator);
11891194
try expect((try arr_payload.getArrLen()) == 3);

0 commit comments

Comments
 (0)