From 72349459ec29ee2d910a3dd3274be00254a7acd4 Mon Sep 17 00:00:00 2001 From: Rekai Musuka Date: Tue, 23 May 2023 01:38:15 -0500 Subject: [PATCH] feat(channel): impl peek() --- src/lib.zig | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/lib.zig b/src/lib.zig index 184b958..711b09d 100644 --- a/src/lib.zig +++ b/src/lib.zig @@ -78,6 +78,16 @@ fn Channel(comptime T: type) type { return value; } + pub fn peek(self: *const Self) ?T { + const read_idx = self.read.load(.Acquire); + const write_idx = self.write.load(.Acquire); + + if (read_idx == write_idx) return null; + + std.atomic.fence(.Acquire); + return self.buf[self.mask(read_idx)]; + } + pub fn len(self: *const Self) Index { const read_idx = self.read.load(.Acquire); const write_idx = self.write.load(.Acquire);