forked from lithdew/flatend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathio.go
41 lines (35 loc) · 885 Bytes
/
io.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package flatend
import (
"errors"
"fmt"
"io"
)
type pipeReader struct {
*io.PipeReader
}
func (r *pipeReader) Read(buf []byte) (n int, err error) {
n, err = r.PipeReader.Read(buf)
if err != nil && errors.Is(err, io.ErrClosedPipe) {
err = io.EOF
}
return n, err
}
type pipeWriter struct {
*io.PipeWriter
}
func (w *pipeWriter) Write(buf []byte) (n int, err error) {
n, err = w.PipeWriter.Write(buf)
if err != nil && errors.Is(err, io.ErrClosedPipe) {
err = fmt.Errorf("%s: %w", err, io.EOF)
}
return n, err
}
// createWrappedPipe wraps around a reader/writer pair from io.Pipe() such that all
// errors reported by such reader/writer pair that comprise of io.ErrClosedPipe
// will be wrapped with io.EOF.
func createWrappedPipe() (*pipeReader, *pipeWriter) {
r, w := io.Pipe()
pr := &pipeReader{PipeReader: r}
pw := &pipeWriter{PipeWriter: w}
return pr, pw
}