EOF should be reported differently than returning 0 #2717
Labels
A-tokio
Area: The main tokio crate
C-feature-request
Category: A feature request.
M-io
Module: tokio/io
Is your feature request related to a problem? Please describe.
read
function in AsyncReadExt forces user to check against zero which is legal value. EOF should be explicitly reported - similar toNone
inOption
to do not allow to forget.Describe the solution you'd like
Change result of
read
fromio::Result<usize>
toio::Result<Option<core::num::NonZeroUsize>>
or to some enum (e.g. EOF,Length(core::num::NonZeroUsize)) - this will add no memory overhead with explicit EOF handling. This will also prevent using zero sized buffer for reading (this is sadly price of explicit checking and zero memory overhead) - for this Error would be reported.Describe alternatives you've considered
I was thinking about using Option or enum with usize directly but I can not image real world scenario where actually using zero sized buffer for reading would be useful.
Additional context
It seems to me, that the checking if read returned 0 bytes and accept is as EOF looks similar to infamous null pointer problem. This is actually the purpose of structs like
Option
orResult
to ensure that programmer must explicitly ignore things to make a problem.The text was updated successfully, but these errors were encountered: