Skip to content

Commit 9dfda62

Browse files
committed
Clarify docs for Read::read's return value
1 parent 76c500e commit 9dfda62

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

library/std/src/io/mod.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -514,8 +514,8 @@ pub trait Read {
514514
/// waiting for data, but if an object needs to block for a read and cannot,
515515
/// it will typically signal this via an [`Err`] return value.
516516
///
517-
/// If the return value of this method is [`Ok(n)`], then it must be
518-
/// guaranteed that `0 <= n <= buf.len()`. A nonzero `n` value indicates
517+
/// If the return value of this method is [`Ok(n)`], then implementations must
518+
/// guarantee that `0 <= n <= buf.len()`. A nonzero `n` value indicates
519519
/// that the buffer `buf` has been filled in with `n` bytes of data from this
520520
/// source. If `n` is `0`, then it can indicate one of two scenarios:
521521
///
@@ -529,6 +529,11 @@ pub trait Read {
529529
/// This may happen for example because fewer bytes are actually available right now
530530
/// (e. g. being close to end-of-file) or because read() was interrupted by a signal.
531531
///
532+
/// As this trait is safe to implement, callers cannot rely on `n <= buf.len()` for safety.
533+
/// Extra care needs to be taken when `unsafe` functions are used to access the read bytes.
534+
/// Callers have to ensure that no unchecked out-of-bounds accesses are possible even if
535+
/// `n > buf.len()`.
536+
///
532537
/// No guarantees are provided about the contents of `buf` when this
533538
/// function is called, implementations cannot rely on any property of the
534539
/// contents of `buf` being true. It is recommended that *implementations*

0 commit comments

Comments
 (0)