mirror of
https://github.com/chatmail/core.git
synced 2026-05-19 23:06:32 +03:00
docs(STYLE.md): prefer try_next() over next()
This commit is contained in:
21
STYLE.md
21
STYLE.md
@@ -78,6 +78,27 @@ All errors should be handled in one of these ways:
|
|||||||
- With `.log_err().ok()`.
|
- With `.log_err().ok()`.
|
||||||
- Bubbled up with `?`.
|
- Bubbled up with `?`.
|
||||||
|
|
||||||
|
When working with [async streams](https://docs.rs/futures/0.3.31/futures/stream/index.html),
|
||||||
|
prefer [`try_next`](https://docs.rs/futures/0.3.31/futures/stream/trait.TryStreamExt.html#method.try_next) over `next()`, e.g. do
|
||||||
|
```
|
||||||
|
while let Some(event) = stream.try_next().await? {
|
||||||
|
todo!();
|
||||||
|
}
|
||||||
|
```
|
||||||
|
instead of
|
||||||
|
```
|
||||||
|
while let Some(event_res) = stream.next().await {
|
||||||
|
todo!();
|
||||||
|
}
|
||||||
|
```
|
||||||
|
as it allows bubbling up the error early with `?`
|
||||||
|
with no way to accidentally skip error processing
|
||||||
|
with early `continue` or `break`.
|
||||||
|
Some streams reading from a connection
|
||||||
|
return infinite number of `Some(Err(_))`
|
||||||
|
items when connection breaks and not processing
|
||||||
|
errors may result in infinite loop.
|
||||||
|
|
||||||
`backtrace` feature is enabled for `anyhow` crate
|
`backtrace` feature is enabled for `anyhow` crate
|
||||||
and `debug = 1` option is set in the test profile.
|
and `debug = 1` option is set in the test profile.
|
||||||
This allows to run `RUST_BACKTRACE=1 cargo test`
|
This allows to run `RUST_BACKTRACE=1 cargo test`
|
||||||
|
|||||||
Reference in New Issue
Block a user