Compare commits

..

6 Commits

Author SHA1 Message Date
f742ee1a85 build.zig.zon: match the project name 2024-04-06 15:35:19 -07:00
7db55e9ac9 subscription: fix setCompletionCallback
This was not being tested, and it was really broken. Now it is being
tested and it is no longer broken.
2024-04-06 15:34:38 -07:00
73fccb4662 all: allow passing more types of pointers as callback userdata
By performing more pointer casting gymnastics, additional types of
pointer can be supported. For example, now const qualified pointers
can be passed through thanks to the use of constCast. Also, having
explicit ptrCast allows nested pointers (e.g. a pointer to a slice,
such as `*[]const u8`) to be passed as userdata as well (the compiler
refuses to coerce a pointer to a pointer to `?*anyopaque` for some
reason, I guess because maybe it's ambiguous somehow?) Hopefully this
extra casting does not appreciably reduce the compiler's ability to
catch real bugs (for example, on a 64-bit machine, ptrCast can convert
a *u64 into a **u64 because there is no alignment change).

Also, the `volatile` pointer specifier is still not supported.
`allowzero` pointers probably also have a problem. Those are both
extreme edge cases, however.

This was intended to work before but did not due to an oversight.
Specifically, because the userdata pointers are stored as ?*anyopaque,
which does *not* have the const qualifier, they must have their const
qualifiers also removed. This is safe because the thunk guarantees
that the consumer code never sees the non-const version of the
pointer, and the nats library itself does nothing except pass the
pointer through to the user callback.

The tests have been updated to ensure this case works. The examples
still use a mutable userdata pointer to show that that case also
works. More tests could be added for the sake of increased rigor, but
I don't think it adds much.
2024-04-06 15:34:38 -07:00
42d0b24710 thunk: fix slightly confusing compile errors
The error messages not on the slice detection code path should not be
talking about slices.
2024-04-06 15:01:03 -07:00
b17a3fba6c readme: update zig support status 2024-01-15 16:30:26 -08:00
4124b912eb build: update for zig-0.12.0-dev.2208+4debd4338
Incorporate various build API changes. Hopefully there won't be any
other major API changes before the 0.12.0 release.
2024-01-15 16:23:45 -08:00
4 changed files with 27 additions and 16 deletions

View File

@@ -22,7 +22,7 @@ Only tagged release versions of `nats.c` will be used. The current version of `n
# Zig Version Support
Since the language is still under active development, any written Zig code is a moving target. The plan is to support Zig `0.11.*` exclusively until the NATS library API has good coverage and is stabilized. At that point, if there are major breaking changes, a maintenance branch will be created, and master will probably move to track Zig master.
Since the language is still under active development, any written Zig code is a moving target. The master branch targets zig 0.12 development versions (though it is not guaranteed to work with all versions. Check the commit history for specific version updates). The `zig-0.11.x` branch targets the current stable zig release, 0.11.
# Using

View File

@@ -9,8 +9,9 @@ pub fn build(b: *std.Build) void {
const optimize = b.standardOptimizeOption(.{});
const nats = b.addModule("nats", .{
.source_file = .{ .path = "src/nats.zig" },
.root_source_file = .{ .path = "src/nats.zig" },
});
nats.addIncludePath(.{ .path = b.getInstallPath(.header, "") });
const nats_c = nats_build.nats_c_lib(b, .{
.name = "nats-c",
@@ -25,7 +26,7 @@ pub fn build(b: *std.Build) void {
.optimize = optimize,
});
tests.addModule("nats", nats);
tests.root_module.addImport("nats", nats);
tests.linkLibrary(nats_c);
const run_main_tests = b.addRunArtifact(tests);
@@ -42,7 +43,7 @@ pub fn build(b: *std.Build) void {
}
const ExampleOptions = struct {
target: std.zig.CrossTarget,
target: std.Build.ResolvedTarget,
optimize: std.builtin.OptimizeMode,
nats_module: *std.Build.Module,
nats_c: *std.Build.Step.Compile,
@@ -70,7 +71,7 @@ pub fn add_examples(b: *std.Build, options: ExampleOptions) void {
.optimize = .Debug,
});
ex_exe.addModule("nats", options.nats_module);
ex_exe.root_module.addImport("nats", options.nats_module);
ex_exe.linkLibrary(options.nats_c);
const install = b.addInstallArtifact(ex_exe, .{});

View File

@@ -1,10 +1,20 @@
.{
.name = "nats.zig",
.version = "0.0.1",
.paths = .{
"src",
"deps/nats.c/src",
"deps/nats.c/LICENSE",
"deps/protobuf-c",
"build.zig",
"nats-c.build.zig",
"build.zig.zon",
"LICENSE",
},
.dependencies = .{
.libressl = .{
.url = "https://github.com/epicyclic-dev/LibreSSL-portable/archive/4bbf9ad43fd5d56c8e15bc2e880aab7c4e49731b.tar.gz",
.hash = "1220282c6f64f531b9d07d5ed1959708822f4f8dc2486a7005be391c8f5cdf2a502a",
.url = "https://github.com/epicyclic-dev/LibreSSL-portable/archive/9f74aeb1d2f5db5c375a1040cbd2b9abfa2749d1.tar.gz",
.hash = "122092a200f7e27e90974013d7e5cd5ef27438f67016852b5244ea287018263e78dc",
},
},
}

View File

@@ -5,7 +5,7 @@ const std = @import("std");
const NatsCOptions = struct {
name: []const u8,
target: std.zig.CrossTarget,
target: std.Build.ResolvedTarget,
optimize: std.builtin.OptimizeMode,
};
@@ -24,36 +24,36 @@ pub fn nats_c_lib(
};
lib.linkLibC();
lib.addCSourceFiles(&common_sources, &cflags);
lib.addCSourceFiles(.{ .files = &common_sources, .flags = &cflags });
lib.addIncludePath(.{ .path = nats_src_prefix ++ "include" });
// if building with streaming support (protocol.pb-c.c includes
// <protobuf-c/protobuf-c.h>, unfortunately)
lib.addIncludePath(.{ .path = "deps" });
lib.addIncludePath(.{ .path = nats_src_prefix ++ "stan" });
lib.addCSourceFiles(&streaming_sources, &cflags);
lib.addCSourceFiles(&protobuf_c_sources, &cflags);
lib.addCSourceFiles(.{ .files = &streaming_sources, .flags = &cflags });
lib.addCSourceFiles(.{ .files = &protobuf_c_sources, .flags = &cflags });
const ssl_dep = b.dependency("libressl", .{
.target = options.target,
.optimize = options.optimize,
});
const tinfo = lib.target_info.target;
const tinfo = options.target.result;
switch (tinfo.os.tag) {
.windows => {
lib.addCSourceFiles(&win_sources, &cflags);
lib.addCSourceFiles(.{ .files = &win_sources, .flags = &cflags });
if (tinfo.abi != .msvc) {
lib.addCSourceFiles(&.{"src/win-crosshack.c"}, &cflags);
lib.addCSourceFiles(.{ .files = &.{"src/win-crosshack.c"}, .flags = &cflags });
}
lib.defineCMacro("_WIN32", null);
lib.linkSystemLibrary("ws2_32");
},
.macos => {
lib.addCSourceFiles(&unix_sources, &cflags);
lib.addCSourceFiles(.{ .files = &unix_sources, .flags = &cflags });
lib.defineCMacro("DARWIN", null);
},
else => {
lib.addCSourceFiles(&unix_sources, &cflags);
lib.addCSourceFiles(.{ .files = &unix_sources, .flags = &cflags });
lib.defineCMacro("_GNU_SOURCE", null);
lib.defineCMacro("LINUX", null);
// may need to link pthread and rt. Not sure if those are included with linkLibC