Skip to content

Commit 0c31d81

Browse files
authored
Merge pull request #625 from ivmarkov/master
Clarify Read trait blocking behavior
2 parents 4382c34 + d745418 commit 0c31d81

File tree

2 files changed

+37
-11
lines changed

2 files changed

+37
-11
lines changed

Diff for: embedded-io-async/src/lib.rs

+19-6
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,25 @@ pub use embedded_io::{
1919
pub trait Read: ErrorType {
2020
/// Read some bytes from this source into the specified buffer, returning how many bytes were read.
2121
///
22-
/// If no bytes are currently available to read, this function waits until at least one byte is available.
23-
///
24-
/// If bytes are available, a non-zero amount of bytes is read to the beginning of `buf`, and the amount
25-
/// is returned. It is not guaranteed that *all* available bytes are returned, it is possible for the
26-
/// implementation to read an amount of bytes less than `buf.len()` while there are more bytes immediately
27-
/// available.
22+
/// If no bytes are currently available to read:
23+
/// - The method waits until at least one byte becomes available;
24+
/// - Once at least one (or more) bytes become available, a non-zero amount of those is copied to the
25+
/// beginning of `buf`, and the amount is returned, *without waiting any further for more bytes to
26+
/// become available*.
27+
///
28+
/// If bytes are available to read:
29+
/// - A non-zero amount of bytes is read to the beginning of `buf`, and the amount is returned immediately,
30+
/// *without waiting for more bytes to become available*;
31+
///
32+
/// Note that once some bytes are available to read, it is *not* guaranteed that all available bytes are returned.
33+
/// It is possible for the implementation to read an amount of bytes less than `buf.len()` while there are more
34+
/// bytes immediately available.
35+
///
36+
/// This waiting behavior is important for the cases where `Read` represents the "read" leg of a pipe-like
37+
/// protocol (a socket, a pipe, a serial line etc.). The semantics is that the caller - by passing a non-empty
38+
/// buffer - does expect _some_ data (one or more bytes) - but _not necessarily `buf.len()` or more bytes_ -
39+
/// to become available, before the peer represented by `Read` would stop sending bytes due to
40+
/// application-specific reasons (as in the peer waiting for a response to the data it had sent so far).
2841
///
2942
/// If the reader is at end-of-file (EOF), `Ok(0)` is returned. There is no guarantee that a reader at EOF
3043
/// will always be so in the future, for example a reader can stop being at EOF if another process appends

Diff for: embedded-io/src/lib.rs

+18-5
Original file line numberDiff line numberDiff line change
@@ -300,12 +300,25 @@ 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 copied to the
306+
/// beginning of `buf`, and the amount is returned, *without waiting or blocking any further for
307+
/// more bytes to become available*.
308+
///
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+
///
313+
/// Note that once some bytes are available to read, it is *not* guaranteed that all available bytes are returned.
314+
/// It is possible for the implementation to read an amount of bytes less than `buf.len()` while there are more
315+
/// bytes immediately available.
304316
///
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.
317+
/// This blocking behavior is important for the cases where `Read` represents the "read" leg of a pipe-like
318+
/// protocol (a socket, a pipe, a serial line etc.). The semantics is that the caller - by passing a non-empty
319+
/// buffer - does expect _some_ data (one or more bytes) - but _not necessarily `buf.len()` or more bytes_ -
320+
/// to become available, before the peer represented by `Read` would stop sending bytes due to
321+
/// application-specific reasons (as in the peer waiting for a response to the data it had sent so far).
309322
///
310323
/// If the reader is at end-of-file (EOF), `Ok(0)` is returned. There is no guarantee that a reader at EOF
311324
/// will always be so in the future, for example a reader can stop being at EOF if another process appends

0 commit comments

Comments
 (0)