Skip to content

Commit 02905d7

Browse files
committed
move macro
1 parent 310f0fc commit 02905d7

File tree

2 files changed

+133
-5
lines changed

2 files changed

+133
-5
lines changed

src/macros.zig

+125
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
const std = @import("std");
2+
3+
pub const c = @import("zenoh_c");
4+
5+
pub fn Moved(T: type) type {
6+
const converter = switch (T) {
7+
c.z_owned_alloc_layout_t => c.z_alloc_layout_move,
8+
c.z_owned_bytes_t => c.z_bytes_move,
9+
c.z_owned_bytes_writer_t => c.z_bytes_writer_move,
10+
c.z_owned_chunk_alloc_result_t => c.z_chunk_alloc_result_move,
11+
c.z_owned_closure_hello_t => c.z_closure_hello_move,
12+
c.z_owned_closure_matching_status_t => c.z_closure_matching_status_move,
13+
c.z_owned_closure_query_t => c.z_closure_query_move,
14+
c.z_owned_closure_reply_t => c.z_closure_reply_move,
15+
c.z_owned_closure_sample_t => c.z_closure_sample_move,
16+
c.z_owned_closure_zid_t => c.z_closure_zid_move,
17+
c.z_owned_condvar_t => c.z_condvar_move,
18+
c.z_owned_config_t => c.z_config_move,
19+
c.z_owned_encoding_t => c.z_encoding_move,
20+
c.z_owned_fifo_handler_query_t => c.z_fifo_handler_query_move,
21+
c.z_owned_fifo_handler_reply_t => c.z_fifo_handler_reply_move,
22+
c.z_owned_fifo_handler_sample_t => c.z_fifo_handler_sample_move,
23+
c.z_owned_hello_t => c.z_hello_move,
24+
c.z_owned_keyexpr_t => c.z_keyexpr_move,
25+
c.z_owned_liveliness_token_t => c.z_liveliness_token_move,
26+
c.z_owned_matching_listener_t => c.z_matching_listener_move,
27+
c.z_owned_memory_layout_t => c.z_memory_layout_move,
28+
c.z_owned_mutex_t => c.z_mutex_move,
29+
c.z_owned_publisher_t => c.z_publisher_move,
30+
c.z_owned_querier_t => c.z_querier_move,
31+
c.z_owned_query_t => c.z_query_move,
32+
c.z_owned_queryable_t => c.z_queryable_move,
33+
c.z_owned_reply_t => c.z_reply_move,
34+
c.z_owned_reply_err_t => c.z_reply_err_move,
35+
c.z_owned_ring_handler_query_t => c.z_ring_handler_query_move,
36+
c.z_owned_ring_handler_reply_t => c.z_ring_handler_reply_move,
37+
c.z_owned_ring_handler_sample_t => c.z_ring_handler_sample_move,
38+
c.z_owned_sample_t => c.z_sample_move,
39+
c.z_owned_session_t => c.z_session_move,
40+
c.z_owned_shm_client_t => c.z_shm_client_move,
41+
c.z_owned_shm_client_storage_t => c.z_shm_client_storage_move,
42+
c.z_owned_shm_t => c.z_shm_move,
43+
c.z_owned_shm_mut_t => c.z_shm_mut_move,
44+
c.z_owned_shm_provider_t => c.z_shm_provider_move,
45+
c.z_owned_slice_t => c.z_slice_move,
46+
c.z_owned_source_info_t => c.z_source_info_move,
47+
c.z_owned_string_array_t => c.z_string_array_move,
48+
c.z_owned_string_t => c.z_string_move,
49+
c.z_owned_subscriber_t => c.z_subscriber_move,
50+
c.z_owned_task_t => c.z_task_move,
51+
c.zc_owned_closure_log_t => c.zc_closure_log_move,
52+
c.zc_owned_concurrent_close_handle_t => c.zc_concurrent_close_handle_move,
53+
c.zc_owned_shm_client_list_t => c.zc_shm_client_list_move,
54+
c.ze_owned_advanced_publisher_t => c.ze_advanced_publisher_move,
55+
c.ze_owned_advanced_subscriber_t => c.ze_advanced_subscriber_move,
56+
c.ze_owned_closure_miss_t => c.ze_closure_miss_move,
57+
c.ze_owned_publication_cache_t => c.ze_publication_cache_move,
58+
c.ze_owned_querying_subscriber_t => c.ze_querying_subscriber_move,
59+
c.ze_owned_sample_miss_listener_t => c.ze_sample_miss_listener_move,
60+
c.ze_owned_serializer_t => c.ze_serializer_move,
61+
else => comptime unreachable,
62+
};
63+
return @typeInfo(@TypeOf(converter)).@"fn".return_type.?;
64+
}
65+
66+
pub fn move(value: anytype) Moved(std.meta.Child(@TypeOf(value))) {
67+
const converter = switch (std.meta.Child(@TypeOf(value))) {
68+
c.z_owned_alloc_layout_t => c.z_alloc_layout_move,
69+
c.z_owned_bytes_t => c.z_bytes_move,
70+
c.z_owned_bytes_writer_t => c.z_bytes_writer_move,
71+
c.z_owned_chunk_alloc_result_t => c.z_chunk_alloc_result_move,
72+
c.z_owned_closure_hello_t => c.z_closure_hello_move,
73+
c.z_owned_closure_matching_status_t => c.z_closure_matching_status_move,
74+
c.z_owned_closure_query_t => c.z_closure_query_move,
75+
c.z_owned_closure_reply_t => c.z_closure_reply_move,
76+
c.z_owned_closure_sample_t => c.z_closure_sample_move,
77+
c.z_owned_closure_zid_t => c.z_closure_zid_move,
78+
c.z_owned_condvar_t => c.z_condvar_move,
79+
c.z_owned_config_t => c.z_config_move,
80+
c.z_owned_encoding_t => c.z_encoding_move,
81+
c.z_owned_fifo_handler_query_t => c.z_fifo_handler_query_move,
82+
c.z_owned_fifo_handler_reply_t => c.z_fifo_handler_reply_move,
83+
c.z_owned_fifo_handler_sample_t => c.z_fifo_handler_sample_move,
84+
c.z_owned_hello_t => c.z_hello_move,
85+
c.z_owned_keyexpr_t => c.z_keyexpr_move,
86+
c.z_owned_liveliness_token_t => c.z_liveliness_token_move,
87+
c.z_owned_matching_listener_t => c.z_matching_listener_move,
88+
c.z_owned_memory_layout_t => c.z_memory_layout_move,
89+
c.z_owned_mutex_t => c.z_mutex_move,
90+
c.z_owned_publisher_t => c.z_publisher_move,
91+
c.z_owned_querier_t => c.z_querier_move,
92+
c.z_owned_query_t => c.z_query_move,
93+
c.z_owned_queryable_t => c.z_queryable_move,
94+
c.z_owned_reply_t => c.z_reply_move,
95+
c.z_owned_reply_err_t => c.z_reply_err_move,
96+
c.z_owned_ring_handler_query_t => c.z_ring_handler_query_move,
97+
c.z_owned_ring_handler_reply_t => c.z_ring_handler_reply_move,
98+
c.z_owned_ring_handler_sample_t => c.z_ring_handler_sample_move,
99+
c.z_owned_sample_t => c.z_sample_move,
100+
c.z_owned_session_t => c.z_session_move,
101+
c.z_owned_shm_client_t => c.z_shm_client_move,
102+
c.z_owned_shm_client_storage_t => c.z_shm_client_storage_move,
103+
c.z_owned_shm_t => c.z_shm_move,
104+
c.z_owned_shm_mut_t => c.z_shm_mut_move,
105+
c.z_owned_shm_provider_t => c.z_shm_provider_move,
106+
c.z_owned_slice_t => c.z_slice_move,
107+
c.z_owned_source_info_t => c.z_source_info_move,
108+
c.z_owned_string_array_t => c.z_string_array_move,
109+
c.z_owned_string_t => c.z_string_move,
110+
c.z_owned_subscriber_t => c.z_subscriber_move,
111+
c.z_owned_task_t => c.z_task_move,
112+
c.zc_owned_closure_log_t => c.zc_closure_log_move,
113+
c.zc_owned_concurrent_close_handle_t => c.zc_concurrent_close_handle_move,
114+
c.zc_owned_shm_client_list_t => c.zc_shm_client_list_move,
115+
c.ze_owned_advanced_publisher_t => c.ze_advanced_publisher_move,
116+
c.ze_owned_advanced_subscriber_t => c.ze_advanced_subscriber_move,
117+
c.ze_owned_closure_miss_t => c.ze_closure_miss_move,
118+
c.ze_owned_publication_cache_t => c.ze_publication_cache_move,
119+
c.ze_owned_querying_subscriber_t => c.ze_querying_subscriber_move,
120+
c.ze_owned_sample_miss_listener_t => c.ze_sample_miss_listener_move,
121+
c.ze_owned_serializer_t => c.ze_serializer_move,
122+
else => comptime unreachable,
123+
};
124+
return converter(value);
125+
}

