input: binding set needs to clean up leader memory on manual put

This commit is contained in:
Mitchell Hashimoto
2024-08-15 19:55:24 -07:00
committed by Mitchell Hashimoto
parent 2bf20ec32c
commit bfb31c374e

View File

@ -1007,18 +1007,26 @@ pub const Set = struct {
const gop = try self.bindings.getOrPut(alloc, t); const gop = try self.bindings.getOrPut(alloc, t);
// If we have an existing binding for this trigger, we have to if (gop.found_existing) switch (gop.value_ptr.*) {
// update the reverse mapping to remove the old action. // If we have a leader we need to clean up the memory
if (gop.found_existing) { .leader => |s| {
const t_hash = t.hash(); s.deinit(alloc);
var it = self.reverse.iterator(); alloc.destroy(s);
while (it.next()) |reverse_entry| it: { },
if (t_hash == reverse_entry.value_ptr.hash()) {
self.reverse.removeByPtr(reverse_entry.key_ptr); // If we have an existing binding for this trigger, we have to
break :it; // update the reverse mapping to remove the old action.
.action, .action_unconsumed => {
const t_hash = t.hash();
var it = self.reverse.iterator();
while (it.next()) |reverse_entry| it: {
if (t_hash == reverse_entry.value_ptr.hash()) {
self.reverse.removeByPtr(reverse_entry.key_ptr);
break :it;
}
} }
} },
} };
gop.value_ptr.* = if (consumed) .{ gop.value_ptr.* = if (consumed) .{
.action = action, .action = action,
@ -1617,6 +1625,26 @@ test "set: parseAndPut sequence preserves reverse mapping" {
} }
} }
test "set: put overwrites sequence" {
const testing = std.testing;
const alloc = testing.allocator;
var s: Set = .{};
defer s.deinit(alloc);
try s.parseAndPut(alloc, "ctrl+a>b=new_window");
try s.put(alloc, .{
.mods = .{ .ctrl = true },
.key = .{ .translated = .a },
}, .{ .new_window = {} });
// Creates reverse mapping
{
const trigger = s.getTrigger(.{ .new_window = {} }).?;
try testing.expect(trigger.key.translated == .a);
}
}
test "set: maintains reverse mapping" { test "set: maintains reverse mapping" {
const testing = std.testing; const testing = std.testing;
const alloc = testing.allocator; const alloc = testing.allocator;