Skip to content

Commit

Permalink
collections: add helpers to convert List to zig slice (#96)
Browse files Browse the repository at this point in the history
Add `pgzx.listItemsOf` and `pgzx.collections.list.List.itemsOf`
convenience helpers to make it easier to convert a postgres `List` to a
Zig based typed slice.
  • Loading branch information
urso authored Oct 7, 2024
1 parent cefe15e commit a6e363a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/pgzx.zig
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub const bgworker = @import("pgzx/bgworker.zig");

pub const collections = @import("pgzx/collections.zig");
pub const PointerListOf = collections.list.PointerListOf;
pub const listItemsOf = collections.list.listItemsOf;
pub const SList = collections.slist.SList;
pub const DList = collections.dlist.DList;
pub const HTab = collections.htab.HTab;
Expand Down
8 changes: 8 additions & 0 deletions src/pgzx/collections/list.zig
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ pub fn PointerListOf(comptime T: type) type {
return Self{ .list = from };
}

pub fn itemsOf(list: ?*pg.List) []*T {
return Self.initFrom(list).items();
}

pub fn init1(v: *T) Self {
return Self.initFrom(pg.list_make1_impl(
pg.T_List,
Expand Down Expand Up @@ -213,6 +217,10 @@ pub fn PointerListOf(comptime T: type) type {
};
}

pub fn listItemsOf(comptime T: type, list: ?*pg.List) []*T {
return PointerListOf(T).initFrom(list).items();
}

pub fn IteratorOf(comptime T: type) type {
return IteratorOfWith(T, pg.list_head, pg.lnext);
}
Expand Down

0 comments on commit a6e363a

Please sign in to comment.