Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proposal: bytes: guarantee a useful zero value of Reader #28269

Closed
FiloSottile opened this issue Oct 18, 2018 · 5 comments
Closed

proposal: bytes: guarantee a useful zero value of Reader #28269

FiloSottile opened this issue Oct 18, 2018 · 5 comments

Comments

@FiloSottile
Copy link
Contributor

A zero bytes.Reader is almost functional (like a bytes.NewReader(nil)), except for the prevRune offset which is only used by UnreadRune. It would be nice to make the zero value useful and document it as such. For example we could make prevRune 1-indexed and have 0 be the sentinel previous operation was not ReadRune value. It already suspiciously truncates an int64 index into an int without checking anyway (which might be a bug AFAICT).

I found myself wishing for this in https://go-review.googlesource.com/c/go/+/142818 where I had a bytes.Reader field that would meaningfully sit empty at times, including at object creation.

@gopherbot gopherbot added this to the Proposal milestone Oct 18, 2018
@bradfitz
Copy link
Contributor

To be clear, you mean a bytes.Reader value and not a nil *bytes.Reader pointer, right? (Because it's the pointer type that implements io.Reader, etc.)

So you mean that a (perhaps implicit) pointer to a zero-value bytes.Reader should act like a bytes.NewReader(nil)?

If so, that might be okay (if we also do strings.Reader), but I would object to making a nil pointer act like an always-EOF reader.

@FiloSottile
Copy link
Contributor Author

Yes, correct, a zero bytes.Reader value, not a nil pointer. The idea is to avoid initialization of a bytes.Reader field, like bytes.Buffer and sync.Mutex allow.

The zero value for Buffer is an empty buffer ready to use.

The zero value for a Mutex is an unlocked mutex.

Looks like strings.Reader can be adapted just as easily.

@rsc
Copy link
Contributor

rsc commented Oct 24, 2018

We should definitely make new(bytes.Reader) and bytes.NewReader(nil) behave identically. There's no need to redefine prevRune necessarily - there's enough other info in the struct to understand that there is no previous (i==0 for example). It looks like UnreadRune just needs the same 3-line if statement as in UnreadByte added to it (not replacing the existing if statement, which is something else).

Marking accepted from an API point of view.

@gopherbot
Copy link

Change https://golang.org/cl/145098 mentions this issue: bytes: fix Reader.UnreadRune returning without error on a zero Reader

@gopherbot
Copy link

Change https://golang.org/cl/145737 mentions this issue: crypto/tls: remove unneeded calls to bytes.NewReader

gopherbot pushed a commit that referenced this issue Oct 30, 2018
Updates #28269

Change-Id: Iae765f85e6ae49f4b581161ed489b2f5ee27cdba
Reviewed-on: https://go-review.googlesource.com/c/145737
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
@golang golang locked and limited conversation to collaborators Oct 29, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants