diff --git a/src/collection.zig b/src/collection.zig index 975b0ab..001264e 100644 --- a/src/collection.zig +++ b/src/collection.zig @@ -15,17 +15,13 @@ pub fn Collection(comptime T: anytype) type /// Initialize a new collection of values. /// Values are now owned by the collection and will free them when it is deinitialized. /// The allocator must be the one that manages the slice and its items. - pub fn init(allocator: std.mem.Allocator, values: []*T) !*Self + pub fn init(allocator: std.mem.Allocator, values: []*T) Self { - const self = try allocator.create(Self); - - self.* = .{ + return .{ .allocator = allocator, // Store given values in items slice. .items = values, }; - - return self; } /// Free any pointer value. @@ -108,9 +104,6 @@ pub fn Collection(comptime T: anytype) type // Free items slice. self.allocator.free(self.items); - - // Destroy the current collection. - self.allocator.destroy(self); } }; } diff --git a/tests/collection.zig b/tests/collection.zig index b5e1c93..dd50f50 100644 --- a/tests/collection.zig +++ b/tests/collection.zig @@ -37,7 +37,7 @@ test "simple collection" { slice[2] = try allocator.create(u8); // Create a collection with your slice of elements. - const collection = try zollections.Collection(u8).init(allocator, slice); + var collection = zollections.Collection(u8).init(allocator, slice); // Free your collection: your slice and all your elements will be freed. defer collection.deinit(); } @@ -60,7 +60,7 @@ test "recursive free" { slice[0] = slicePointer; // Create a collection with your slice of elements. - const collection = try zollections.Collection([]*u8).init(allocator, slice); + var collection = zollections.Collection([]*u8).init(allocator, slice); // Free your collection: your slice and all your slices and their elements will be freed. defer collection.deinit(); } @@ -79,7 +79,7 @@ test "custom struct deinit" { slice[2].* = try ExampleStruct.init(16); // Create a collection with your slice of elements. - const collection = try zollections.Collection(ExampleStruct).init(allocator, slice); + var collection = zollections.Collection(ExampleStruct).init(allocator, slice); // Free your collection: your slice and all your elements will be deinitialized and freed. defer collection.deinit(); }