chore: better conform to zig idioms

This commit is contained in:
2022-09-03 18:30:48 -03:00
parent 59669ba3a5
commit 3fb7f2f814
8 changed files with 55 additions and 71 deletions

View File

@@ -8,27 +8,27 @@ pub const size = 0x4000;
const Self = @This();
buf: ?[]u8,
alloc: Allocator,
allocator: Allocator,
addr_latch: u32,
pub fn init(alloc: Allocator, maybe_path: ?[]const u8) !Self {
pub fn init(allocator: Allocator, maybe_path: ?[]const u8) !Self {
const buf: ?[]u8 = if (maybe_path) |path| blk: {
const file = try std.fs.cwd().openFile(path, .{});
defer file.close();
break :blk try file.readToEndAlloc(alloc, try file.getEndPos());
break :blk try file.readToEndAlloc(allocator, try file.getEndPos());
} else null;
return Self{
.buf = buf,
.alloc = alloc,
.allocator = allocator,
.addr_latch = 0,
};
}
pub fn deinit(self: *Self) void {
if (self.buf) |buf| self.alloc.free(buf);
if (self.buf) |buf| self.allocator.free(buf);
self.* = undefined;
}

View File

@@ -5,20 +5,20 @@ const ewram_size = 0x40000;
const Self = @This();
buf: []u8,
alloc: Allocator,
allocator: Allocator,
pub fn init(alloc: Allocator) !Self {
const buf = try alloc.alloc(u8, ewram_size);
pub fn init(allocator: Allocator) !Self {
const buf = try allocator.alloc(u8, ewram_size);
std.mem.set(u8, buf, 0);
return Self{
.buf = buf,
.alloc = alloc,
.allocator = allocator,
};
}
pub fn deinit(self: *Self) void {
self.alloc.free(self.buf);
self.allocator.free(self.buf);
self.* = undefined;
}

View File

@@ -8,22 +8,22 @@ const Self = @This();
title: [12]u8,
buf: []u8,
alloc: Allocator,
allocator: Allocator,
backup: Backup,
pub fn init(alloc: Allocator, rom_path: []const u8, save_path: ?[]const u8) !Self {
pub fn init(allocator: Allocator, rom_path: []const u8, save_path: ?[]const u8) !Self {
const file = try std.fs.cwd().openFile(rom_path, .{});
defer file.close();
const file_buf = try file.readToEndAlloc(alloc, try file.getEndPos());
const file_buf = try file.readToEndAlloc(allocator, try file.getEndPos());
const title = parseTitle(file_buf);
const kind = Backup.guessKind(file_buf) orelse .None;
const pak = Self{
.buf = file_buf,
.alloc = alloc,
.allocator = allocator,
.title = title,
.backup = try Backup.init(alloc, kind, title, save_path),
.backup = try Backup.init(allocator, kind, title, save_path),
};
pak.parseHeader();
@@ -60,7 +60,7 @@ inline fn isLarge(self: *const Self) bool {
pub fn deinit(self: *Self) void {
self.backup.deinit();
self.alloc.free(self.buf);
self.allocator.free(self.buf);
self.* = undefined;
}

View File

@@ -5,20 +5,20 @@ const iwram_size = 0x8000;
const Self = @This();
buf: []u8,
alloc: Allocator,
allocator: Allocator,
pub fn init(alloc: Allocator) !Self {
const buf = try alloc.alloc(u8, iwram_size);
pub fn init(allocator: Allocator) !Self {
const buf = try allocator.alloc(u8, iwram_size);
std.mem.set(u8, buf, 0);
return Self{
.buf = buf,
.alloc = alloc,
.allocator = allocator,
};
}
pub fn deinit(self: *Self) void {
self.alloc.free(self.buf);
self.allocator.free(self.buf);
self.* = undefined;
}

View File

@@ -17,7 +17,7 @@ pub const Backup = struct {
const Self = @This();
buf: []u8,
alloc: Allocator,
allocator: Allocator,
kind: Kind,
title: [12]u8,
@@ -49,7 +49,7 @@ pub const Backup = struct {
var backup = Self{
.buf = buf,
.alloc = allocator,
.allocator = allocator,
.kind = kind,
.title = title,
.save_path = path,
@@ -75,8 +75,8 @@ pub const Backup = struct {
}
pub fn deinit(self: *Self) void {
if (self.save_path) |path| self.writeSaveToDisk(self.alloc, path) catch |e| log.err("Failed to write save: {}", .{e});
self.alloc.free(self.buf);
if (self.save_path) |path| self.writeSaveToDisk(self.allocator, path) catch |e| log.err("Failed to write save: {}", .{e});
self.allocator.free(self.buf);
self.* = undefined;
}
@@ -310,7 +310,7 @@ const Eeprom = struct {
writer: Writer,
reader: Reader,
alloc: Allocator,
allocator: Allocator,
const Kind = enum {
Unknown,
@@ -326,14 +326,14 @@ const Eeprom = struct {
RequestEnd,
};
fn init(alloc: Allocator) Self {
fn init(allocator: Allocator) Self {
return .{
.kind = .Unknown,
.state = .Ready,
.writer = Writer.init(),
.reader = Reader.init(),
.addr = 0,
.alloc = alloc,
.allocator = allocator,
};
}
@@ -361,7 +361,7 @@ const Eeprom = struct {
else => unreachable,
};
buf.* = self.alloc.alloc(u8, len) catch |e| {
buf.* = self.allocator.alloc(u8, len) catch |e| {
log.err("Failed to resize EEPROM buf to {} bytes", .{len});
std.debug.panic("EEPROM entered irrecoverable state {}", .{e});
};