Compare commits

...

2 Commits

Author SHA1 Message Date
226d678e68
examples: make request_reply slightly more interesting 2023-08-24 20:23:19 -07:00
0dac8402cf
thunks: also aligncast userdata
This is probably the sort of thing that I wouldn't actually think of
having explicit test coverage for if I hadn't accidentally ran across
it while fiddling. I will compensate for this easily by not actually
writing tests at all.
2023-08-24 20:23:19 -07:00
4 changed files with 14 additions and 14 deletions

View File

@ -2,7 +2,7 @@ const std = @import("std");
const nats = @import("nats");
fn onMessage(
userdata: *bool,
userdata: *u32,
connection: *nats.Connection,
subscription: *nats.Subscription,
message: *nats.Message,
@ -18,18 +18,18 @@ fn onMessage(
connection.publish(reply, "salutations") catch @panic("HELP");
}
userdata.* = true;
userdata.* += 1;
}
pub fn main() !void {
const connection = try nats.Connection.connectTo(nats.default_server_url);
defer connection.destroy();
var done = false;
const subscription = try connection.subscribe(bool, "channel", onMessage, &done);
var count: u32 = 0;
const subscription = try connection.subscribe(u32, "channel", onMessage, &count);
defer subscription.destroy();
while (!done) {
while (count < 10) : (nats.sleep(1000)) {
const reply = try connection.request("channel", "greetings", 1000);
defer reply.destroy();

View File

@ -893,7 +893,7 @@ pub fn makeConnectionCallbackThunk(
return struct {
fn thunk(conn: ?*nats_c.natsConnection, userdata: ?*anyopaque) callconv(.C) void {
const connection: *Connection = if (conn) |c| @ptrCast(c) else unreachable;
const data: *T = if (userdata) |u| @ptrCast(u) else unreachable;
const data: *T = if (userdata) |u| @alignCast(@ptrCast(u)) else unreachable;
callback(connection, data);
}
}.thunk;
@ -916,7 +916,7 @@ pub fn makeReconnectDelayCallbackThunk(
userdata: ?*anyopaque,
) callconv(.C) i64 {
const connection: *Connection = if (conn) |c| @ptrCast(c) else unreachable;
const data: *T = if (userdata) |u| @ptrCast(u) else unreachable;
const data: *T = if (userdata) |u| @alignCast(@ptrCast(u)) else unreachable;
return callback(connection, attempts, data);
}
}.thunk;
@ -946,7 +946,7 @@ pub fn makeErrorHandlerCallbackThunk(
) callconv(.C) void {
const connection: *Connection = if (conn) |c| @ptrCast(c) else unreachable;
const subscription: *Subscription = if (sub) |s| @ptrCast(s) else unreachable;
const data: *T = if (userdata) |u| @ptrCast(u) else unreachable;
const data: *T = if (userdata) |u| @alignCast(@ptrCast(u)) else unreachable;
callback(connection, subscription, Status.fromInt(status), data);
}
@ -1005,7 +1005,7 @@ pub fn makeEventLoopAddRemoveCallbackThunk(
userdata: ?*anyopaque,
) callconv(.C) nats_c.natsStatus {
const connection: *Connection = if (conn) |c| @ptrCast(c) else unreachable;
const data: *T = if (userdata) |u| @ptrCast(u) else unreachable;
const data: *T = if (userdata) |u| @alignCast(@ptrCast(u)) else unreachable;
callback(connection, attempts, data) catch |err|
return Status.fromError(err).toInt();
@ -1028,7 +1028,7 @@ pub fn makeEventLoopDetachCallbackThunk(
fn thunk(
userdata: ?*anyopaque,
) callconv(.C) nats_c.natsStatus {
const data: *T = if (userdata) |u| @ptrCast(u) else unreachable;
const data: *T = if (userdata) |u| @alignCast(@ptrCast(u)) else unreachable;
callback(data) catch |err| return Status.fromError(err).toInt();
return nats_c.NATS_OK;
}
@ -1058,7 +1058,7 @@ pub fn makeJwtHandlerCallbackThunk(
err_out: *?[*:0]u8,
userdata: ?*anyopaque,
) callconv(.C) nats_c.natsStatus {
const data: *T = if (userdata) |u| @ptrCast(u) else unreachable;
const data: *T = if (userdata) |u| @alignCast(@ptrCast(u)) else unreachable;
switch (callback(data)) {
.jwt => |jwt| {
@ -1099,7 +1099,7 @@ pub fn makeSignatureHandlerCallbackThunk(
nonce: [*:0]const u8,
userdata: ?*anyopaque,
) callconv(.C) nats_c.natsStatus {
const data: *T = if (userdata) |u| @ptrCast(u) else unreachable;
const data: *T = if (userdata) |u| @alignCast(@ptrCast(u)) else unreachable;
switch (callback(std.mem.sliceTo(nonce, 0), data)) {
.signature => |sig| {

View File

@ -197,7 +197,7 @@ pub fn makeSubscriptionCallbackThunk(
const connection: *Connection = if (conn) |c| @ptrCast(c) else unreachable;
const subscription: *Subscription = if (sub) |s| @ptrCast(s) else unreachable;
const data: *T = if (userdata) |u| @ptrCast(u) else unreachable;
const data: *T = if (userdata) |u| @alignCast(@ptrCast(u)) else unreachable;
callback(data, connection, subscription, message);
}

View File

@ -16,7 +16,7 @@ pub fn makeSimpleCallbackThunk(
) *const SimpleCallback {
return struct {
fn thunk(userdata: ?*anyopaque) callconv(.C) void {
const data: *T = if (userdata) |u| @ptrCast(u) else unreachable;
const data: *T = if (userdata) |u| @alignCast(@ptrCast(u)) else unreachable;
callback(data);
}
}.thunk;