Compare commits

..

No commits in common. "b698f18c4d1504296d2155425284db75f24e7c78" and "ef2c6c3a3c937d00db8c474e759be800bdb7ae92" have entirely different histories.

4 changed files with 44 additions and 48 deletions

2
.vscode/launch.json vendored
View File

@ -14,7 +14,7 @@
"name": "Test",
"type": "gdb",
"request": "launch",
"target": "${workspaceFolder}/zig-cache/o/2c2b6e0f85a1dcd7caa00765b05ec679/test",
"target": "$(find zig-cache -name test) src/main.zig",
"arguments": "main.zig",
"cwd": "${workspaceRoot}",
"valuesFormatting": "parseText",

View File

@ -171,10 +171,9 @@ test "Spliterating text" {
}
///
/// Closure that captures a reference to writable resources like block devices, memory buffers,
/// network sockets, and more.
/// Opaque interface to a "writable" resource like a block device, memory buffer, or network socket.
///
pub const Writer = meta.Function(@sizeOf(usize), []const u8, usize);
pub const Writer = meta.Function([]const u8, usize);
///
/// Returns `true` if `this_bytes` is the same length and contains the same data as `that_bytes`,
@ -213,19 +212,19 @@ test "Hashing bytes" {
try testing.expect(hashBytes(bytes_sequence) != hashBytes(&.{69, 42}));
}
var null_context: usize = undefined;
///
/// Returns a [Writer] that silently consumes all given data without failure and throws it away.
/// Writer that silently throws consumed data away and never fails.
///
/// This is commonly used for testing or redirected otherwise unwanted output data that has to be
/// This is commonly used for testing or redirected otherwise unwanted output data that can't not be
/// sent somewhere for whatever reason.
///
pub fn nullWriter() Writer {
return Writer.capture(std.mem.zeroes(usize), struct {
fn write(_: usize, buffer: []const u8) usize {
return buffer.len;
}
}.write);
}
pub const null_writer = Writer.wrap(&null_context, struct {
fn write(_: *@TypeOf(null_context), buffer: []const u8) usize {
return buffer.len;
}
}.write);
test "Null writing" {
const testing = std.testing;
@ -233,7 +232,7 @@ test "Null writing" {
{
const sequence = "foo";
try testing.expectEqual(nullWriter().apply(sequence), sequence.len);
try testing.expectEqual(null_writer.apply(sequence), sequence.len);
}
}
@ -242,5 +241,5 @@ test "Null writing" {
/// otherwise `false`.
///
pub fn writeByte(writer: Writer, byte: u8) bool {
return (writer.apply(std.mem.asBytes(&byte)) != 0);
return (writer.call(.{@ptrCast([*]const u8, &byte)[0 .. 1]}) != 0);
}

View File

@ -1,5 +1,3 @@
const std = @import("std");
///
/// Returns the return type of the function type `Fn`.
///
@ -12,14 +10,13 @@ pub fn FnReturn(comptime Fn: type) type {
}
///
/// Returns a single-input single-output closure type where `Input` represents the input type,
/// `Output` represents the output type, and `captures_size` represents the size of the closure
/// context.
/// Returns single-input closure type where `Input` is the input type and `Output` is the output
/// type.
///
pub fn Function(comptime captures_size: usize, comptime Input: type, comptime Output: type) type {
pub fn Function(comptime Input: type, comptime Output: type) type {
return struct {
applyErased: fn (*anyopaque, Input) Output,
context: [captures_size]u8,
context: *anyopaque,
contextualApply: fn (*anyopaque, Input) Output,
///
/// Function type.
@ -27,41 +24,38 @@ pub fn Function(comptime captures_size: usize, comptime Input: type, comptime Ou
const Self = @This();
///
/// Applies `input` to `self`, producing a result according to the current context data.
/// Applies `input` to `self`, producing a result according to the type-erased
/// implementation.
///
pub fn apply(self: *Self, input: Input) Output {
return self.applyErased(&self.context, input);
pub fn apply(self: Self, input: Input) Output {
return self.contextualApply(self.context, input);
}
///
/// Creates a new [Self] by capturing the `captures` value as the context and `call` as the
/// as the behavior executed when [apply] or [applyErased] is called.
/// Creates a new [Self] by wrapping `concrete_context` as a pointer to the implementation
/// and `contextualApply` as the behavior executed when [apply] is called.
///
/// The newly created [Self] is returned.
///
pub fn capture(captures: anytype, comptime call: fn (@TypeOf(captures), Input) Output) Self {
const Captures = @TypeOf(captures);
pub fn wrap(
concrete_context: anytype,
comptime contextualApply: fn (@TypeOf(concrete_context), Input) Output
) Self {
const ConcreteContext = @TypeOf(concrete_context);
if (@sizeOf(Captures) > captures_size)
@compileError("`captures` must be smaller than or equal to " ++
std.fmt.comptimePrint("{d}", .{captures_size}) ++ " bytes");
if (@typeInfo(ConcreteContext) != .Pointer)
@compileError("`concrete_context` must be a pointer type");
const captures_align = @alignOf(Captures);
return .{
.context = concrete_context,
var function = Self{
.context = undefined,
.applyErased = struct {
fn do(erased: *anyopaque, input: Input) Output {
return call(@ptrCast(*Captures, @alignCast(
captures_align, erased)).*, input);
.contextualApply = struct {
fn call(erased_context: *anyopaque, input: Input) Output {
return contextualApply(@ptrCast(ConcreteContext, @alignCast(
@alignOf(ConcreteContext), erased_context)), input);
}
}.do,
}.call,
};
@ptrCast(*Captures, @alignCast(captures_align, &function.context)).* = captures;
return function;
}
};
}

View File

@ -23,7 +23,7 @@ pub fn Fixed(comptime Element: type) type {
if (Element != u8) @compileError("Cannot coerce fixed stack of type " ++
@typeName(Element) ++ " into a Writer");
return io.Writer.capture(self, struct {
return io.Writer.wrap(self, struct {
fn write(stack: *Self, buffer: []const u8) usize {
stack.pushAll(buffer) catch |err| switch (err) {
error.OutOfMemory => return 0,
@ -109,5 +109,8 @@ test "Fixed stack manipulation" {
stack.clear();
try testing.expectEqual(stack.count(), 0);
try testing.expectEqual(stack.writer().apply(&.{0, 0, 0, 0}), 4);
const writer = stack.writer();
try testing.expectEqual(writer.apply(&.{0, 0, 0, 0}), 4);
}