authorgravatar for motiejus@jakstys.ltMotiejus Jakštys <motiejus@jakstys.lt> 2022-02-27 22:24:00+02:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2022-02-27 15:24:00-05:00
logc03b733f0999418b234067cac51eac0e4412dfe1
treebabffc6d7495f74b75d06be22dd39503df07c7ad
parent4d658f83ed3893cf950615e9f58c2b533525b71d
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

std.HashMap: return explicit errors (#11000)

All errors from std.HashMap are allocation errors. Mark them as such. This is helpful when one wants to return explicit errors where HashMap is used.

1 files changed, 33 insertions(+), 33 deletions(-)

lib/std/hash_map.zig+33-33
......@@ -473,7 +473,7 @@ pub fn HashMap(
473473 /// Otherwise, puts a new item with undefined value, and
474474 /// the `Entry` pointers point to it. Caller should then initialize
475475 /// the value (but not the key).
476 pub fn getOrPut(self: *Self, key: K) !GetOrPutResult {
476 pub fn getOrPut(self: *Self, key: K) Allocator.Error!GetOrPutResult {
477477 return self.unmanaged.getOrPutContext(self.allocator, key, self.ctx);
478478 }
479479
......@@ -483,7 +483,7 @@ pub fn HashMap(
483483 /// Otherwise, puts a new item with undefined key and value, and
484484 /// the `Entry` pointers point to it. Caller must then initialize
485485 /// the key and value.
486 pub fn getOrPutAdapted(self: *Self, key: anytype, ctx: anytype) !GetOrPutResult {
486 pub fn getOrPutAdapted(self: *Self, key: anytype, ctx: anytype) Allocator.Error!GetOrPutResult {
487487 return self.unmanaged.getOrPutContextAdapted(self.allocator, key, ctx, self.ctx);
488488 }
489489
......@@ -509,7 +509,7 @@ pub fn HashMap(
509509 return self.unmanaged.getOrPutAssumeCapacityAdapted(self.allocator, key, ctx);
510510 }
511511
512 pub fn getOrPutValue(self: *Self, key: K, value: V) !Entry {
512 pub fn getOrPutValue(self: *Self, key: K, value: V) Allocator.Error!Entry {
513513 return self.unmanaged.getOrPutValueContext(self.allocator, key, value, self.ctx);
514514 }
515515
......@@ -517,14 +517,14 @@ pub fn HashMap(
517517
518518 /// Increases capacity, guaranteeing that insertions up until the
519519 /// `expected_count` will not cause an allocation, and therefore cannot fail.
520 pub fn ensureTotalCapacity(self: *Self, expected_count: Size) !void {
520 pub fn ensureTotalCapacity(self: *Self, expected_count: Size) Allocator.Error!void {
521521 return self.unmanaged.ensureTotalCapacityContext(self.allocator, expected_count, self.ctx);
522522 }
523523
524524 /// Increases capacity, guaranteeing that insertions up until
525525 /// `additional_count` **more** items will not cause an allocation, and
526526 /// therefore cannot fail.
527 pub fn ensureUnusedCapacity(self: *Self, additional_count: Size) !void {
527 pub fn ensureUnusedCapacity(self: *Self, additional_count: Size) Allocator.Error!void {
528528 return self.unmanaged.ensureUnusedCapacityContext(self.allocator, additional_count, self.ctx);
529529 }
530530
......@@ -536,13 +536,13 @@ pub fn HashMap(
536536
537537 /// Clobbers any existing data. To detect if a put would clobber
538538 /// existing data, see `getOrPut`.
539 pub fn put(self: *Self, key: K, value: V) !void {
539 pub fn put(self: *Self, key: K, value: V) Allocator.Error!void {
540540 return self.unmanaged.putContext(self.allocator, key, value, self.ctx);
541541 }
542542
543543 /// Inserts a key-value pair into the hash map, asserting that no previous
544544 /// entry with the same key is already present
545 pub fn putNoClobber(self: *Self, key: K, value: V) !void {
545 pub fn putNoClobber(self: *Self, key: K, value: V) Allocator.Error!void {
546546 return self.unmanaged.putNoClobberContext(self.allocator, key, value, self.ctx);
547547 }
548548
......@@ -561,7 +561,7 @@ pub fn HashMap(
561561 }
562562
563563 /// Inserts a new `Entry` into the hash map, returning the previous one, if any.
564 pub fn fetchPut(self: *Self, key: K, value: V) !?KV {
564 pub fn fetchPut(self: *Self, key: K, value: V) Allocator.Error!?KV {
565565 return self.unmanaged.fetchPutContext(self.allocator, key, value, self.ctx);
566566 }
567567
......@@ -647,19 +647,19 @@ pub fn HashMap(
647647 }
648648
649649 /// Creates a copy of this map, using the same allocator
650 pub fn clone(self: Self) !Self {
650 pub fn clone(self: Self) Allocator.Error!Self {
651651 var other = try self.unmanaged.cloneContext(self.allocator, self.ctx);
652652 return other.promoteContext(self.allocator, self.ctx);
653653 }
654654
655655 /// Creates a copy of this map, using a specified allocator
656 pub fn cloneWithAllocator(self: Self, new_allocator: Allocator) !Self {
656 pub fn cloneWithAllocator(self: Self, new_allocator: Allocator) Allocator.Error!Self {
657657 var other = try self.unmanaged.cloneContext(new_allocator, self.ctx);
658658 return other.promoteContext(new_allocator, self.ctx);
659659 }
660660
661661 /// Creates a copy of this map, using a specified context
662 pub fn cloneWithContext(self: Self, new_ctx: anytype) !HashMap(K, V, @TypeOf(new_ctx), max_load_percentage) {
662 pub fn cloneWithContext(self: Self, new_ctx: anytype) Allocator.Error!HashMap(K, V, @TypeOf(new_ctx), max_load_percentage) {
663663 var other = try self.unmanaged.cloneContext(self.allocator, new_ctx);
664664 return other.promoteContext(self.allocator, new_ctx);
665665 }
......@@ -669,7 +669,7 @@ pub fn HashMap(
669669 self: Self,
670670 new_allocator: Allocator,
671671 new_ctx: anytype,
672 ) !HashMap(K, V, @TypeOf(new_ctx), max_load_percentage) {
672 ) Allocator.Error!HashMap(K, V, @TypeOf(new_ctx), max_load_percentage) {
673673 var other = try self.unmanaged.cloneContext(new_allocator, new_ctx);
674674 return other.promoteContext(new_allocator, new_ctx);
675675 }
......@@ -896,20 +896,20 @@ pub fn HashMapUnmanaged(
896896
897897 pub const ensureCapacity = @compileError("deprecated; call `ensureUnusedCapacity` or `ensureTotalCapacity`");
898898
899 pub fn ensureTotalCapacity(self: *Self, allocator: Allocator, new_size: Size) !void {
899 pub fn ensureTotalCapacity(self: *Self, allocator: Allocator, new_size: Size) Allocator.Error!void {
900900 if (@sizeOf(Context) != 0)
901901 @compileError("Cannot infer context " ++ @typeName(Context) ++ ", call ensureTotalCapacityContext instead.");
902902 return ensureTotalCapacityContext(self, allocator, new_size, undefined);
903903 }
904 pub fn ensureTotalCapacityContext(self: *Self, allocator: Allocator, new_size: Size, ctx: Context) !void {
904 pub fn ensureTotalCapacityContext(self: *Self, allocator: Allocator, new_size: Size, ctx: Context) Allocator.Error!void {
905905 if (new_size > self.size)
906906 try self.growIfNeeded(allocator, new_size - self.size, ctx);
907907 }
908908
909 pub fn ensureUnusedCapacity(self: *Self, allocator: Allocator, additional_size: Size) !void {
909 pub fn ensureUnusedCapacity(self: *Self, allocator: Allocator, additional_size: Size) Allocator.Error!void {
910910 return ensureUnusedCapacityContext(self, allocator, additional_size, undefined);
911911 }
912 pub fn ensureUnusedCapacityContext(self: *Self, allocator: Allocator, additional_size: Size, ctx: Context) !void {
912 pub fn ensureUnusedCapacityContext(self: *Self, allocator: Allocator, additional_size: Size, ctx: Context) Allocator.Error!void {
913913 return ensureTotalCapacityContext(self, allocator, self.count() + additional_size, ctx);
914914 }
915915
......@@ -986,12 +986,12 @@ pub fn HashMapUnmanaged(
986986 }
987987
988988 /// Insert an entry in the map. Assumes it is not already present.
989 pub fn putNoClobber(self: *Self, allocator: Allocator, key: K, value: V) !void {
989 pub fn putNoClobber(self: *Self, allocator: Allocator, key: K, value: V) Allocator.Error!void {
990990 if (@sizeOf(Context) != 0)
991991 @compileError("Cannot infer context " ++ @typeName(Context) ++ ", call putNoClobberContext instead.");
992992 return self.putNoClobberContext(allocator, key, value, undefined);
993993 }
994 pub fn putNoClobberContext(self: *Self, allocator: Allocator, key: K, value: V, ctx: Context) !void {
994 pub fn putNoClobberContext(self: *Self, allocator: Allocator, key: K, value: V, ctx: Context) Allocator.Error!void {
995995 assert(!self.containsContext(key, ctx));
996996 try self.growIfNeeded(allocator, 1, ctx);
997997
......@@ -1043,12 +1043,12 @@ pub fn HashMapUnmanaged(
10431043 }
10441044
10451045 /// Inserts a new `Entry` into the hash map, returning the previous one, if any.
1046 pub fn fetchPut(self: *Self, allocator: Allocator, key: K, value: V) !?KV {
1046 pub fn fetchPut(self: *Self, allocator: Allocator, key: K, value: V) Allocator.Error!?KV {
10471047 if (@sizeOf(Context) != 0)
10481048 @compileError("Cannot infer context " ++ @typeName(Context) ++ ", call fetchPutContext instead.");
10491049 return self.fetchPutContext(allocator, key, value, undefined);
10501050 }
1051 pub fn fetchPutContext(self: *Self, allocator: Allocator, key: K, value: V, ctx: Context) !?KV {
1051 pub fn fetchPutContext(self: *Self, allocator: Allocator, key: K, value: V, ctx: Context) Allocator.Error!?KV {
10521052 const gop = try self.getOrPutContext(allocator, key, ctx);
10531053 var result: ?KV = null;
10541054 if (gop.found_existing) {
......@@ -1182,12 +1182,12 @@ pub fn HashMapUnmanaged(
11821182 }
11831183
11841184 /// Insert an entry if the associated key is not already present, otherwise update preexisting value.
1185 pub fn put(self: *Self, allocator: Allocator, key: K, value: V) !void {
1185 pub fn put(self: *Self, allocator: Allocator, key: K, value: V) Allocator.Error!void {
11861186 if (@sizeOf(Context) != 0)
11871187 @compileError("Cannot infer context " ++ @typeName(Context) ++ ", call putContext instead.");
11881188 return self.putContext(allocator, key, value, undefined);
11891189 }
1190 pub fn putContext(self: *Self, allocator: Allocator, key: K, value: V, ctx: Context) !void {
1190 pub fn putContext(self: *Self, allocator: Allocator, key: K, value: V, ctx: Context) Allocator.Error!void {
11911191 const result = try self.getOrPutContext(allocator, key, ctx);
11921192 result.value_ptr.* = value;
11931193 }
......@@ -1256,24 +1256,24 @@ pub fn HashMapUnmanaged(
12561256 return null;
12571257 }
12581258
1259 pub fn getOrPut(self: *Self, allocator: Allocator, key: K) !GetOrPutResult {
1259 pub fn getOrPut(self: *Self, allocator: Allocator, key: K) Allocator.Error!GetOrPutResult {
12601260 if (@sizeOf(Context) != 0)
12611261 @compileError("Cannot infer context " ++ @typeName(Context) ++ ", call getOrPutContext instead.");
12621262 return self.getOrPutContext(allocator, key, undefined);
12631263 }
1264 pub fn getOrPutContext(self: *Self, allocator: Allocator, key: K, ctx: Context) !GetOrPutResult {
1264 pub fn getOrPutContext(self: *Self, allocator: Allocator, key: K, ctx: Context) Allocator.Error!GetOrPutResult {
12651265 const gop = try self.getOrPutContextAdapted(allocator, key, ctx, ctx);
12661266 if (!gop.found_existing) {
12671267 gop.key_ptr.* = key;
12681268 }
12691269 return gop;
12701270 }
1271 pub fn getOrPutAdapted(self: *Self, allocator: Allocator, key: anytype, key_ctx: anytype) !GetOrPutResult {
1271 pub fn getOrPutAdapted(self: *Self, allocator: Allocator, key: anytype, key_ctx: anytype) Allocator.Error!GetOrPutResult {
12721272 if (@sizeOf(Context) != 0)
12731273 @compileError("Cannot infer context " ++ @typeName(Context) ++ ", call getOrPutContextAdapted instead.");
12741274 return self.getOrPutContextAdapted(allocator, key, key_ctx, undefined);
12751275 }
1276 pub fn getOrPutContextAdapted(self: *Self, allocator: Allocator, key: anytype, key_ctx: anytype, ctx: Context) !GetOrPutResult {
1276 pub fn getOrPutContextAdapted(self: *Self, allocator: Allocator, key: anytype, key_ctx: anytype, ctx: Context) Allocator.Error!GetOrPutResult {
12771277 self.growIfNeeded(allocator, 1, ctx) catch |err| {
12781278 // If allocation fails, try to do the lookup anyway.
12791279 // If we find an existing item, we can return it.
......@@ -1367,12 +1367,12 @@ pub fn HashMapUnmanaged(
13671367 };
13681368 }
13691369
1370 pub fn getOrPutValue(self: *Self, allocator: Allocator, key: K, value: V) !Entry {
1370 pub fn getOrPutValue(self: *Self, allocator: Allocator, key: K, value: V) Allocator.Error!Entry {
13711371 if (@sizeOf(Context) != 0)
13721372 @compileError("Cannot infer context " ++ @typeName(Context) ++ ", call getOrPutValueContext instead.");
13731373 return self.getOrPutValueContext(allocator, key, value, undefined);
13741374 }
1375 pub fn getOrPutValueContext(self: *Self, allocator: Allocator, key: K, value: V, ctx: Context) !Entry {
1375 pub fn getOrPutValueContext(self: *Self, allocator: Allocator, key: K, value: V, ctx: Context) Allocator.Error!Entry {
13761376 const res = try self.getOrPutAdapted(allocator, key, ctx);
13771377 if (!res.found_existing) {
13781378 res.key_ptr.* = key;
......@@ -1450,18 +1450,18 @@ pub fn HashMapUnmanaged(
14501450 return @truncate(Size, max_load - self.available);
14511451 }
14521452
1453 fn growIfNeeded(self: *Self, allocator: Allocator, new_count: Size, ctx: Context) !void {
1453 fn growIfNeeded(self: *Self, allocator: Allocator, new_count: Size, ctx: Context) Allocator.Error!void {
14541454 if (new_count > self.available) {
14551455 try self.grow(allocator, capacityForSize(self.load() + new_count), ctx);
14561456 }
14571457 }
14581458
1459 pub fn clone(self: Self, allocator: Allocator) !Self {
1459 pub fn clone(self: Self, allocator: Allocator) Allocator.Error!Self {
14601460 if (@sizeOf(Context) != 0)
14611461 @compileError("Cannot infer context " ++ @typeName(Context) ++ ", call cloneContext instead.");
14621462 return self.cloneContext(allocator, @as(Context, undefined));
14631463 }
1464 pub fn cloneContext(self: Self, allocator: Allocator, new_ctx: anytype) !HashMapUnmanaged(K, V, @TypeOf(new_ctx), max_load_percentage) {
1464 pub fn cloneContext(self: Self, allocator: Allocator, new_ctx: anytype) Allocator.Error!HashMapUnmanaged(K, V, @TypeOf(new_ctx), max_load_percentage) {
14651465 var other = HashMapUnmanaged(K, V, @TypeOf(new_ctx), max_load_percentage){};
14661466 if (self.size == 0)
14671467 return other;
......@@ -1486,7 +1486,7 @@ pub fn HashMapUnmanaged(
14861486 return other;
14871487 }
14881488
1489 fn grow(self: *Self, allocator: Allocator, new_capacity: Size, ctx: Context) !void {
1489 fn grow(self: *Self, allocator: Allocator, new_capacity: Size, ctx: Context) Allocator.Error!void {
14901490 @setCold(true);
14911491 const new_cap = std.math.max(new_capacity, minimal_capacity);
14921492 assert(new_cap > self.capacity());
......@@ -1517,7 +1517,7 @@ pub fn HashMapUnmanaged(
15171517 std.mem.swap(Self, self, &map);
15181518 }
15191519
1520 fn allocate(self: *Self, allocator: Allocator, new_capacity: Size) !void {
1520 fn allocate(self: *Self, allocator: Allocator, new_capacity: Size) Allocator.Error!void {
15211521 const header_align = @alignOf(Header);
15221522 const key_align = if (@sizeOf(K) == 0) 1 else @alignOf(K);
15231523 const val_align = if (@sizeOf(V) == 0) 1 else @alignOf(V);