1
0
mirror of https://github.com/golang/go synced 2024-11-23 00:10:07 -07:00

io: unexport internal methods

The methods on the pipe type don't need to be exported. Doing so sets
a bad precedent that it's OK to export methods to indicate an internal
public API.  That's not a good idea in general, because exported methods
increase cognitive load when reading code: the reader needs to consider
whether the exported method might be used via some external interface
or reflection.

Change-Id: Ib13f1b3f9fe0ff251628f31b776182a0953268ee
Reviewed-on: https://go-review.googlesource.com/c/go/+/341409
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
Reviewed-by: Joe Tsai <joetsai@digital-static.net>
Trust: Daniel Martí <mvdan@mvdan.cc>
This commit is contained in:
Roger Peppe 2021-08-11 12:14:41 +01:00 committed by roger peppe
parent 740f7d7370
commit 3bdc1799d6

View File

@ -47,7 +47,7 @@ type pipe struct {
werr onceError werr onceError
} }
func (p *pipe) Read(b []byte) (n int, err error) { func (p *pipe) read(b []byte) (n int, err error) {
select { select {
case <-p.done: case <-p.done:
return 0, p.readCloseError() return 0, p.readCloseError()
@ -64,15 +64,7 @@ func (p *pipe) Read(b []byte) (n int, err error) {
} }
} }
func (p *pipe) readCloseError() error { func (p *pipe) closeRead(err error) error {
rerr := p.rerr.Load()
if werr := p.werr.Load(); rerr == nil && werr != nil {
return werr
}
return ErrClosedPipe
}
func (p *pipe) CloseRead(err error) error {
if err == nil { if err == nil {
err = ErrClosedPipe err = ErrClosedPipe
} }
@ -81,7 +73,7 @@ func (p *pipe) CloseRead(err error) error {
return nil return nil
} }
func (p *pipe) Write(b []byte) (n int, err error) { func (p *pipe) write(b []byte) (n int, err error) {
select { select {
case <-p.done: case <-p.done:
return 0, p.writeCloseError() return 0, p.writeCloseError()
@ -103,15 +95,7 @@ func (p *pipe) Write(b []byte) (n int, err error) {
return n, nil return n, nil
} }
func (p *pipe) writeCloseError() error { func (p *pipe) closeWrite(err error) error {
werr := p.werr.Load()
if rerr := p.rerr.Load(); werr == nil && rerr != nil {
return rerr
}
return ErrClosedPipe
}
func (p *pipe) CloseWrite(err error) error {
if err == nil { if err == nil {
err = EOF err = EOF
} }
@ -120,6 +104,24 @@ func (p *pipe) CloseWrite(err error) error {
return nil return nil
} }
// readCloseError is considered internal to the pipe type.
func (p *pipe) readCloseError() error {
rerr := p.rerr.Load()
if werr := p.werr.Load(); rerr == nil && werr != nil {
return werr
}
return ErrClosedPipe
}
// writeCloseError is considered internal to the pipe type.
func (p *pipe) writeCloseError() error {
werr := p.werr.Load()
if rerr := p.rerr.Load(); werr == nil && rerr != nil {
return rerr
}
return ErrClosedPipe
}
// A PipeReader is the read half of a pipe. // A PipeReader is the read half of a pipe.
type PipeReader struct { type PipeReader struct {
p *pipe p *pipe
@ -131,7 +133,7 @@ type PipeReader struct {
// If the write end is closed with an error, that error is // If the write end is closed with an error, that error is
// returned as err; otherwise err is EOF. // returned as err; otherwise err is EOF.
func (r *PipeReader) Read(data []byte) (n int, err error) { func (r *PipeReader) Read(data []byte) (n int, err error) {
return r.p.Read(data) return r.p.read(data)
} }
// Close closes the reader; subsequent writes to the // Close closes the reader; subsequent writes to the
@ -146,7 +148,7 @@ func (r *PipeReader) Close() error {
// CloseWithError never overwrites the previous error if it exists // CloseWithError never overwrites the previous error if it exists
// and always returns nil. // and always returns nil.
func (r *PipeReader) CloseWithError(err error) error { func (r *PipeReader) CloseWithError(err error) error {
return r.p.CloseRead(err) return r.p.closeRead(err)
} }
// A PipeWriter is the write half of a pipe. // A PipeWriter is the write half of a pipe.
@ -160,7 +162,7 @@ type PipeWriter struct {
// If the read end is closed with an error, that err is // If the read end is closed with an error, that err is
// returned as err; otherwise err is ErrClosedPipe. // returned as err; otherwise err is ErrClosedPipe.
func (w *PipeWriter) Write(data []byte) (n int, err error) { func (w *PipeWriter) Write(data []byte) (n int, err error) {
return w.p.Write(data) return w.p.write(data)
} }
// Close closes the writer; subsequent reads from the // Close closes the writer; subsequent reads from the
@ -176,7 +178,7 @@ func (w *PipeWriter) Close() error {
// CloseWithError never overwrites the previous error if it exists // CloseWithError never overwrites the previous error if it exists
// and always returns nil. // and always returns nil.
func (w *PipeWriter) CloseWithError(err error) error { func (w *PipeWriter) CloseWithError(err error) error {
return w.p.CloseWrite(err) return w.p.closeWrite(err)
} }
// Pipe creates a synchronous in-memory pipe. // Pipe creates a synchronous in-memory pipe.