Skip to content

Commit 9ae97f8

Browse files
ianlancetaylorgopherbot
authored andcommitted
os: don't hide all methods in recursive call to io.Copy
In order to avoid a recursive call to ReadFrom, we were converting a *File to an io.Writer. But all we really need to do is hide the ReadFrom method. In particular, this gives us the option of adding a WriteTo method. For #58808 Change-Id: I20d3a45749d528c93c23267c467e607fc17dc83f Reviewed-on: https://go-review.googlesource.com/c/go/+/475535 Reviewed-by: Bryan Mills <[email protected]> Auto-Submit: Ian Lance Taylor <[email protected]> Run-TryBot: Ian Lance Taylor <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Cherry Mui <[email protected]>
1 parent ebf8e26 commit 9ae97f8

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

src/os/file.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,11 +158,19 @@ func (f *File) ReadFrom(r io.Reader) (n int64, err error) {
158158
}
159159

160160
func genericReadFrom(f *File, r io.Reader) (int64, error) {
161-
return io.Copy(onlyWriter{f}, r)
161+
return io.Copy(fileWithoutReadFrom{f}, r)
162162
}
163163

164-
type onlyWriter struct {
165-
io.Writer
164+
// fileWithoutReadFrom implements all the methods of *File other
165+
// than ReadFrom. This is used to permit ReadFrom to call io.Copy
166+
// without leading to a recursive call to ReadFrom.
167+
type fileWithoutReadFrom struct {
168+
*File
169+
}
170+
171+
// This ReadFrom method hides the *File ReadFrom method.
172+
func (fileWithoutReadFrom) ReadFrom(fileWithoutReadFrom) {
173+
panic("unreachable")
166174
}
167175

168176
// Write writes len(b) bytes from b to the File.

0 commit comments

Comments
 (0)