github.com/rclone/rclone@v1.66.1-0.20240517100346-7b89735ae726/lib/readers/context.go (about) 1 // Package readers provides io.Reader related utilities. 2 package readers 3 4 import ( 5 "context" 6 "io" 7 ) 8 9 // NewContextReader creates a reader, that returns any errors that ctx gives 10 func NewContextReader(ctx context.Context, r io.Reader) io.Reader { 11 return &contextReader{ 12 ctx: ctx, 13 r: r, 14 } 15 } 16 17 type contextReader struct { 18 ctx context.Context 19 r io.Reader 20 } 21 22 // Read bytes as per io.Reader interface 23 func (cr *contextReader) Read(p []byte) (n int, err error) { 24 err = cr.ctx.Err() 25 if err != nil { 26 return 0, err 27 } 28 return cr.r.Read(p) 29 }