Remove unused coral IO functions

This commit is contained in:
kayomn 2023-04-19 20:53:51 +01:00
parent bb2745c3d0
commit a7605c6ffa
1 changed files with 207 additions and 219 deletions

426
source/coral/io.zig Normal file → Executable file
View File

@ -1,219 +1,207 @@
const debug = @import("./debug.zig"); const debug = @import("./debug.zig");
const math = @import("./math.zig"); const math = @import("./math.zig");
pub const MemoryArena = struct { pub const MemoryArena = struct {
pub fn as_allocator(self: *MemoryArena) MemoryAllocator { pub fn as_allocator(self: *MemoryArena) MemoryAllocator {
return MemoryAllocator.bind(self, MemoryArena); return MemoryAllocator.bind(self, MemoryArena);
} }
}; };
pub const MemoryAllocator = struct { pub const MemoryAllocator = struct {
context: *anyopaque, context: *anyopaque,
call: *const fn (capture: *anyopaque, maybe_allocation: ?[*]u8, size: usize) ?[*]u8, call: *const fn (capture: *anyopaque, maybe_allocation: ?[*]u8, size: usize) ?[*]u8,
const Capture = [@sizeOf(usize)]u8; const Capture = [@sizeOf(usize)]u8;
pub fn bind(state: anytype, comptime Actions: type) MemoryAllocator { pub fn bind(state: anytype, comptime Actions: type) MemoryAllocator {
const State = @TypeOf(state); const State = @TypeOf(state);
const state_info = @typeInfo(State); const state_info = @typeInfo(State);
if (state_info != .Pointer) @compileError("`@typeOf(state)` must be a pointer type"); if (state_info != .Pointer) @compileError("`@typeOf(state)` must be a pointer type");
return .{ return .{
.context = state, .context = state,
.call = struct { .call = struct {
fn reallocate(context: *anyopaque, maybe_allocation: ?[*]u8, size: usize) ?[*]u8 { fn reallocate(context: *anyopaque, maybe_allocation: ?[*]u8, size: usize) ?[*]u8 {
return Actions.reallocate(@ptrCast(State, @alignCast(@alignOf(state_info.Pointer.child), context)), maybe_allocation, size); return Actions.reallocate(@ptrCast(State, @alignCast(@alignOf(state_info.Pointer.child), context)), maybe_allocation, size);
} }
}.reallocate, }.reallocate,
}; };
} }
pub fn allocate_many(self: MemoryAllocator, comptime Type: type, amount: usize) ?[*]Type { pub fn allocate_many(self: MemoryAllocator, comptime Type: type, amount: usize) ?[*]Type {
return @ptrCast(?[*]Type, @alignCast(@alignOf(Type), self.call(self.context, null, @sizeOf(Type) * amount))); return @ptrCast(?[*]Type, @alignCast(@alignOf(Type), self.call(self.context, null, @sizeOf(Type) * amount)));
} }
pub fn allocate_one(self: MemoryAllocator, comptime Type: type) ?*Type { pub fn allocate_one(self: MemoryAllocator, comptime Type: type) ?*Type {
return @ptrCast(?*Type, @alignCast(@alignOf(Type), self.call(self.context, null, @sizeOf(Type)))); return @ptrCast(?*Type, @alignCast(@alignOf(Type), self.call(self.context, null, @sizeOf(Type))));
} }
pub fn deallocate(self: MemoryAllocator, maybe_allocation: anytype) void { pub fn deallocate(self: MemoryAllocator, maybe_allocation: anytype) void {
if (@typeInfo(@TypeOf(maybe_allocation)) != .Pointer) if (@typeInfo(@TypeOf(maybe_allocation)) != .Pointer)
@compileError("`maybe_allocation` must be a pointer type"); @compileError("`maybe_allocation` must be a pointer type");
debug.assert(self.call(self.context, @ptrCast(?[*]u8, maybe_allocation), 0) == null); debug.assert(self.call(self.context, @ptrCast(?[*]u8, maybe_allocation), 0) == null);
} }
pub fn reallocate(self: MemoryAllocator, comptime Type: type, maybe_allocation: ?[*]Type, amount: usize) ?[*]Type { pub fn reallocate(self: MemoryAllocator, comptime Type: type, maybe_allocation: ?[*]Type, amount: usize) ?[*]Type {
return @ptrCast(?[*]Type, @alignCast(@alignOf(Type), return @ptrCast(?[*]Type, @alignCast(@alignOf(Type),
self.call(self.context, @ptrCast(?[*]u8, maybe_allocation), @sizeOf(Type) * amount))); self.call(self.context, @ptrCast(?[*]u8, maybe_allocation), @sizeOf(Type) * amount)));
} }
}; };
pub const ReadError = error{ pub const ReadError = error{
IoUnavailable, IoUnavailable,
}; };
pub const Reader = struct { pub const Reader = struct {
context: *anyopaque, context: *anyopaque,
call: *const fn (context: *anyopaque, buffer: []u8) ReadError!usize, call: *const fn (context: *anyopaque, buffer: []u8) ReadError!usize,
pub fn bind(state: anytype, comptime Actions: type) Reader { pub fn bind(state: anytype, comptime Actions: type) Reader {
const State = @TypeOf(state); const State = @TypeOf(state);
const state_info = @typeInfo(State); const state_info = @typeInfo(State);
if (@typeInfo(State) != .Pointer) @compileError("`@typeOf(state)` must be a pointer type"); if (@typeInfo(State) != .Pointer) @compileError("`@typeOf(state)` must be a pointer type");
return .{ return .{
.context = @ptrCast(*anyopaque, state), .context = @ptrCast(*anyopaque, state),
.call = struct { .call = struct {
fn read(context: *anyopaque, buffer: []u8) ReadError!usize { fn read(context: *anyopaque, buffer: []u8) ReadError!usize {
return Actions.read(@ptrCast(State, @alignCast(@alignOf(state_info.Pointer.child), context)), buffer); return Actions.read(@ptrCast(State, @alignCast(@alignOf(state_info.Pointer.child), context)), buffer);
} }
}.read, }.read,
}; };
} }
pub fn read(self: Reader, buffer: []u8) ReadError!usize { pub fn read(self: Reader, buffer: []u8) ReadError!usize {
return self.call(self.context, buffer); return self.call(self.context, buffer);
} }
}; };
pub fn Tag(comptime Element: type) type { pub const WriteError = error{
return switch (@typeInfo(Element)) { IoUnavailable,
.Enum => |info| info.tag_type, };
.Union => |info| info.tag_type orelse @compileError(@typeName(Element) ++ " has no tag type"),
else => @compileError("expected enum or union type, found '" ++ @typeName(Element) ++ "'"), pub const Writer = struct {
}; context: *anyopaque,
} call: *const fn (context: *anyopaque, buffer: []const u8) WriteError!usize,
pub const WriteError = error{ pub fn bind(state: anytype, comptime Actions: type) Writer {
IoUnavailable, const State = @TypeOf(state);
}; const state_info = @typeInfo(State);
pub const Writer = struct { if (state_info != .Pointer) @compileError("`@typeOf(state)` must be a pointer type");
context: *anyopaque,
call: *const fn (context: *anyopaque, buffer: []const u8) WriteError!usize, return .{
.context = @ptrCast(*anyopaque, state),
pub fn bind(state: anytype, comptime Actions: type) Writer {
const State = @TypeOf(state); .call = struct {
const state_info = @typeInfo(State); fn write(context: *anyopaque, buffer: []const u8) ReadError!usize {
return Actions.write(@ptrCast(State, @alignCast(@alignOf(state_info.Pointer.child), context)), buffer);
if (state_info != .Pointer) @compileError("`@typeOf(state)` must be a pointer type"); }
}.write,
return .{ };
.context = @ptrCast(*anyopaque, state), }
.call = struct { pub fn write(self: Writer, buffer: []const u8) WriteError!usize {
fn write(context: *anyopaque, buffer: []const u8) ReadError!usize { return self.call(self.context, buffer);
return Actions.write(@ptrCast(State, @alignCast(@alignOf(state_info.Pointer.child), context)), buffer); }
} };
}.write,
}; pub fn bytes_of(value: anytype) []const u8 {
} const pointer_info = @typeInfo(@TypeOf(value)).Pointer;
pub fn write(self: Writer, buffer: []const u8) WriteError!usize { debug.assert(pointer_info.size == .One);
return self.call(self.context, buffer);
} return @ptrCast([*]const u8, value)[0 .. @sizeOf(pointer_info.child)];
}; }
pub fn bytes_of(value: anytype) []const u8 { pub fn compare(this: []const u8, that: []const u8) isize {
const pointer_info = @typeInfo(@TypeOf(value)).Pointer; const range = math.min(usize, this.len, that.len);
var index: usize = 0;
debug.assert(pointer_info.size == .One);
while (index < range) : (index += 1) {
return @ptrCast([*]const u8, value)[0 .. @sizeOf(pointer_info.child)]; const difference = @intCast(isize, this[index]) - @intCast(isize, that[index]);
}
if (difference != 0) return difference;
pub fn compare(this: []const u8, that: []const u8) isize { }
const range = math.min(usize, this.len, that.len);
var index: usize = 0; return @intCast(isize, this.len) - @intCast(isize, that.len);
}
while (index < range) : (index += 1) {
const difference = @intCast(isize, this[index]) - @intCast(isize, that[index]); pub fn copy(target: []u8, source: []const u8) void {
var index: usize = 0;
if (difference != 0) return difference;
} while (index < source.len) : (index += 1) target[index] = source[index];
}
return @intCast(isize, this.len) - @intCast(isize, that.len);
} pub fn ends_with(target: []const u8, match: []const u8) bool {
if (target.len < match.len) return false;
pub fn copy(target: []u8, source: []const u8) void {
var index: usize = 0; var index = @as(usize, 0);
while (index < source.len) : (index += 1) target[index] = source[index]; while (index < match.len) : (index += 1) {
} if (target[target.len - (1 + index)] != match[match.len - (1 + index)]) return false;
}
pub fn ends_with(target: []const u8, match: []const u8) bool {
if (target.len < match.len) return false; return true;
}
var index = @as(usize, 0);
pub fn equals(this: []const u8, that: []const u8) bool {
while (index < match.len) : (index += 1) { if (this.len != that.len) return false;
if (target[target.len - (1 + index)] != match[match.len - (1 + index)]) return false;
} {
var index: usize = 0;
return true;
} while (index < this.len) : (index += 1) if (this[index] != that[index]) return false;
}
pub fn equals(this: []const u8, that: []const u8) bool {
if (this.len != that.len) return false; return true;
}
{
var index: usize = 0; var null_context = @as(usize, 0);
while (index < this.len) : (index += 1) if (this[index] != that[index]) return false; pub const null_writer = Writer.bind(&null_context, struct {
} pub fn write(context: *usize, buffer: []const u8) usize {
debug.assert(context.* == 0);
return true;
} return buffer.len;
}
var null_context = @as(usize, 0); });
pub const null_writer = Writer.bind(&null_context, struct { pub fn slice_sentineled(comptime element: type, comptime sentinel: element, sequence: [*:sentinel]const element) []const element {
pub fn write(context: *usize, buffer: []const u8) usize { var length: usize = 0;
debug.assert(context.* == 0);
while (sequence[length] != sentinel) : (length += 1) {}
return buffer.len;
} return sequence[0..length];
}); }
pub fn slice_sentineled(comptime element: type, comptime sentinel: element, sequence: [*:sentinel]const element) []const element { pub fn stream(output: Writer, input: Reader, buffer: []u8) (ReadError || WriteError)!u64 {
var length: usize = 0; var total_written: u64 = 0;
var read = try input.read(buffer);
while (sequence[length] != sentinel) : (length += 1) {}
while (read != 0) {
return sequence[0..length]; total_written += try output.write(buffer[0..read]);
} read = try input.read(buffer);
}
pub fn stream(output: Writer, input: Reader, buffer: []u8) (ReadError || WriteError)!u64 {
var total_written: u64 = 0; return total_written;
var read = try input.read(buffer); }
while (read != 0) { pub fn swap(comptime Element: type, this: *Element, that: *Element) void {
total_written += try output.write(buffer[0..read]); const temp = this.*;
read = try input.read(buffer);
} this.* = that.*;
that.* = temp;
return total_written; }
}
pub fn zero(target: []u8) void {
pub fn swap(comptime Element: type, this: *Element, that: *Element) void { for (target) |*t| t.* = 0;
const temp = this.*; }
this.* = that.*;
that.* = temp;
}
pub fn tag(value: anytype) Tag(@TypeOf(value)) {
return @as(Tag(@TypeOf(value)), value);
}
pub fn zero(target: []u8) void {
for (target) |*t| t.* = 0;
}