circbuf doesn't use zeroes, it takes a default value

This commit is contained in:
Mitchell Hashimoto
2022-08-31 15:27:45 -07:00
parent e8009f89cf
commit 10ec5f509e
2 changed files with 6 additions and 6 deletions

View File

@ -311,7 +311,7 @@ pub const RowIndexTag = enum {
} }
}; };
const StorageBuf = CircBuf(StorageCell); const StorageBuf = CircBuf(StorageCell, .{ .cell = .{} });
/// The allocator used for all the storage operations /// The allocator used for all the storage operations
alloc: Allocator, alloc: Allocator,

View File

@ -3,7 +3,7 @@ const assert = std.debug.assert;
const Allocator = std.mem.Allocator; const Allocator = std.mem.Allocator;
/// Returns a circular buffer containing type T. /// Returns a circular buffer containing type T.
pub fn CircBuf(comptime T: type) type { pub fn CircBuf(comptime T: type, comptime default: T) type {
return struct { return struct {
const Self = @This(); const Self = @This();
@ -26,7 +26,7 @@ pub fn CircBuf(comptime T: type) type {
/// Initialize a new circular buffer that can store size elements. /// Initialize a new circular buffer that can store size elements.
pub fn init(alloc: Allocator, size: usize) !Self { pub fn init(alloc: Allocator, size: usize) !Self {
var buf = try alloc.alloc(T, size); var buf = try alloc.alloc(T, size);
std.mem.set(T, buf, std.mem.zeroes(T)); std.mem.set(T, buf, default);
return Self{ return Self{
.storage = buf, .storage = buf,
@ -141,7 +141,7 @@ test {
const testing = std.testing; const testing = std.testing;
const alloc = testing.allocator; const alloc = testing.allocator;
const Buf = CircBuf(u8); const Buf = CircBuf(u8, 0);
var buf = try Buf.init(alloc, 12); var buf = try Buf.init(alloc, 12);
defer buf.deinit(alloc); defer buf.deinit(alloc);
@ -153,7 +153,7 @@ test "getPtrSlice fits" {
const testing = std.testing; const testing = std.testing;
const alloc = testing.allocator; const alloc = testing.allocator;
const Buf = CircBuf(u8); const Buf = CircBuf(u8, 0);
var buf = try Buf.init(alloc, 12); var buf = try Buf.init(alloc, 12);
defer buf.deinit(alloc); defer buf.deinit(alloc);
@ -167,7 +167,7 @@ test "getPtrSlice wraps" {
const testing = std.testing; const testing = std.testing;
const alloc = testing.allocator; const alloc = testing.allocator;
const Buf = CircBuf(u8); const Buf = CircBuf(u8, 0);
var buf = try Buf.init(alloc, 4); var buf = try Buf.init(alloc, 4);
defer buf.deinit(alloc); defer buf.deinit(alloc);