bytes: clarify semantics regarding Buffer.Bytes and Buffer.Write #42986
Labels
Documentation
NeedsDecision
Feedback is required from experts, contributors, and/or the community before a change can be made.
Performance
Milestone
Buffer.Bytes
is currently documented as:The documented use case for
Bytes
is that it's a view of the unread portion. If so, then I would expectlen(b) == cap(b)
for the returned buffer, but its not. Instead, the buffered returned is both the unread portion inb[:len(b)]
and the unwritten portion inb[len(b):]
.Thus, you can do something like:
Is this is an intended use-case for
Bytes
? From the implementation, I see no reason why this is invalid nor any significant hindrances it would impose on the internal implementation ofBuffer
. This technique allows users ofBuffer
to avoid allocating a temporary local buffer and an unnecessary copy if they already wrote the data directly into the internal buffer.EDIT: The suggestion below is not necessary as pointed out by @cherrymui.
If this is an appropriate use of theBytes
method, then I suggest thatWrite
have a special case where it trivially reslices the internal buffer if the input slice aliases the next buffer segment:The text was updated successfully, but these errors were encountered: