aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/io.zig24
-rw-r--r--src/main.zig10
-rw-r--r--src/pointer.zig10
3 files changed, 15 insertions, 29 deletions
diff --git a/src/io.zig b/src/io.zig
index 886d7e7..f33437f 100644
--- a/src/io.zig
+++ b/src/io.zig
@@ -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" {
diff --git a/src/main.zig b/src/main.zig
index b85aa6f..32874e5 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -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);
}
diff --git a/src/pointer.zig b/src/pointer.zig
index 75bcd10..082ce37 100644
--- a/src/pointer.zig
+++ b/src/pointer.zig
@@ -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{});
}