src/root.zig

+8-5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ const std = @import("std");
22

33
pub const c = @import("zenoh_c");
44

5+
pub const macros = @import("macros.zig");
6+
const move = macros.move;
7+
58
pub const Error = error{ZenohError};
69

710
pub fn err(code: c.z_result_t) Error!void {
@@ -38,7 +41,7 @@ pub const Config = struct {
3841
}
3942

4043
pub fn deinit(self: *Config) void {
41-
c.z_config_drop(c.z_config_move(&self._c));
44+
c.z_config_drop(move(&self._c));
4245
}
4346
};
4447

@@ -65,12 +68,12 @@ pub const Session = struct {
6568

6669
pub fn open(config: *Config, options: *const OpenOptions) Error!Session {
6770
var c_session: c.z_owned_session_t = undefined;
68-
try err(c.z_open(&c_session, c.z_config_move(&config._c), &options._c));
71+
try err(c.z_open(&c_session, move(&config._c), &options._c));
6972
return Session{ ._c = c_session };
7073
}
7174

7275
pub fn deinit(self: *Session) void {
73-
c.z_session_drop(c.z_session_move(&self._c));
76+
c.z_session_drop(move(&self._c));
7477
}
7578

7679
pub const CloseOptions = struct {
@@ -117,7 +120,7 @@ pub const Session = struct {
117120
try err(c.z_put(
118121
c.z_session_loan(&self._c),
119122
c.z_view_keyexpr_loan(&view_keyexpr),
120-
c.z_bytes_move(&bytes._c),
123+
move(&bytes._c),
121124
&options._c,
122125
));
123126
}
@@ -133,7 +136,7 @@ pub const Bytes = struct {
133136
}
134137

135138
pub fn deinit(self: *Bytes) void {
136-
c.z_bytes_drop(c.z_bytes_move(&self._c));
139+
c.z_bytes_drop(move(&self._c));
137140
}
138141
};
139142

0 commit comments

Comments
 (0)