Skip to content

Commit 5fe9c76

Browse files
refactor(util): wrap object creation inside init function
- create object in init method, return object after; arena allocator
1 parent 1cc11a3 commit 5fe9c76

1 file changed

Lines changed: 21 additions & 22 deletions

File tree

src/util.zig

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,21 @@ const TREE_NODE = struct {
1313
node_contents: []const u8,
1414

1515
pub fn init(
16-
self: *Self,
1716
allocator: STD.mem.Allocator,
1817
node_options: struct {
1918
parent: *TREE_NODE = undefined,
2019
node_contents: []const u8 = "empty",
2120
},
22-
) !void {
21+
) !*TREE_NODE {
22+
const TREE_NODE_INSTANCE = try allocator.create(TREE_NODE);
23+
2324
const DUPLICATED_CHILD_CONTENT = try allocator.dupe(u8, node_options.node_contents);
24-
self.node_contents = DUPLICATED_CHILD_CONTENT;
25+
TREE_NODE_INSTANCE.node_contents = DUPLICATED_CHILD_CONTENT;
2526

26-
self.parent = node_options.parent;
27-
self.children = .empty;
27+
TREE_NODE_INSTANCE.parent = node_options.parent;
28+
TREE_NODE_INSTANCE.children = .empty;
29+
30+
return TREE_NODE_INSTANCE;
2831
}
2932

3033
pub fn deinit(self: *Self, allocator: STD.mem.Allocator) void {
@@ -44,26 +47,27 @@ pub const DIR_TREE = struct {
4447

4548
// NOTE: https://ziggit.dev/t/cant-work-around-the-error-unable-to-resolve-inferred-error-set/2239/6
4649
pub fn init(
47-
self: *Self,
4850
allocator: STD.mem.Allocator,
4951
dir_path: []const u8,
50-
) anyerror!void {
51-
self.root_node = try allocator.create(TREE_NODE);
52+
) anyerror!*DIR_TREE {
53+
const DIR_TREE_INSTANCE: *DIR_TREE = try allocator.create(DIR_TREE);
5254

53-
try self.root_node.init(
55+
DIR_TREE_INSTANCE.root_node = try TREE_NODE.init(
5456
allocator,
5557
.{ .node_contents = dir_path },
5658
);
5759

58-
self.tree_nodes = .empty;
60+
DIR_TREE_INSTANCE.tree_nodes = .empty;
5961

60-
self.populateTree(allocator, self.root_node) catch {
62+
DIR_TREE_INSTANCE.populateTree(allocator, DIR_TREE_INSTANCE.root_node) catch {
6163
STD.debug.print("{s}{s}Failed to populate tree instance{s}", .{
6264
STYLES.ASCII_STYLES.red,
6365
STYLES.ASCII_STYLES.underline,
6466
STYLES.ASCII_STYLES.clear_style,
6567
});
6668
};
69+
70+
return DIR_TREE_INSTANCE;
6771
}
6872

6973
fn populateTree(
@@ -106,9 +110,7 @@ pub const DIR_TREE = struct {
106110
);
107111

108112
for (children_node_contents.items) |child_content| {
109-
const CHILD_NODE_INSTANCE: *TREE_NODE = try allocator.create(TREE_NODE);
110-
111-
try CHILD_NODE_INSTANCE.init(
113+
const CHILD_NODE_INSTANCE: *TREE_NODE = try TREE_NODE.init(
112114
allocator,
113115
.{
114116
.parent = parent_node,
@@ -310,9 +312,8 @@ test "build and search directory tree" {
310312

311313
const allocator: STD.mem.Allocator = mem_arena.allocator();
312314

313-
const DIR_TREE_INSTANCE: *DIR_TREE = try allocator.create(DIR_TREE);
315+
const DIR_TREE_INSTANCE: *DIR_TREE = try DIR_TREE.init(allocator, TEST_DIR);
314316
defer DIR_TREE_INSTANCE.deinit(allocator);
315-
try DIR_TREE_INSTANCE.init(allocator, TEST_DIR);
316317

317318
var returned_dir_contents: STD.ArrayList([]const u8) = .empty;
318319
defer returned_dir_contents.deinit(allocator);
@@ -333,13 +334,12 @@ test "build and search solutions directory tree" {
333334
var mem_arena: STD.heap.ArenaAllocator = STD.heap.ArenaAllocator.init(STD.testing.allocator);
334335
defer mem_arena.deinit();
335336

336-
var allocator: STD.mem.Allocator = mem_arena.allocator();
337+
const ALLOCATOR: STD.mem.Allocator = mem_arena.allocator();
337338

338-
const DIR_TREE_INSTANCE: *DIR_TREE = try allocator.create(DIR_TREE);
339-
defer DIR_TREE_INSTANCE.deinit(allocator);
340-
try DIR_TREE_INSTANCE.init(allocator, ".patches/solutions");
339+
const DIR_TREE_INSTANCE: *DIR_TREE = try DIR_TREE.init(ALLOCATOR, ".patches/solutions");
340+
defer DIR_TREE_INSTANCE.deinit(ALLOCATOR);
341341

342-
try DIR_TREE_INSTANCE.iterateAndFilterTree(allocator, .{
342+
try DIR_TREE_INSTANCE.iterateAndFilterTree(ALLOCATOR, .{
343343
.is_debug_enabled = false,
344344
.include_filter = "src",
345345
});
@@ -393,4 +393,3 @@ test "run hello world sub process" {
393393
STD.mem.eql(u8, process_output.items, "hello world\n"),
394394
);
395395
}
396-

0 commit comments

Comments
 (0)