zig libvirt: implement numOf helper

This commit is contained in:
Jeeves 2024-06-25 07:17:56 -06:00
parent 454c89c20a
commit 05969155df

View file

@ -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 {