Skip to content

Commit

Permalink
refactor: polish the usage of z_result_t in for loop
Browse files Browse the repository at this point in the history
  • Loading branch information
YuanYuYuan committed Aug 7, 2024
1 parent 56332aa commit 97779e0
Show file tree
Hide file tree
Showing 7 changed files with 12 additions and 11 deletions.
4 changes: 2 additions & 2 deletions examples/z_get.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ int main(int argc, char **argv) {
z_move(opts)); // here, the send is moved and will be dropped by zenoh when adequate
z_owned_reply_t reply;

for (z_result_t res = z_recv(z_loan(handler), &reply); res == Z_OK; res = z_recv(z_loan(handler), &reply)) {
while (z_recv(z_loan(handler), &reply) == Z_OK) {
if (z_reply_is_ok(z_loan(reply))) {
const z_loaned_sample_t *sample = z_reply_ok(z_loan(reply));

Expand All @@ -93,4 +93,4 @@ int main(int argc, char **argv) {
z_drop(z_move(handler));
z_close(z_move(s));
return 0;
}
}
2 changes: 1 addition & 1 deletion examples/z_get_liveliness.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ int main(int argc, char **argv) {
z_fifo_channel_reply_new(&closure, &handler, 16);
zc_liveliness_get(z_loan(s), z_loan(keyexpr), z_move(closure), NULL);
z_owned_reply_t reply;
for (z_result_t res = z_recv(z_loan(handler), &reply); res == Z_OK; res = z_recv(z_loan(handler), &reply)) {
while (z_recv(z_loan(handler), &reply) == Z_OK) {
if (z_reply_is_ok(z_loan(reply))) {
const z_loaned_sample_t *sample = z_reply_ok(z_loan(reply));
z_view_string_t key_str;
Expand Down
4 changes: 2 additions & 2 deletions examples/z_get_shm.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ int main(int argc, char** argv) {
z_move(opts)); // here, the send is moved and will be dropped by zenoh when adequate
z_owned_reply_t reply;

for (z_result_t res = z_recv(z_loan(handler), &reply); res == Z_OK; res = z_recv(z_loan(handler), &reply)) {
while (z_recv(z_loan(handler), &reply) == Z_OK) {
if (z_reply_is_ok(z_loan(reply))) {
const z_loaned_sample_t* sample = z_reply_ok(z_loan(reply));

Expand All @@ -135,4 +135,4 @@ int main(int argc, char** argv) {
z_drop(z_move(provider));
z_drop(z_move(layout));
return 0;
}
}
7 changes: 4 additions & 3 deletions examples/z_non_blocking_get.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ int main(int argc, char **argv) {
z_get(z_loan(s), z_loan(keyexpr), "", z_move(closure),
z_move(opts)); // here, the closure is moved and will be dropped by zenoh when adequate
z_owned_reply_t reply;
for (z_result_t res = z_try_recv(z_loan(handler), &reply); res != Z_CHANNEL_DISCONNECTED;
res = z_try_recv(z_loan(handler), &reply)) {

z_result_t res;
while ((res = z_try_recv(z_loan(handler), &reply)) != Z_CHANNEL_DISCONNECTED) {
if (res != Z_OK) {
z_sleep_ms(50);
continue;
Expand All @@ -78,4 +79,4 @@ int main(int argc, char **argv) {
z_drop(z_move(handler));
z_close(z_move(s));
return 0;
}
}
2 changes: 1 addition & 1 deletion examples/z_queryable_with_channels.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ int main(int argc, char **argv) {

printf("^C to quit...\n");
z_owned_query_t oquery;
for (z_result_t res = z_recv(z_loan(handler), &oquery); res == Z_OK; res = z_recv(z_loan(handler), &oquery)) {
while (z_recv(z_loan(handler), &oquery) == Z_OK) {
const z_loaned_query_t *query = z_loan(oquery);
z_view_string_t key_string;
z_keyexpr_as_view_string(z_query_keyexpr(query), &key_string);
Expand Down
2 changes: 1 addition & 1 deletion tests/z_int_queryable_attachment_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ int run_get() {
opts.attachment = &attachment;
z_get(z_loan(s), z_loan(ke), "", z_move(closure), &opts);
z_owned_reply_t reply;
for (z_result_t res = z_recv(z_loan(handler), &reply); res == Z_OK; res = z_recv(z_loan(handler), &reply)) {
while (z_recv(z_loan(handler), &reply) == Z_OK) {
assert(z_reply_is_ok(z_loan(reply)));

const z_loaned_sample_t *sample = z_reply_ok(z_loan(reply));
Expand Down
2 changes: 1 addition & 1 deletion tests/z_int_queryable_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ int run_get() {
z_get_options_default(&opts);
z_get(z_loan(s), z_loan(ke), "", z_move(closure), &opts);
z_owned_reply_t reply;
for (z_result_t res = z_recv(z_loan(handler), &reply); res == Z_OK; res = z_recv(z_loan(handler), &reply)) {
while (z_recv(z_loan(handler), &reply) == Z_OK) {
assert(z_reply_is_ok(z_loan(reply)));

const z_loaned_sample_t *sample = z_reply_ok(z_loan(reply));
Expand Down

0 comments on commit 97779e0

Please sign in to comment.