Skip to content

Commit

Permalink
Mark default loop init with nodiscard
Browse files Browse the repository at this point in the history
  • Loading branch information
vector-of-bool committed Jan 25, 2025
1 parent 1a7996b commit cc382c9
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 17 deletions.
16 changes: 10 additions & 6 deletions docs/how-to/communicate.example.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,13 @@ int main(int argc, char const* const* argv) {
}
const char* const uri = argv[1];

amongoc_loop loop;
amongoc_default_loop_init(&loop);
amongoc_loop loop;
amongoc_status status = amongoc_default_loop_init(&loop);
if (amongoc_is_error(status)) {
amongoc_declmsg(msg, status);
fprintf(stderr, "Error setting up the event loop: %s\n", msg);
return 2;
}

struct app_state state = {0};

Expand All @@ -88,8 +93,7 @@ int main(int argc, char const* const* argv) {
amongoc_box_pointer(&state),
after_connect_say_hello);

amongoc_status fin_status = amongoc_okay;
amongoc_operation op = amongoc_tie(em, &fin_status);
amongoc_operation op = amongoc_tie(em, &status);
amongoc_start(&op);
amongoc_default_loop_run(&loop);
amongoc_operation_delete(op);
Expand All @@ -98,8 +102,8 @@ int main(int argc, char const* const* argv) {
amongoc_client_delete(state.client);
amongoc_default_loop_destroy(&loop);

if (amongoc_is_error(fin_status)) {
amongoc_declmsg(msg, fin_status);
if (amongoc_is_error(status)) {
amongoc_declmsg(msg, status);
fprintf(stderr, "An error occurred: %s\n", msg);
return 2;
} else {
Expand Down
4 changes: 2 additions & 2 deletions docs/how-to/communicate.rst
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ The first "interesting" code will declare and initialize the default event loop:
.. literalinclude:: communicate.example.c
:lineno-match:
:start-at: loop;
:end-at: );
:end-at: }

.. seealso:: `amongoc_loop` and `amongoc_default_loop_init`

Expand Down Expand Up @@ -204,7 +204,7 @@ the first continuation, we use `amongoc_tie` to convert the emitter to an

.. literalinclude:: communicate.example.c
:lineno-match:
:start-at: fin_status
:start-at: amongoc_tie
:end-at: amongoc_tie

This will allow us to see the final result status of the program in
Expand Down
9 changes: 7 additions & 2 deletions docs/learn/connect.example.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@
amongoc_box on_connect(amongoc_box userdata, amongoc_status* status, amongoc_box result);

int main(void) {
amongoc_loop loop;
amongoc_default_loop_init(&loop);
amongoc_loop loop;
amongoc_status status = amongoc_default_loop_init(&loop);
if (amongoc_is_error(status)) {
amongoc_declmsg(msg, status);
fprintf(stderr, "Failed to prepare the event loop: %s\n", msg);
return 2;
}

// Initiate a connection
amongoc_emitter em = amongoc_client_new(&loop, "mongodb://localhost:27017");
Expand Down
2 changes: 1 addition & 1 deletion docs/learn/connect.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ programs:
.. literalinclude:: connect.example.c
:caption: Create a default event loop
:start-at: int main
:end-at: default_loop_init
:end-at: }
:lineno-match:
:dedent:

Expand Down
10 changes: 6 additions & 4 deletions include/amongoc/default_loop.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@

mlib_extern_c_begin();

extern amongoc_status amongoc_default_loop_init_with_allocator(amongoc_loop* loop,
mlib_allocator) mlib_noexcept;
static inline amongoc_status amongoc_default_loop_init(amongoc_loop* loop) mlib_noexcept {
mlib_nodiscard("This function may fail to allocate resources") extern amongoc_status
amongoc_default_loop_init_with_allocator(amongoc_loop* loop, mlib_allocator) mlib_noexcept;

mlib_nodiscard("This function may fail to allocate resources") static inline amongoc_status
amongoc_default_loop_init(amongoc_loop* loop) mlib_noexcept {
return amongoc_default_loop_init_with_allocator(loop, mlib_default_allocator);
}

Expand All @@ -24,7 +26,7 @@ namespace amongoc {

struct default_event_loop {
public:
default_event_loop() { ::amongoc_default_loop_init(&_loop); }
default_event_loop() { ::amongoc_default_loop_init(&_loop).throw_for_error(); }
~default_event_loop() { ::amongoc_default_loop_destroy(&_loop); }

default_event_loop(default_event_loop&&) = delete;
Expand Down
7 changes: 7 additions & 0 deletions include/amongoc/status.h
Original file line number Diff line number Diff line change
Expand Up @@ -797,6 +797,8 @@ struct amongoc_status {
constexpr bool operator!=(amongoc_status const& other) const noexcept {
return not(*this == other);
}

inline void throw_for_error() const;
#endif
};

Expand Down Expand Up @@ -895,4 +897,9 @@ class exception : std::runtime_error {

bool amongoc_status::is_error() const noexcept { return amongoc_is_error(*this); }

void amongoc_status::throw_for_error() const {
if (this->is_error()) {
throw amongoc::exception(*this);
}
}
#endif
4 changes: 2 additions & 2 deletions src/amongoc/async.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ TEST_CASE("Async/Transform with the C API") {

TEST_CASE("Async/Timeout") {
amongoc_loop loop;
amongoc_default_loop_init(&loop);
amongoc_default_loop_init(&loop).throw_for_error();
// One minute delay (too slow)
auto dur = timespec{};
dur.tv_sec = 60;
Expand Down Expand Up @@ -93,7 +93,7 @@ emitter waits(amongoc_loop& loop) {

TEST_CASE("Async/let") {
amongoc_loop loop;
amongoc_default_loop_init(&loop);
amongoc_default_loop_init(&loop).throw_for_error();
auto em = amongoc_let(waits(loop),
amongoc_async_forward_errors,
mlib_default_allocator,
Expand Down

0 comments on commit cc382c9

Please sign in to comment.