From 3bdc1799d6ce441d7a972faf1452e34b6dce0826 Mon Sep 17 00:00:00 2001 From: Roger Peppe Date: Wed, 11 Aug 2021 12:14:41 +0100 Subject: [PATCH] io: unexport internal methods MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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í Reviewed-by: Joe Tsai Reviewed-by: Joe Tsai Trust: Daniel Martí --- src/io/pipe.go | 50 ++++++++++++++++++++++++++------------------------ 1 file changed, 26 insertions(+), 24 deletions(-) diff --git a/src/io/pipe.go b/src/io/pipe.go index b5343bb6b73..2724e3f7abe 100644 --- a/src/io/pipe.go +++ b/src/io/pipe.go @@ -47,7 +47,7 @@ type pipe struct { werr onceError } -func (p *pipe) Read(b []byte) (n int, err error) { +func (p *pipe) read(b []byte) (n int, err error) { select { case <-p.done: return 0, p.readCloseError() @@ -64,15 +64,7 @@ func (p *pipe) Read(b []byte) (n int, err error) { } } -func (p *pipe) readCloseError() 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 { +func (p *pipe) closeRead(err error) error { if err == nil { err = ErrClosedPipe } @@ -81,7 +73,7 @@ func (p *pipe) CloseRead(err error) error { return nil } -func (p *pipe) Write(b []byte) (n int, err error) { +func (p *pipe) write(b []byte) (n int, err error) { select { case <-p.done: return 0, p.writeCloseError() @@ -103,15 +95,7 @@ func (p *pipe) Write(b []byte) (n int, err error) { return n, nil } -func (p *pipe) writeCloseError() 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 { +func (p *pipe) closeWrite(err error) error { if err == nil { err = EOF } @@ -120,6 +104,24 @@ func (p *pipe) CloseWrite(err error) error { 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. type PipeReader struct { p *pipe @@ -131,7 +133,7 @@ type PipeReader struct { // If the write end is closed with an error, that error is // returned as err; otherwise err is EOF. 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 @@ -146,7 +148,7 @@ func (r *PipeReader) Close() error { // CloseWithError never overwrites the previous error if it exists // and always returns nil. 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. @@ -160,7 +162,7 @@ type PipeWriter struct { // If the read end is closed with an error, that err is // returned as err; otherwise err is ErrClosedPipe. 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 @@ -176,7 +178,7 @@ func (w *PipeWriter) Close() error { // CloseWithError never overwrites the previous error if it exists // and always returns nil. func (w *PipeWriter) CloseWithError(err error) error { - return w.p.CloseWrite(err) + return w.p.closeWrite(err) } // Pipe creates a synchronous in-memory pipe.