From 31632abc24afb2c774aaea33b6c5b7249c3efb72 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sun, 3 Nov 2024 15:16:38 -0800 Subject: [PATCH] some small tweaks, mostly subjective --- src/iterator.zig | 42 ++++++++++++++++++------------------------ src/object.zig | 2 ++ 2 files changed, 20 insertions(+), 24 deletions(-) diff --git a/src/iterator.zig b/src/iterator.zig index af5238f..c28bfee 100644 --- a/src/iterator.zig +++ b/src/iterator.zig @@ -3,34 +3,30 @@ const objc = @import("main.zig"); // From . const NSFastEnumerationState = extern struct { - state: c_ulong, - itemsPtr: ?[*]objc.c.id, - mutationsPtr: ?*c_ulong, - extra: [5]c_ulong, + state: c_ulong = 0, + itemsPtr: ?[*]objc.c.id = null, + mutationsPtr: ?*c_ulong = null, + extra: [5]c_ulong = [_]c_ulong{0} ** 5, }; +/// An iterator that uses the fast enumeration protocol[1] to iterate over +/// objects in an Objective-C collection. This can be used with any object +/// that conforms to the `NSFastEnumeration` protocol. +/// +/// [1]: Nhttps://developer.apple.com/documentation/foundation/nsfastenumeration pub const Iterator = struct { object: objc.Object, sel: objc.Sel, - state: NSFastEnumerationState, - initial_mutations_value: ?c_ulong, + state: NSFastEnumerationState = .{}, + initial_mutations_value: ?c_ulong = null, // Clang compiles `for…in` loops with a size 16 buffer. - buffer: [16]objc.c.id, - slice: []objc.c.id, + buffer: [16]objc.c.id = [_]objc.c.id{null} ** 16, + slice: []const objc.c.id = &.{}, pub fn init(object: objc.Object) Iterator { return .{ .object = object, .sel = objc.sel("countByEnumeratingWithState:objects:count:"), - .state = .{ - .state = 0, - .itemsPtr = null, - .mutationsPtr = null, - .extra = [_]c_ulong{0} ** 5, - }, - .initial_mutations_value = null, - .buffer = [_]objc.c.id{null} ** 16, - .slice = &[_]objc.c.id{}, }; } @@ -54,13 +50,11 @@ pub const Iterator = struct { self.slice = self.state.itemsPtr.?[0..count]; } - if (self.slice.len > 0) { - const first = self.slice[0]; - self.slice = self.slice[1..]; - return objc.Object.fromId(first); - } else { - return null; - } + if (self.slice.len == 0) return null; + + const first = self.slice[0]; + self.slice = self.slice[1..]; + return objc.Object.fromId(first); } }; diff --git a/src/object.zig b/src/object.zig index 5f97fe4..2615502 100644 --- a/src/object.zig +++ b/src/object.zig @@ -106,6 +106,8 @@ pub const Object = struct { objc_release(self.value); } + /// Return an iterator for this object. The object must implement the + /// `NSFastEnumeration` protocol. pub fn iterate(self: Object) Iterator { return Iterator.init(self); }