Skip to content

Commit 3a8deed

Browse files
committed
give up on gen
1 parent 851cc4c commit 3a8deed

File tree

4 files changed

+45
-42
lines changed

4 files changed

+45
-42
lines changed

build.zig

+1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ pub fn build(b: *std.Build) void {
7575
.target = b.resolveTargetQuery(.{ .cpu_arch = .x86_64, .os_tag = .linux, .abi = .musl }),
7676
.root_source_file = zenoh_c_dep.path("include/zenoh.h"),
7777
});
78+
zenoh.addImport("zenoh_c", translate_c.createModule());
7879
const gen_tool = b.addExecutable(.{
7980
.name = "generate_bindings",
8081
.root_source_file = b.path("tools/generate_bindings.zig"),

examples/examples.zig

+12-12
Original file line numberDiff line numberDiff line change
@@ -21,46 +21,46 @@ fn publish() !void {
2121

2222
var got_message: bool = false;
2323

24-
fn data_handler(sample: [*c]zenoh.c.LoanedSample, arg: ?*anyopaque) callconv(.c) void {
24+
fn data_handler(sample: [*c]zenoh.c.z_loaned_sample_t, arg: ?*anyopaque) callconv(.c) void {
2525
_ = sample;
2626
_ = arg;
2727
std.log.info("Got sample!", .{});
2828
got_message = true;
2929
}
3030

3131
fn subscribe() !void {
32-
var config: zenoh.c.Config = undefined;
32+
var config: zenoh.c.z_owned_config_t = undefined;
3333
_ = zenoh.c.z_config_default(&config);
34-
defer zenoh.c.z_config_drop(&config);
34+
defer zenoh.c.z_config_drop(zenoh.c.z_config_move(&config));
3535

36-
var options: zenoh.c.OpenOptions = undefined;
36+
var options: zenoh.c.z_open_options_t = undefined;
3737
zenoh.c.z_open_options_default(&options);
38-
var session: zenoh.c.Session = undefined;
39-
if (zenoh.c.z_open(&session, &config, &options) != 0) {
38+
var session: zenoh.c.z_owned_session_t = undefined;
39+
if (zenoh.c.z_open(&session, zenoh.c.z_config_move(&config), &options) != 0) {
4040
std.log.err("Failed to open zenoh session.", .{});
4141
return error.OpenSessionFailure;
4242
}
43-
defer zenoh.c.z_session_drop(&session);
43+
defer zenoh.c.z_session_drop(zenoh.c.z_session_move(&session));
4444

45-
var callback: zenoh.c.ClosureSample = undefined;
45+
var callback: zenoh.c.z_owned_closure_sample_t = undefined;
4646
zenoh.c.z_closure_sample(&callback, &data_handler, null, null);
4747

48-
var key_expr: zenoh.c.ViewKeyexpr = undefined;
48+
var key_expr: zenoh.c.z_view_keyexpr_t = undefined;
4949
_ = zenoh.c.z_view_keyexpr_from_str(&key_expr, "key/expression");
5050

51-
var subscriber: zenoh.c.Subscriber = undefined;
51+
var subscriber: zenoh.c.z_owned_subscriber_t = undefined;
5252

5353
if (zenoh.c.z_declare_subscriber(
5454
zenoh.c.z_session_loan_mut(&session),
5555
&subscriber,
5656
zenoh.c.z_view_keyexpr_loan(&key_expr),
57-
&callback,
57+
zenoh.c.z_closure_sample_move(&callback),
5858
null,
5959
) != 0) {
6060
std.log.err("Failed to create zenoh subscriber", .{});
6161
return error.DeclareSubscriberFailure;
6262
}
63-
defer zenoh.c.z_subscriber_drop(&subscriber);
63+
defer zenoh.c.z_subscriber_drop(zenoh.c.z_subscriber_move(&subscriber));
6464

6565
var timer = std.time.Timer.start() catch @panic("timer unsupported");
6666

examples/z_bytes.zig

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ const std = @import("std");
44
const zenoh = @import("zenoh");
55

66
test "wrapping raw bytes into a z_bytes_t" {
7-
var payload: zenoh.c.Bytes = undefined;
7+
var payload: zenoh.c.z_owned_bytes_t = undefined;
88
const input_bytes: []const u8 = &[_]u8{ 1, 2, 3, 4 };
9-
var output_bytes: zenoh.c.Slice = undefined;
9+
var output_bytes: zenoh.c.z_owned_slice_t = undefined;
1010
_ = zenoh.c.z_bytes_copy_from_buf(&payload, input_bytes.ptr, 4);
1111
_ = zenoh.c.z_bytes_to_slice(zenoh.c.z_bytes_loan_mut(&payload), &output_bytes);
1212
// try std.testing.expectEqualSlices(u8, input_bytes, zenoh.c.z_slice_data(zenoh.c.z_slice_loan(&output_bytes)));

src/root.zig

+30-28
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,46 @@
11
const std = @import("std");
22

3-
pub const c = @import("c.zig");
3+
pub const c = @import("zenoh_c");
4+
5+
// pub const c = @import("c.zig");
46

57
pub const Error = error{ZenohError};
68

7-
pub fn err(code: c.Result) Error!void {
9+
pub fn err(code: c.z_result_t) Error!void {
810
if (code < 0) {
911
return error.ZenohError;
1012
}
1113
}
1214

1315
pub const Config = struct {
14-
_c: c.Config,
16+
_c: c.z_owned_config_t,
1517

1618
pub fn initDefault() Error!Config {
17-
var c_config: c.Config = undefined;
19+
var c_config: c.z_owned_config_t = undefined;
1820
try err(c.z_config_default(&c_config));
1921
return Config{ ._c = c_config };
2022
}
2123

2224
pub fn initFromEnv() Error!Config {
23-
var c_config: c.Config = undefined;
25+
var c_config: c.z_owned_config_t = undefined;
2426
try err(c.zc_config_from_env(&c_config));
2527
return Config{ ._c = c_config };
2628
}
2729

2830
pub fn initFromFile(path: [:0]const u8) Error!Config {
29-
var c_config: c.Config = undefined;
31+
var c_config: c.z_owned_config_t = undefined;
3032
try err(c.zc_config_from_file(&c_config, path.ptr));
3133
return Config{ ._c = c_config };
3234
}
3335

3436
pub fn initFromString(str: [:0]const u8) Error!Config {
35-
var c_config: c.Config = undefined;
37+
var c_config: c.z_owned_config_t = undefined;
3638
try err(c.zc_config_from_str(&c_config, str.ptr));
3739
return Config{ ._c = c_config };
3840
}
3941

4042
pub fn deinit(self: *Config) void {
41-
c.z_config_drop(&self._c);
43+
c.z_config_drop(c.z_config_move(&self._c));
4244
}
4345
};
4446

@@ -51,48 +53,48 @@ pub const ZID = struct {
5153
};
5254

5355
pub const Session = struct {
54-
_c: c.Session,
56+
_c: c.z_owned_session_t,
5557

5658
pub const OpenOptions = struct {
57-
_c: c.OpenOptions,
59+
_c: c.z_open_options_t,
5860

5961
pub fn init() OpenOptions {
60-
var c_openoptions: c.OpenOptions = undefined;
62+
var c_openoptions: c.z_open_options_t = undefined;
6163
c.z_open_options_default(&c_openoptions);
6264
return OpenOptions{ ._c = c_openoptions };
6365
}
6466
};
6567

6668
pub fn open(config: *Config, options: *const OpenOptions) Error!Session {
67-
var c_session: c.Session = undefined;
68-
try err(c.z_open(&c_session, &config._c, &options._c));
69+
var c_session: c.z_owned_session_t = undefined;
70+
try err(c.z_open(&c_session, c.z_config_move(&config._c), &options._c));
6971
return Session{ ._c = c_session };
7072
}
7173

7274
pub fn deinit(self: *Session) void {
73-
c.z_session_drop(&self._c);
75+
c.z_session_drop(c.z_session_move(&self._c));
7476
}
7577

7678
pub const CloseOptions = struct {
77-
_c: c.CloseOptions,
79+
_c: c.z_close_options_t,
7880

7981
pub fn init() CloseOptions {
80-
var c_closeoptions: c.CloseOptions = undefined;
82+
var c_closeoptions: c.z_close_options_t = undefined;
8183
c.z_close_options_default(&c_closeoptions);
8284
return CloseOptions{ ._c = c_closeoptions };
8385
}
8486
};
8587

8688
pub fn close(self: *Session, options: *CloseOptions) Error!void {
87-
try err(c.z_close(c.z_session_loan_mut(self), options));
89+
try err(c.z_close(c.z_session_loan_mut(self), &options._c));
8890
}
8991

9092
pub fn isClosed(self: *const Session) bool {
91-
return c.z_session_is_closed(c.z_session_loan(self));
93+
return c.z_session_is_closed(c.z_session_loan(&self._c));
9294
}
9395

9496
pub fn infoZid(self: *const Session) error{InvalidSession}!ZID {
95-
const c_zid = c.z_info_zid(c.z_session_loan(self));
97+
const c_zid = c.z_info_zid(c.z_session_loan(&self._c));
9698
const zid = ZID{ ._c = c_zid };
9799
if (!zid.isValid()) return error.InvalidSession;
98100
return zid;
@@ -101,48 +103,48 @@ pub const Session = struct {
101103
// TODO: other zid stuff
102104

103105
pub const PutOptions = struct {
104-
_c: c.PutOptions,
106+
_c: c.z_put_options_t,
105107

106108
pub fn init() PutOptions {
107-
var c_options: c.PutOptions = undefined;
109+
var c_options: c.z_put_options_t = undefined;
108110
c.z_put_options_default(&c_options);
109111
return PutOptions{ ._c = c_options };
110112
}
111113
};
112114

113115
pub fn put(self: *const Session, key_expr: [:0]const u8, bytes: *Bytes, options: *PutOptions) Error!void {
114-
var view_keyexpr: c.ViewKeyexpr = undefined;
116+
var view_keyexpr: c.z_view_keyexpr_t = undefined;
115117
try err(c.z_view_keyexpr_from_str(&view_keyexpr, key_expr.ptr));
116118

117119
try err(c.z_put(
118120
c.z_session_loan(&self._c),
119121
c.z_view_keyexpr_loan(&view_keyexpr),
120-
&bytes._c,
122+
c.z_bytes_move(&bytes._c),
121123
&options._c,
122124
));
123125
}
124126
};
125127

126128
pub const Bytes = struct {
127-
_c: c.Bytes,
129+
_c: c.z_owned_bytes_t,
128130

129131
pub fn initFromStaticString(string: [:0]const u8) Error!Bytes {
130-
var c_bytes: c.Bytes = undefined;
132+
var c_bytes: c.z_owned_bytes_t = undefined;
131133
try err(c.z_bytes_from_static_str(&c_bytes, string.ptr));
132134
return Bytes{ ._c = c_bytes };
133135
}
134136

135137
pub fn deinit(self: *Bytes) void {
136-
c.z_bytes_drop(&self._c);
138+
c.z_bytes_drop(c.z_bytes_move(&self._c));
137139
}
138140
};
139141

140142
pub const KeyExpr = struct {
141-
_c: c.Keyexpr,
143+
_c: c.z_owned_keyexpr_t,
142144
};
143145

144146
pub const ViewKeyExpr = struct {
145-
_c: c.ViewKeyexpr,
147+
_c: c.z_view_keyexpr_t,
146148
};
147149

148150
test "sanity check" {

0 commit comments

Comments
 (0)