Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a dynamic buffer in the AsyncReadExt's read_exact example #5863

Merged
merged 1 commit into from Jul 16, 2023

Conversation

avdb13
Copy link
Contributor

@avdb13 avdb13 commented Jul 13, 2023

Motivation

See this SO question.
In most cases when read_exact is used the user doesn't know beforehand how many bytes are gonna be read. This leads to code like Vec::with_capacity(len) when really what the user wants is vec![0; len] (the former has a length of zero so it succeeds with Ok(0)).

Solution

Change the example to use the vec! macro instead of a fixed-sized array. This will make adding a warning obsolete.

See: https://stackoverflow.com/questions/68979882/readread-exact-does-not-fill-buffer
In most cases when `read_exact` is used the user doesn't know beforehand how many bytes are gonna be read.
This leads to code like `Vec::with_capacity(len)` when really what the user wants is `vec![0; len]` (the former has a length of zero so it succeeds with `Ok(0)`.
@Darksonn Darksonn added T-docs Topic: documentation A-tokio Area: The main tokio crate M-io Module: tokio/io labels Jul 16, 2023
@Darksonn Darksonn merged commit 33d6d4f into tokio-rs:master Jul 16, 2023
73 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-tokio Area: The main tokio crate M-io Module: tokio/io T-docs Topic: documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants