Debian Codegraph @

_test/issue-1425.go

raw symbols SCIP
1 package main
2
3 import (
4 "io"
5 "log"
6 "os"
7 "strings"
8 )
9
10 type WrappedReader struct {
11 reader io.Reader
12 }
13
14 func (wr WrappedReader) Read(p []byte) (n int, err error) {
15 return wr.reader.Read(p)
16 }
17
18 // Of course, this implementation is completely stupid because it does not write
19 // to the intended writer, as any honest WriteTo implementation should. its
20 // implemtion is just to make obvious the divergence of behaviour with yaegi.
21 func (wr WrappedReader) WriteTo(w io.Writer) (n int64, err error) {
22 // Ignore w, send to Stdout to prove whether this WriteTo is used.
23 data, err := io.ReadAll(wr)
24 if err != nil {
25 return 0, err
26 }
27 nn, err := os.Stdout.Write(data)
28 return int64(nn), err
29 }
30
31 func main() {
32 f := strings.NewReader("hello world")
33 wr := WrappedReader{reader: f}
34
35 // behind the scenes, io.Copy is supposed to use wr.WriteTo if the implementation exists.
36 // With Go, it works as expected, i.e. the output is sent to os.Stdout.
37 // With Yaegi, it doesn't, i.e. the output is sent to io.Discard.
38 if _, err := io.Copy(io.Discard, wr); err != nil {
39 log.Fatal(err)
40 }
41 }
42
43 // Output:
44 // hello world