Skip to content

Commit 9b1e754

Browse files
committed
Clarify Read trait blocking behavior
1 parent bbd5803 commit 9b1e754

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

embedded-io/src/lib.rs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -300,16 +300,23 @@ impl<E: fmt::Debug> std::error::Error for WriteFmtError<E> {}
300300
pub trait Read: ErrorType {
301301
/// Read some bytes from this source into the specified buffer, returning how many bytes were read.
302302
///
303-
/// If no bytes are currently available to read, this function blocks until at least one byte is available.
303+
/// If no bytes are currently available to read:
304+
/// - The method blocks until at least one byte becomes available;
305+
/// - Once at least one (or more) bytes become available, a non-zero amount of those is read to the
306+
/// beginning of `buf`, and the amount is returned, *without waiting or blocking any further for
307+
/// more bytes to become available*.
304308
///
305-
/// If bytes are available, a non-zero amount of bytes is read to the beginning of `buf`, and the amount
306-
/// is returned. It is not guaranteed that *all* available bytes are returned, it is possible for the
307-
/// implementation to read an amount of bytes less than `buf.len()` while there are more bytes immediately
308-
/// available.
309+
/// If bytes are available to read:
310+
/// - A non-zero amount of bytes is read to the beginning of `buf`, and the amount is returned immediately,
311+
/// *without blocking and waiting for more bytes to become available*;
312+
/// - It is not guaranteed that *all* available bytes are returned, it is possible for the implementation to
313+
/// read an amount of bytes less than `buf.len()` while there are more bytes immediately available.
309314
///
310-
/// If the reader is at end-of-file (EOF), `Ok(0)` is returned. There is no guarantee that a reader at EOF
311-
/// will always be so in the future, for example a reader can stop being at EOF if another process appends
312-
/// more bytes to the underlying file.
315+
/// This blocking behavior is important for the cases where `Read` represents the "read" leg of a pipe-like
316+
/// protocol (a socket, a pipe, a serial line etc.). The semantics is that the caller - by passing a non-empty
317+
/// buffer - does expect _some_ data (one or more bytes) - but _not necessarily `buf.len()` or more bytes_ -
318+
/// to become available, before the peer represented by `Read` would stop sending bytes due to
319+
/// application-specific reasons (as in the peer waiting for a response to the data it had sent so far).
313320
///
314321
/// If `buf.len() == 0`, `read` returns without blocking, with either `Ok(0)` or an error.
315322
/// The `Ok(0)` doesn't indicate EOF, unlike when called with a non-empty buffer.

0 commit comments

Comments
 (0)