Reputation: 14354
How would you declare the following struct equivalently in Zig?
static struct lws_protocols protocols[] = {
{ "http", lws_callback_http_dummy, 0, 0 },
LWS_PLUGIN_PROTOCOL_MINIMAL,
{ NULL, NULL, 0, 0 } /* terminator */
};
Looking at the LWS_PLUGIN_PROTOCOL_MINIMAL
, it's defined in a header as:
#define LWS_PLUGIN_PROTOCOL_MINIMAL \
{ \
"lws-minimal-proxy", \
callback_minimal, \
sizeof(struct per_session_data__minimal), \
128, \
0, NULL, 0 \
}
and
struct lws_protocols {
const char *name;
lws_callback_function *callback;
size_t per_session_data_size;
size_t rx_buffer_size;
unsigned int id;
void *user;
size_t tx_packet_size;
};
Upvotes: 0
Views: 844
Reputation: 14354
I used zig translate-c -l libwebsockets minimal-ws-server.c > foo.zig
to produce a zig source file from the original C source.
I then looked through the foo.zig file and extracted the following:
pub const struct_lws_protocols = extern struct {
name: [*c]const u8,
callback: ?lws.lws_callback_function,
per_session_data_size: usize,
rx_buffer_size: usize,
id: c_uint,
user: ?*c_void,
tx_packet_size: usize,
};
pub var protocols: [0]struct_lws_protocols = [0]struct_lws_protocols{
struct_lws_protocols{
.name = "http",
.callback = lws.lws_callback_http_dummy,
.per_session_data_size = @bitCast(usize, @as(c_long, @as(c_int, 0))),
.rx_buffer_size = @bitCast(usize, @as(c_long, @as(c_int, 0))),
.id = 0,
.user = null,
.tx_packet_size = 0,
},
};
I then discovered that using the zig C header import:
const lws = @cImport(@cInclude("libwebsockets.h"));
I could just reference the structs (or other definitions) provided by the header by prefixing the names with lws.
e.g. lws.lws_callback_http_dummy
(as used above) rather than duplicating those definitions in my zig source
Upvotes: 1