From 05969155df01013eea11b5680be8774f7e41f818 Mon Sep 17 00:00:00 2001 From: Jeeves Date: Tue, 25 Jun 2024 07:17:56 -0600 Subject: [PATCH] zig libvirt: implement numOf helper --- server/src/libvirt.zig | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/server/src/libvirt.zig b/server/src/libvirt.zig index 5c2e1f2..df1b7a3 100644 --- a/server/src/libvirt.zig +++ b/server/src/libvirt.zig @@ -116,6 +116,15 @@ fn Iterator(comptime T: type, comptime Ptr: type, comptime freeFn: *const fn (Pt }; } +fn numOf( + comptime P: type, + ptr: P, + fnPtr: *const fn (P) callconv(.C) c_int, +) !u32 { + const num = fnPtr(ptr); + return if (num < 0) handleError() else @intCast(num); +} + pub const Connection = struct { ptr: c.virConnectPtr, allocator: mem.Allocator, @@ -144,12 +153,10 @@ pub const Connection = struct { } pub fn numOfPools(self: *const Connection) !u32 { - const num = c.virConnectNumOfStoragePools(self.ptr); - return if (num < 0) handleError() else @intCast(num); + return numOf(c.virConnectPtr, self.ptr, c.virConnectNumOfStoragePools); } pub fn numOfDefinedPools(self: *const Connection) !u32 { - const num = c.virConnectNumOfDefinedStoragePools(self.ptr); - return if (num < 0) handleError() else @intCast(num); + return numOf(c.virConnectPtr, self.ptr, c.virConnectNumOfDefinedStoragePools); } pub fn iteratePools(self: *const Connection, flags: []const Pool.ListFlags) !Pool.PoolIterator { @@ -174,13 +181,10 @@ pub const Connection = struct { } pub fn numOfDomains(self: *const Connection) !u32 { - const num = c.virConnectNumOfDomains(self.ptr); - return if (num < 0) handleError() else @intCast(num); + return numOf(c.virConnectPtr, self.ptr, c.virConnectNumOfDomains); } - pub fn numOfDefinedDomains(self: *const Connection) !u32 { - const num = c.virConnectNumOfDefinedDomains(self.ptr); - return if (num < 0) handleError() else @intCast(num); + return numOf(c.virConnectPtr, self.ptr, c.virConnectNumOfDefinedDomains); } pub fn iterateDomains(self: *const Connection, flags: []const Domain.ListFlags) !Domain.DomainIterator {