Skip to content

Commit f521aa0

Browse files
committed
std.io.Reader: add more docs for rebase
closes #24418
1 parent 0cb558b commit f521aa0

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

lib/std/Io/Reader.zig

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1303,6 +1303,13 @@ fn takeMultipleOf7Leb128(r: *Reader, comptime Result: type) TakeLeb128Error!Resu
13031303
}
13041304

13051305
/// Left-aligns data such that `r.seek` becomes zero.
1306+
///
1307+
/// If `r.seek` is not already zero then `buffer` is mutated, making it illegal
1308+
/// to call this function with a const-casted `buffer`, such as in the case of
1309+
/// `fixed`. This issue can be avoided:
1310+
/// * in implementations, by attempting a read before a rebase, in which
1311+
/// case the read will return `error.EndOfStream`, preventing the rebase.
1312+
/// * in usage, by copying into a mutable buffer before initializing `fixed`.
13061313
pub fn rebase(r: *Reader) void {
13071314
if (r.seek == 0) return;
13081315
const data = r.buffer[r.seek..r.end];
@@ -1315,6 +1322,13 @@ pub fn rebase(r: *Reader) void {
13151322
/// if necessary.
13161323
///
13171324
/// Asserts `capacity` is within the buffer capacity.
1325+
///
1326+
/// If the rebase occurs then `buffer` is mutated, making it illegal to call
1327+
/// this function with a const-casted `buffer`, such as in the case of `fixed`.
1328+
/// This issue can be avoided:
1329+
/// * in implementations, by attempting a read before a rebase, in which
1330+
/// case the read will return `error.EndOfStream`, preventing the rebase.
1331+
/// * in usage, by copying into a mutable buffer before initializing `fixed`.
13181332
pub fn rebaseCapacity(r: *Reader, capacity: usize) void {
13191333
if (r.end > r.buffer.len - capacity) rebase(r);
13201334
}

0 commit comments

Comments
 (0)