diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2024-11-27 13:53:39 -0800 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2024-11-27 14:34:08 -0800 |
| commit | c9c7ede2f9be23fd212d968fbc2fdec6ebefdeeb (patch) | |
| tree | 2e60e8e3b08d0840e350d3d377b95d94d49f2d9d | |
| parent | 1a99c99ee99fa3ca43935fe7d7d0865145b82d4e (diff) | |
| download | zig-c9c7ede2f9be23fd212d968fbc2fdec6ebefdeeb.tar.gz zig-c9c7ede2f9be23fd212d968fbc2fdec6ebefdeeb.zip | |
introduce std.ArrayHashMap.reinit
| -rw-r--r-- | lib/std/array_hash_map.zig | 8 |
1 files changed, 6 insertions, 2 deletions
diff --git a/lib/std/array_hash_map.zig b/lib/std/array_hash_map.zig index bf411b3798..b8f549a8a8 100644 --- a/lib/std/array_hash_map.zig +++ b/lib/std/array_hash_map.zig @@ -621,15 +621,19 @@ pub fn ArrayHashMapUnmanaged( pub fn init(allocator: Allocator, key_list: []const K, value_list: []const V) !Self { var self: Self = .{}; + errdefer self.deinit(allocator); + try self.reinit(allocator, key_list, value_list); + return self; + } + + pub fn reinit(self: *Self, allocator: Allocator, key_list: []const K, value_list: []const V) !void { try self.entries.resize(allocator, key_list.len); - errdefer self.entries.deinit(allocator); @memcpy(self.keys(), key_list); if (@sizeOf(V) != 0) { assert(key_list.len == value_list.len); @memcpy(self.values(), value_list); } try self.reIndex(allocator); - return self; } /// Frees the backing allocation and leaves the map in an undefined state. |
