Change Allocator to use interface style
continuous-integration/drone/push Build is passing Details
continuous-integration/drone/pr Build is passing Details

This commit is contained in:
kayomn 2022-10-24 13:59:37 +01:00
parent 989de70461
commit d578bb422e
2 changed files with 30 additions and 23 deletions

View File

@ -3,9 +3,9 @@ const stack = @import("./stack.zig");
const std = @import("std");
///
/// Closure for allocating, reallocating, and deallocating dynamic memory resources through itself.
///
pub const Allocator = meta.BiFunction(56, ?[]u8, usize, AllocationError![]u8);
///
pub const Allocator = std.mem.Allocator;
///
/// File-system agnostic abstraction for manipulating a file.
@ -203,7 +203,7 @@ pub fn equals(comptime Element: type, this: []const Element, that: []const Eleme
return true;
}
test "Equivalence of bytes" {
test "Memory buffers equal" {
const bytes_sequence = &.{69, 42, 0};
const testing = std.testing;
@ -255,11 +255,3 @@ test "Null writing" {
try testing.expectEqual(nullWriter().apply(sequence), sequence.len);
}
}
///
/// Applies the singular `byte` to `writer`, returning `true` if it was successfully written,
/// otherwise `false`.
///
pub fn writeByte(writer: *Writer, byte: u8) bool {
return (writer.apply(std.mem.asBytes(&byte)) != 0);
}

View File

@ -1,4 +1,4 @@
const std = @import("std");
const io = @import("./io.zig");
///
/// Returns a hash-backed table type of `Value`s indexed by `Key` and using `key_context` as the key
@ -7,7 +7,7 @@ const std = @import("std");
pub fn Hashed(comptime Key: type, comptime Value: type,
comptime key_context: KeyContext(Key)) type {
const Allocator = std.mem.Allocator;
const Allocator = io.Allocator;
return struct {
allocator: Allocator,
@ -44,11 +44,13 @@ pub fn Hashed(comptime Key: type, comptime Value: type,
///
/// Initializes a [Self] using `allocator` as the memory allocation strategy.
///
/// Returns a new [Self] value or an [Allocator.Error] if initializing failed.
/// Returns a new [Self] value or an [io.Allocator.Error] if initializing failed.
///
pub fn init(allocator: Allocator) Allocator.Error!Self {
const initial_capacity = 4;
return Self{
.buckets = try allocator.alloc(Bucket, 4),
.buckets = try allocator.alloc(Bucket, initial_capacity),
.filled = 0,
.allocator = allocator,
.load_limit = 0.75,
@ -158,7 +160,7 @@ pub fn Hashed(comptime Key: type, comptime Value: type,
/// [InsertError.KeyExists] occurs when an insertion was attempted on a table with a matching key
/// already present.
///
pub const InsertError = std.mem.Allocator.Error || error {
pub const InsertError = io.Allocator.Error || error {
KeyExists,
};
@ -173,14 +175,27 @@ pub fn KeyContext(comptime Key: type) type {
};
}
test "Hashed table manipulation with bytes context" {
const testing = std.testing;
const io = @import("./io.zig");
///
/// A [KeyContext] for dealing with string literal (i.e. []const u8) values.
///
/// **Note** that, while lightweight, this context should only be considered safe to use with string
/// literals or variables pointing to string literals - as the [KeyContext] does not take ownership
/// of its keys beyond copying the reference.
///
pub const string_literal_context = KeyContext([]const u8){
.hash = io.hashBytes,
var table = try Hashed([]const u8, u32, .{
.equals = io.equalsBytes,
.hash = io.hashBytes,
}).init(testing.allocator);
.equals = struct {
fn stringsEqual(this: []const u8, that: []const u8) bool {
return io.equals(u8, this, that);
}
}.stringsEqual,
};
test "Hash table manipulation with string literal context" {
const testing = @import("std").testing;
var table = try Hashed([]const u8, u32, string_literal_context).init(testing.allocator);
defer table.deinit();