RefCountedSet: add some missing context delete callbacks

This commit is contained in:
Qwerasd
2024-07-08 22:15:31 -04:00
parent 31d5384920
commit a9aef11b4b

View File

@ -232,10 +232,16 @@ pub fn RefCountedSet(
// we're reusing the existing value in the set. This allows
// callers to clean up any resources associated with the value.
if (comptime @hasDecl(Context, "deleted")) ctx.deleted(value);
items[id].meta.ref += 1;
return id;
}
// Notify the context that the value is "deleted" if we return an
// error. This allows callers to clean up any resources associated
// with the value.
errdefer if (comptime @hasDecl(Context, "deleted")) ctx.deleted(value);
// If the item doesn't exist, we need an available ID.
if (self.next_id >= self.layout.cap) {
// Arbitrarily chosen, threshold for rehashing.
@ -279,6 +285,8 @@ pub fn RefCountedSet(
pub fn addWithIdContext(self: *Self, base: anytype, value: T, id: Id, ctx: Context) AddError!?Id {
const items = self.items.ptr(base);
assert(id > 0);
if (id < self.next_id) {
if (items[id].meta.ref == 0) {
self.deleteItem(base, id, ctx);
@ -291,6 +299,11 @@ pub fn RefCountedSet(
return if (added_id == id) null else added_id;
} else if (ctx.eql(value, items[id].value)) {
// Notify the context that the value is "deleted" because
// we're reusing the existing value in the set. This allows
// callers to clean up any resources associated with the value.
if (comptime @hasDecl(Context, "deleted")) ctx.deleted(value);
items[id].meta.ref += 1;
return null;
@ -501,6 +514,7 @@ pub fn RefCountedSet(
// we're reusing the existing value in the set. This allows
// callers to clean up any resources associated with the value.
if (comptime @hasDecl(Context, "deleted")) ctx.deleted(value);
return id;
}