| ... | ... | @@ -105,28 +105,30 @@ pub fn Queue(comptime T: type) type { |
| 105 | 105 | pub fn dump(self: *Self) void { |
| 106 | 106 | var stderr_file = std.io.getStdErr() catch return; |
| 107 | 107 | const stderr = &stderr_file.outStream().stream; |
| 108 | const Error = @typeInfo(@typeOf(stderr)).Pointer.child.Error; |
| 108 | 109 | |
| 109 | | self.dumpToStream(stderr) catch return; |
| 110 | self.dumpToStream(Error, stderr) catch return; |
| 110 | 111 | } |
| 111 | 112 | |
| 112 | | pub fn dumpToStream(self: *Self, stream: var) @typeOf(stream).Error!void { |
| 113 | pub fn dumpToStream(self: *Self, comptime Error: type, stream: *std.io.OutStream(Error)) Error!void { |
| 114 | const S = struct.{ |
| 115 | fn dumpRecursive(s: *std.io.OutStream(Error), optional_node: ?*Node, indent: usize) Error!void { |
| 116 | try s.writeByteNTimes(' ', indent); |
| 117 | if (optional_node) |node| { |
| 118 | try s.print("0x{x}={}\n", @ptrToInt(node), node.data); |
| 119 | try dumpRecursive(s, node.next, indent + 1); |
| 120 | } else { |
| 121 | try s.print("(null)\n"); |
| 122 | } |
| 123 | } |
| 124 | }; |
| 113 | 125 | const held = self.mutex.acquire(); |
| 114 | 126 | defer held.release(); |
| 115 | 127 | |
| 116 | 128 | try stream.print("head: "); |
| 117 | | try dumpRecursive(stream, self.head, 0); |
| 129 | try S.dumpRecursive(stream, self.head, 0); |
| 118 | 130 | try stream.print("tail: "); |
| 119 | | try dumpRecursive(stream, self.tail, 0); |
| 120 | | } |
| 121 | | |
| 122 | | fn dumpRecursive(stream: var, optional_node: ?*Node, indent: usize) error!void { |
| 123 | | try stream.writeByteNTimes(' ', indent); |
| 124 | | if (optional_node) |node| { |
| 125 | | try stream.print("0x{x}={}\n", @ptrToInt(node), node.data); |
| 126 | | try dumpRecursive(stream, node.next, indent + 1); |
| 127 | | } else { |
| 128 | | try stream.print("(null)\n"); |
| 129 | | } |
| 131 | try S.dumpRecursive(stream, self.tail, 0); |
| 130 | 132 | } |
| 131 | 133 | }; |
| 132 | 134 | } |
| ... | ... | @@ -291,15 +293,15 @@ test "std.atomic.Queue dump" { |
| 291 | 293 | |
| 292 | 294 | // Test empty stream |
| 293 | 295 | sos.reset(); |
| 294 | | try queue.dumpToStream(&sos.stream); |
| 296 | try queue.dumpToStream(SliceOutStream.Error, &sos.stream); |
| 295 | 297 | assert(mem.eql(u8, buffer[0..sos.pos], |
| 296 | 298 | \\head: (null) |
| 297 | 299 | \\tail: (null) |
| 298 | 300 | \\ |
| 299 | | )); |
| 301 | )); |
| 300 | 302 | |
| 301 | 303 | // Test a stream with one element |
| 302 | | var node_0 = Queue(i32).Node { |
| 304 | var node_0 = Queue(i32).Node.{ |
| 303 | 305 | .data = 1, |
| 304 | 306 | .next = undefined, |
| 305 | 307 | .prev = undefined, |
| ... | ... | @@ -307,7 +309,7 @@ test "std.atomic.Queue dump" { |
| 307 | 309 | queue.put(&node_0); |
| 308 | 310 | |
| 309 | 311 | sos.reset(); |
| 310 | | try queue.dumpToStream(&sos.stream); |
| 312 | try queue.dumpToStream(SliceOutStream.Error, &sos.stream); |
| 311 | 313 | |
| 312 | 314 | var expected = try std.fmt.bufPrint(expected_buffer[0..], |
| 313 | 315 | \\head: 0x{x}=1 |
| ... | ... | @@ -315,11 +317,11 @@ test "std.atomic.Queue dump" { |
| 315 | 317 | \\tail: 0x{x}=1 |
| 316 | 318 | \\ (null) |
| 317 | 319 | \\ |
| 318 | | , @ptrToInt(queue.head), @ptrToInt(queue.tail)); |
| 320 | , @ptrToInt(queue.head), @ptrToInt(queue.tail)); |
| 319 | 321 | assert(mem.eql(u8, buffer[0..sos.pos], expected)); |
| 320 | 322 | |
| 321 | 323 | // Test a stream with two elements |
| 322 | | var node_1 = Queue(i32).Node { |
| 324 | var node_1 = Queue(i32).Node.{ |
| 323 | 325 | .data = 2, |
| 324 | 326 | .next = undefined, |
| 325 | 327 | .prev = undefined, |
| ... | ... | @@ -327,7 +329,7 @@ test "std.atomic.Queue dump" { |
| 327 | 329 | queue.put(&node_1); |
| 328 | 330 | |
| 329 | 331 | sos.reset(); |
| 330 | | try queue.dumpToStream(&sos.stream); |
| 332 | try queue.dumpToStream(SliceOutStream.Error, &sos.stream); |
| 331 | 333 | |
| 332 | 334 | expected = try std.fmt.bufPrint(expected_buffer[0..], |
| 333 | 335 | \\head: 0x{x}=1 |
| ... | ... | @@ -336,6 +338,6 @@ test "std.atomic.Queue dump" { |
| 336 | 338 | \\tail: 0x{x}=2 |
| 337 | 339 | \\ (null) |
| 338 | 340 | \\ |
| 339 | | , @ptrToInt(queue.head), @ptrToInt(queue.head.?.next), @ptrToInt(queue.tail)); |
| 341 | , @ptrToInt(queue.head), @ptrToInt(queue.head.?.next), @ptrToInt(queue.tail)); |
| 340 | 342 | assert(mem.eql(u8, buffer[0..sos.pos], expected)); |
| 341 | 343 | } |