Skip to content

revision for 0.12.0 stable release #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions build.zig
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
const std = @import("std");

pub fn build(b: *std.Build) void {
_ = b.addModule("async", .{
.root_source_file = .{ .path = "src/main.zig" },
});

const main_tests = b.addTest(.{
.root_source_file = .{ .path = "src/main.zig" },
});
Expand Down
12 changes: 12 additions & 0 deletions build.zig.zon
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
.{
.name = "async",
.version = "0.1.0",
.dependencies = .{},
.paths = .{
"README.md",
"LICENSE",
"build.zig",
"build.zig.zon",
"src",
},
}
6 changes: 3 additions & 3 deletions src/channel.zig
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const std = @import("std");
const testing = std.testing;
const trait = std.meta.trait;
const trait = std.meta;

/// Communication channel between threads
pub fn Channel(comptime T: type) type {
Expand All @@ -24,7 +24,7 @@ pub fn Channel(comptime T: type) type {

pub fn deinit(self: *Self) void {
while (self.fifo.readItem()) |elem| {
if (comptime trait.hasFn("deinit")(T)) {
if (comptime trait.hasFn(T, "deinit")) {
elem.deinit(); // Destroy data when possible
}
}
Expand All @@ -46,7 +46,7 @@ pub fn Channel(comptime T: type) type {

pub fn deinit(self: PopResult) void {
for (self.elements.items) |*data| {
if (comptime trait.hasFn("deinit")(T)) {
if (comptime trait.hasFn(T, "deinit")) {
data.deinit(); // Destroy data when possible
}
}
Expand Down
31 changes: 23 additions & 8 deletions src/task.zig
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const std = @import("std");
const testing = std.testing;
const trait = std.meta.trait;
const trait = std.meta;
const builtin = @import("builtin");

/// Represent a value returned by async task in the future.
Expand Down Expand Up @@ -31,25 +31,25 @@ pub fn Future(comptime T: type) type {
// Only detect one layer of optional/error-union
switch (@typeInfo(T)) {
.Optional => |info| {
if (comptime trait.isSingleItemPtr(info.child) and
trait.hasFn("deinit")(@typeInfo(info.child).Pointer.child))
if (comptime isSingleItemPtr(info.child) and
trait.hasFn(@typeInfo(info.child), "deinit"))
{
if (data) |d| d.deinit();
} else if (comptime trait.hasFn("deinit")(info.child)) {
} else if (comptime trait.hasFn(info.child, "deinit")) {
if (data) |d| d.deinit();
}
},
.ErrorUnion => |info| {
if (comptime trait.isSingleItemPtr(info.payload) and
trait.hasFn("deinit")(@typeInfo(info.payload).Pointer.child))
if (comptime isSingleItemPtr(info.payload) and
trait.hasFn(@typeInfo(info.payload).Pointer.child, "deinit"))
{
if (data) |d| d.deinit() else |_| {}
} else if (comptime trait.hasFn("deinit")(info.payload)) {
} else if (comptime trait.hasFn(info.payload, "deinit")) {
if (data) |d| d.deinit() else |_| {}
}
},
else => {
if (comptime trait.hasFn("deinit")(T)) {
if (comptime trait.hasFn(T, "deinit")) {
data.deinit();
}
},
Expand Down Expand Up @@ -105,6 +105,13 @@ pub fn Task(comptime fun: anytype) type {
};
}

pub fn isSingleItemPtr(comptime T: type) bool {
if (comptime @typeInfo(T) == .Pointer) {
return @typeInfo(T).Pointer.size == .One;
}
return false;
}

test "Async Task" {
if (builtin.single_threaded) return error.SkipZigTest;

Expand Down Expand Up @@ -217,3 +224,11 @@ test "Async Task" {
for (fs) |f| f.deinit();
}
}

test "isSingleItemPtr" {
const array = [_]u8{0} ** 10;
comptime try testing.expect(isSingleItemPtr(@TypeOf(&array[0])));
comptime try testing.expect(!isSingleItemPtr(@TypeOf(array)));
const runtime_zero: usize = 0;
try testing.expect(!isSingleItemPtr(@TypeOf(array[runtime_zero..1])));
}