mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-09-29 08:11:13 +00:00
Documentation: Note that MUST
does not replace error propagation
This commit is contained in:
parent
6607336b38
commit
03c3d7edbc
Notes:
sideshowbarker
2024-07-17 01:11:06 +09:00
Author: https://github.com/timschumi Commit: https://github.com/SerenityOS/serenity/commit/03c3d7edbc Pull-request: https://github.com/SerenityOS/serenity/pull/17187
|
@ -63,19 +63,28 @@ Note: Our `TRY(...)` macro functions similarly to the `?` [operator in rust](htt
|
||||||
The `MUST(...)` macro is similar to `TRY(...)` except the macro enforces that
|
The `MUST(...)` macro is similar to `TRY(...)` except the macro enforces that
|
||||||
the code run inside the macro must succeed, otherwise we assert.
|
the code run inside the macro must succeed, otherwise we assert.
|
||||||
|
|
||||||
|
Note that `MUST(...)` should not be used as a replacement for `TRY(...)` in cases where error propagation is not (currently) possible.
|
||||||
|
Instead, the `release_value_but_fixme_should_propagate_errors()` method of `ErrorOr<>` should be used to retrieve the value
|
||||||
|
and to mark the location for future improvement. `MUST(...)` is reserved for cases where we determine through other circumstances that it
|
||||||
|
should not be possible for the code inside the macro to fail or if a failure is serious enough that the program _needs_ to crash.
|
||||||
|
|
||||||
### Example:
|
### Example:
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
#include <AK/Try.h>
|
#include <AK/Vector.h>
|
||||||
#include <AK/String.h>
|
|
||||||
|
|
||||||
... snip ...
|
... snip ...
|
||||||
|
|
||||||
void log_that_can_not_fail(StringView fmtstr, TypeErasedFormatParams& params)
|
ErrorOr<void> insert_one_to_onehundred(Vector<int>& vector)
|
||||||
{
|
{
|
||||||
StringBuilder builder;
|
TRY(vector.try_ensure_capacity(vector.size() + 100));
|
||||||
MUST(vformat(builder, fmtstr, params));
|
|
||||||
return builder.to_deprecated_string();
|
for (int i = 1; i <= 100; i++) {
|
||||||
|
// We previously made sure that we allocated enough space, so the append operation shouldn't ever fail.
|
||||||
|
MUST(vector.try_append(i));
|
||||||
|
}
|
||||||
|
|
||||||
|
return {};
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue