Skip to content

Commit 2daecf7

Browse files
authored
Rollup merge of #111940 - zirconium-n:io-read-doc-change, r=thomcc
Clarify safety concern of `io::Read::read` is only relevant in unsafe code We have this clarification note in other similar place like [Iterator::size_hint](https://doc.rust-lang.org/stable/std/iter/trait.Iterator.html#method.size_hint). The lack of clarification might lead to confusion to Rust beginners. [Relevant URLO post](https://users.rust-lang.org/t/can-read-overflow-a-buffer/94347).
2 parents 78cc117 + dd56f93 commit 2daecf7

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

library/std/src/io/mod.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -593,7 +593,8 @@ pub trait Read {
593593
/// This may happen for example because fewer bytes are actually available right now
594594
/// (e. g. being close to end-of-file) or because read() was interrupted by a signal.
595595
///
596-
/// As this trait is safe to implement, callers cannot rely on `n <= buf.len()` for safety.
596+
/// As this trait is safe to implement, callers in unsafe code cannot rely on
597+
/// `n <= buf.len()` for safety.
597598
/// Extra care needs to be taken when `unsafe` functions are used to access the read bytes.
598599
/// Callers have to ensure that no unchecked out-of-bounds accesses are possible even if
599600
/// `n > buf.len()`.
@@ -603,8 +604,8 @@ pub trait Read {
603604
/// contents of `buf` being true. It is recommended that *implementations*
604605
/// only write data to `buf` instead of reading its contents.
605606
///
606-
/// Correspondingly, however, *callers* of this method must not assume any guarantees
607-
/// about how the implementation uses `buf`. The trait is safe to implement,
607+
/// Correspondingly, however, *callers* of this method in unsafe code must not assume
608+
/// any guarantees about how the implementation uses `buf`. The trait is safe to implement,
608609
/// so it is possible that the code that's supposed to write to the buffer might also read
609610
/// from it. It is your responsibility to make sure that `buf` is initialized
610611
/// before calling `read`. Calling `read` with an uninitialized `buf` (of the kind one

0 commit comments

Comments
 (0)