From d0904f568c3f4d6d3ed0c522d25ccc33f04ff2f8 Mon Sep 17 00:00:00 2001 From: 2wenty1ne Date: Mon, 11 Nov 2024 15:32:15 +0100 Subject: [PATCH] Removes unnecessary try and error union from return typ Added main function with basic functionality --- LList.zig | 46 +++++++++++++++++++++++++++++----------------- 1 file changed, 29 insertions(+), 17 deletions(-) diff --git a/LList.zig b/LList.zig index 64456a4..aa29b9d 100644 --- a/LList.zig +++ b/LList.zig @@ -12,13 +12,13 @@ pub fn LList(comptime T: type) type { counter: u64, - pub fn init(this: *Self, allocator: Allocator) !void { + pub fn init(this: *Self, allocator: Allocator) void { this.alloc = allocator; this.first = null; this.counter = 0; } - pub fn deinit(this: *Self) !void { + pub fn deinit(this: *Self) void { //? If there is no created Node if (this.first == null) { @@ -40,7 +40,7 @@ pub fn LList(comptime T: type) type { breakNext = true; } - try currentNode.deinit(); + currentNode.deinit(); currentNode = nextNode; @@ -48,7 +48,7 @@ pub fn LList(comptime T: type) type { break; } } - try currentNode.deinit(); + currentNode.deinit(); this.counter = 0; } @@ -64,7 +64,7 @@ pub fn LList(comptime T: type) type { return newNode; } - pub fn size(this: *Self) !u64{ + pub fn size(this: *Self) u64{ return this.counter; } @@ -89,44 +89,56 @@ pub fn Node(comptime T: type) type { } - pub fn deinit(this: *Self) !void { + pub fn deinit(this: *Self) void { this.alloc.destroy(this.value); this.alloc.destroy(this); } - - }; } +pub fn main() !void { + + var gpa = std.heap.GeneralPurposeAllocator(.{}){}; + const allocator = gpa.allocator(); + + var testList: LList(u64) = undefined; + testList.init(allocator); + + _ = try testList.add(10); + _ = try testList.add(20); + + testList.deinit(); +} + test "init()" { var testList: LList(u64) = undefined; - try testList.init(testingAllocator); + testList.init(testingAllocator); try expect(testList.first == null); try expect(testList.counter == 0); - try testList.deinit(); + testList.deinit(); } test "deinit()" { var testList: LList(u64) = undefined; - try testList.init(testingAllocator); + testList.init(testingAllocator); _ = try testList.add(1); _ = try testList.add(2); - try testList.deinit(); + testList.deinit(); } test "add()" { var testList: LList(u64) = undefined; - try testList.init(testingAllocator); + testList.init(testingAllocator); const firstNode = try testList.add(1); const secondNode = try testList.add(2); @@ -138,7 +150,7 @@ test "add()" { try expect(thirdNode.value.* == 3); try expect(fourtNode.value.* == 4); - try testList.deinit(); + testList.deinit(); } @@ -146,15 +158,15 @@ test "add()" { test "size()" { var testList: LList(u64) = undefined; - try testList.init(testingAllocator); + testList.init(testingAllocator); _ = try testList.add(1); _ = try testList.add(2); _ = try testList.add(3); _ = try testList.add(4); - try expect(try testList.size() == 4); + try expect(testList.size() == 4); - try testList.deinit(); + testList.deinit(); }