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 { pub const Connection = struct {
ptr: c.virConnectPtr, ptr: c.virConnectPtr,
allocator: mem.Allocator, allocator: mem.Allocator,
@ -144,12 +153,10 @@ pub const Connection = struct {
} }
pub fn numOfPools(self: *const Connection) !u32 { pub fn numOfPools(self: *const Connection) !u32 {
const num = c.virConnectNumOfStoragePools(self.ptr); return numOf(c.virConnectPtr, self.ptr, c.virConnectNumOfStoragePools);
return if (num < 0) handleError() else @intCast(num);
} }
pub fn numOfDefinedPools(self: *const Connection) !u32 { pub fn numOfDefinedPools(self: *const Connection) !u32 {
const num = c.virConnectNumOfDefinedStoragePools(self.ptr); return numOf(c.virConnectPtr, self.ptr, c.virConnectNumOfDefinedStoragePools);
return if (num < 0) handleError() else @intCast(num);
} }
pub fn iteratePools(self: *const Connection, flags: []const Pool.ListFlags) !Pool.PoolIterator { 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 { pub fn numOfDomains(self: *const Connection) !u32 {
const num = c.virConnectNumOfDomains(self.ptr); return numOf(c.virConnectPtr, self.ptr, c.virConnectNumOfDomains);
return if (num < 0) handleError() else @intCast(num);
} }
pub fn numOfDefinedDomains(self: *const Connection) !u32 { pub fn numOfDefinedDomains(self: *const Connection) !u32 {
const num = c.virConnectNumOfDefinedDomains(self.ptr); return numOf(c.virConnectPtr, self.ptr, c.virConnectNumOfDefinedDomains);
return if (num < 0) handleError() else @intCast(num);
} }
pub fn iterateDomains(self: *const Connection, flags: []const Domain.ListFlags) !Domain.DomainIterator { pub fn iterateDomains(self: *const Connection, flags: []const Domain.ListFlags) !Domain.DomainIterator {