Do not enforce pointer usage for collections.

This commit is contained in:
Madeorsk 2024-10-15 21:48:24 +02:00
parent 6dc68be101
commit f0f6c32267
Signed by: Madeorsk
GPG key ID: 677E51CA765BB79F
2 changed files with 5 additions and 12 deletions

View file

@ -15,17 +15,13 @@ pub fn Collection(comptime T: anytype) type
/// Initialize a new collection of values. /// Initialize a new collection of values.
/// Values are now owned by the collection and will free them when it is deinitialized. /// 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. /// 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); return .{
self.* = .{
.allocator = allocator, .allocator = allocator,
// Store given values in items slice. // Store given values in items slice.
.items = values, .items = values,
}; };
return self;
} }
/// Free any pointer value. /// Free any pointer value.
@ -108,9 +104,6 @@ pub fn Collection(comptime T: anytype) type
// Free items slice. // Free items slice.
self.allocator.free(self.items); self.allocator.free(self.items);
// Destroy the current collection.
self.allocator.destroy(self);
} }
}; };
} }

View file

@ -37,7 +37,7 @@ test "simple collection" {
slice[2] = try allocator.create(u8); slice[2] = try allocator.create(u8);
// Create a collection with your slice of elements. // 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. // Free your collection: your slice and all your elements will be freed.
defer collection.deinit(); defer collection.deinit();
} }
@ -60,7 +60,7 @@ test "recursive free" {
slice[0] = slicePointer; slice[0] = slicePointer;
// Create a collection with your slice of elements. // 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. // Free your collection: your slice and all your slices and their elements will be freed.
defer collection.deinit(); defer collection.deinit();
} }
@ -79,7 +79,7 @@ test "custom struct deinit" {
slice[2].* = try ExampleStruct.init(16); slice[2].* = try ExampleStruct.init(16);
// Create a collection with your slice of elements. // 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. // Free your collection: your slice and all your elements will be deinitialized and freed.
defer collection.deinit(); defer collection.deinit();
} }