input: Binding.Set.clone handles leaders

This commit is contained in:
Mitchell Hashimoto
2024-08-15 20:00:12 -07:00
committed by Mitchell Hashimoto
parent bfb31c374e
commit 9944f5d34d
2 changed files with 27 additions and 7 deletions

View File

@ -3317,13 +3317,7 @@ pub const Keybinds = struct {
/// Deep copy of the struct. Required by Config. /// Deep copy of the struct. Required by Config.
pub fn clone(self: *const Keybinds, alloc: Allocator) !Keybinds { pub fn clone(self: *const Keybinds, alloc: Allocator) !Keybinds {
// TODO: leaders return .{ .set = try self.set.clone(alloc) };
return .{
.set = .{
.bindings = try self.set.bindings.clone(alloc),
.reverse = try self.set.reverse.clone(alloc),
},
};
} }
/// Compare if two of our value are requal. Required by Config. /// Compare if two of our value are requal. Required by Config.

View File

@ -1089,6 +1089,32 @@ pub const Set = struct {
} }
} }
/// Deep clone the set.
pub fn clone(self: *const Set, alloc: Allocator) !Set {
var result: Set = .{
.bindings = try self.bindings.clone(alloc),
.reverse = try self.reverse.clone(alloc),
};
// If we have any leaders we need to clone them.
var it = result.bindings.iterator();
while (it.next()) |entry| switch (entry.value_ptr.*) {
// No data to clone
.action, .action_unconsumed => {},
// Must be deep cloned.
.leader => |*s| {
const ptr = try alloc.create(Set);
errdefer alloc.destroy(ptr);
ptr.* = try s.*.clone(alloc);
errdefer ptr.deinit(alloc);
s.* = ptr;
},
};
return result;
}
/// The hash map context for the set. This defines how the hash map /// The hash map context for the set. This defines how the hash map
/// gets the hash key and checks for equality. /// gets the hash key and checks for equality.
fn Context(comptime KeyType: type) type { fn Context(comptime KeyType: type) type {