Skip to content

Commit 68c8c57

Browse files
committed
free without ptr
1 parent 208b18e commit 68c8c57

File tree

2 files changed

+16
-16
lines changed

2 files changed

+16
-16
lines changed

src/msgpack.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,8 @@ pub const Payload = union(enum) {
182182

183183
/// free the all memeory for this payload and sub payloads
184184
/// the allocator is payload's allocator
185-
pub fn free(self: *Payload, allocator: Allocator) void {
186-
switch (self.*) {
185+
pub fn free(self: Payload, allocator: Allocator) void {
186+
switch (self) {
187187
.str => {
188188
const str = self.str;
189189
allocator.free(str.value());
@@ -211,7 +211,7 @@ pub const Payload = union(enum) {
211211
}
212212
},
213213
.arr => {
214-
var arr = self.arr;
214+
const arr = self.arr;
215215
defer allocator.free(arr);
216216
for (0..arr.len) |i| {
217217
arr[i].free(allocator);

src/msgpack_unit_test.zig

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ test "nil write and read" {
2929
);
3030

3131
try p.write(Payload{ .nil = void{} });
32-
var val = try p.read(allocator);
32+
const val = try p.read(allocator);
3333
defer val.free(allocator);
3434
}
3535

@@ -48,10 +48,10 @@ test "bool write and read" {
4848
try p.write(.{ .bool = test_val_1 });
4949
try p.write(.{ .bool = test_val_2 });
5050

51-
var val_1 = try p.read(allocator);
51+
const val_1 = try p.read(allocator);
5252
defer val_1.free(allocator);
5353

54-
var val_2 = try p.read(allocator);
54+
const val_2 = try p.read(allocator);
5555
defer val_2.free(allocator);
5656

5757
try expect(val_1.bool == test_val_1);
@@ -69,13 +69,13 @@ test "int/uint write and read" {
6969

7070
const test_val_1: u8 = 21;
7171
try p.write(.{ .uint = test_val_1 });
72-
var val_1 = try p.read(allocator);
72+
const val_1 = try p.read(allocator);
7373
defer val_1.free(allocator);
7474
try expect(val_1.uint == test_val_1);
7575

7676
const test_val_2: i8 = -6;
7777
try p.write(.{ .int = test_val_2 });
78-
var val_2 = try p.read(allocator);
78+
const val_2 = try p.read(allocator);
7979
defer val_2.free(allocator);
8080
try expect(val_2.int == test_val_2);
8181
}
@@ -91,7 +91,7 @@ test "float write and read" {
9191

9292
const test_val: f64 = 3.5e+38;
9393
try p.write(.{ .float = test_val });
94-
var val = try p.read(allocator);
94+
const val = try p.read(allocator);
9595
defer val.free(allocator);
9696
try expect(val.float == test_val);
9797
}
@@ -107,7 +107,7 @@ test "str write and read" {
107107

108108
const test_str = "Hello, world!";
109109
try p.write(.{ .str = msgpack.wrapStr(test_str) });
110-
var val = try p.read(allocator);
110+
const val = try p.read(allocator);
111111
defer val.free(allocator);
112112
try expect(u8eql(test_str, val.str.value()));
113113
}
@@ -125,9 +125,9 @@ test "bin write and read" {
125125
// u8 bin
126126
var test_bin = "This is a string that is more than 32 bytes long.".*;
127127
try p.write(.{ .bin = msgpack.wrapBin(&test_bin) });
128-
var val_1 = try p.read(allocator);
129-
defer val_1.free(allocator);
130-
try expect(u8eql(&test_bin, val_1.bin.value()));
128+
const val = try p.read(allocator);
129+
defer val.free(allocator);
130+
try expect(u8eql(&test_bin, val.bin.value()));
131131
}
132132

133133
test "map write and read" {
@@ -167,7 +167,7 @@ test "map write and read" {
167167

168168
try p.write(test_val_1);
169169

170-
var val = try p.read(allocator);
170+
const val = try p.read(allocator);
171171
defer val.free(allocator);
172172

173173
try expect(val == .map);
@@ -204,7 +204,7 @@ test "array write and read" {
204204
}
205205

206206
try p.write(.{ .arr = &test_payload });
207-
var val = try p.read(allocator);
207+
const val = try p.read(allocator);
208208
defer val.free(allocator);
209209

210210
for (val.arr, 0..) |v, i| {
@@ -225,7 +225,7 @@ test "ext write and read" {
225225
const test_type: u8 = 1;
226226

227227
try p.write(.{ .ext = msgpack.EXT{ .type = test_type, .data = &test_data } });
228-
var val = try p.read(allocator);
228+
const val = try p.read(allocator);
229229
defer val.free(allocator);
230230
try expect(u8eql(&test_data, val.ext.data));
231231
try expect(test_type == val.ext.type);

0 commit comments

Comments
 (0)