1
0
Fork 0

Simplified io context implementation

This commit is contained in:
Julien Dessaux 2022-08-03 23:14:41 +02:00
parent e5eca00399
commit 8e9f1f3af0
Signed by: adyxax
GPG key ID: F92E51B86E07177E
3 changed files with 15 additions and 29 deletions

View file

@ -5,18 +5,7 @@ pub fn Context(readerType: anytype, writerType: anytype) type {
reader: readerType,
writer: writerType,
lastChar: ?i64 = null,
allocator: std.mem.Allocator,
const Self = @This();
pub fn deinit(self: *Self) void {
self.allocator.destroy(self);
}
pub fn init(allocator: std.mem.Allocator, reader: readerType, writer: writerType) !*Self {
var c = try allocator.create(Self);
c.allocator = allocator;
c.reader = reader;
c.writer = writer;
return c;
}
pub fn characterInput(self: *Self) !i64 {
if (self.lastChar) |c| {
self.lastChar = null;
@ -33,8 +22,7 @@ pub fn Context(readerType: anytype, writerType: anytype) type {
var stdin = std.io.fixedBufferStream("ab0");
var stdout = std.ArrayList(u8).init(std.testing.allocator);
defer stdout.deinit();
var ioContext = try context(std.testing.allocator, stdin, stdout);
defer ioContext.deinit();
var ioContext = context(stdin, stdout);
try std.testing.expectEqual(try ioContext.characterInput(), 'a');
try std.testing.expectEqual(try ioContext.characterInput(), 'b');
try std.testing.expectEqual(try ioContext.characterInput(), '0');
@ -64,8 +52,7 @@ pub fn Context(readerType: anytype, writerType: anytype) type {
var stdin = std.io.fixedBufferStream("1 234abc5d6ef");
var stdout = std.ArrayList(u8).init(std.testing.allocator);
defer stdout.deinit();
var ioContext = try context(std.testing.allocator, stdin, stdout);
defer ioContext.deinit();
var ioContext = context(stdin, stdout);
try std.testing.expectEqual(try ioContext.decimalInput(), 1);
try std.testing.expectEqual(try ioContext.decimalInput(), 234);
try std.testing.expectEqual(try ioContext.decimalInput(), 5);
@ -83,8 +70,11 @@ pub fn Context(readerType: anytype, writerType: anytype) type {
};
}
pub fn context(allocator: std.mem.Allocator, reader: anytype, writer: anytype) !*Context(@TypeOf(reader), @TypeOf(writer)) {
return Context(@TypeOf(reader), @TypeOf(writer)).init(allocator, reader, writer);
pub fn context(reader: anytype, writer: anytype) Context(@TypeOf(reader), @TypeOf(writer)) {
return .{
.reader = reader,
.writer = writer,
};
}
test "all" {

View file

@ -18,9 +18,8 @@ pub fn main() anyerror!void {
var i = try interpreter.Interpreter.init(gpa.allocator(), file.reader(), args);
defer i.deinit();
var ioContext = try io.context(gpa.allocator(), std.io.getStdIn().reader(), std.io.getStdOut().writer());
defer ioContext.deinit();
std.os.exit(@intCast(u8, try i.run(ioContext)));
var ioContext = io.context(std.io.getStdIn().reader(), std.io.getStdOut().writer());
std.os.exit(@intCast(u8, try i.run(&ioContext)));
}
test "all" {
@ -36,8 +35,7 @@ test "sanity" {
const args = [_][]const u8{"sanity"};
var i = try interpreter.Interpreter.init(std.testing.allocator, file.reader(), args[0..]);
defer i.deinit();
var ioContext = try io.context(std.testing.allocator, stdin.reader(), stdout.writer());
defer ioContext.deinit();
try std.testing.expectEqual(try i.run(ioContext), 0);
var ioContext = io.context(stdin.reader(), stdout.writer());
try std.testing.expectEqual(try i.run(&ioContext), 0);
try std.testing.expectEqual(std.mem.eql(u8, stdout.items, expected), true);
}

View file

@ -342,9 +342,8 @@ test "minimal" {
const argv = [_][]const u8{"minimal"};
var p = try Pointer.init(std.testing.allocator, f, argv[0..]);
defer p.deinit();
var ioContext = try io.context(std.testing.allocator, std.io.getStdIn().reader(), std.io.getStdOut().writer());
defer ioContext.deinit();
try std.testing.expectEqual(p.exec(ioContext), pointerReturn{});
var ioContext = io.context(std.io.getStdIn().reader(), std.io.getStdOut().writer());
try std.testing.expectEqual(p.exec(&ioContext), pointerReturn{});
}
test "almost minimal" {
const minimal = std.io.fixedBufferStream(" @").reader();
@ -353,7 +352,6 @@ test "almost minimal" {
const argv = [_][]const u8{"minimal"};
var p = try Pointer.init(std.testing.allocator, f, argv[0..]);
defer p.deinit();
var ioContext = try io.context(std.testing.allocator, std.io.getStdIn().reader(), std.io.getStdOut().writer());
defer ioContext.deinit();
try std.testing.expectEqual(p.exec(ioContext), pointerReturn{});
var ioContext = io.context(std.io.getStdIn().reader(), std.io.getStdOut().writer());
try std.testing.expectEqual(p.exec(&ioContext), pointerReturn{});
}