2022-08-28 13:21:57 +02:00
|
|
|
// Package readers provides io.Reader related utilities.
|
2021-03-25 16:53:16 +01:00
|
|
|
package readers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io"
|
|
|
|
)
|
|
|
|
|
|
|
|
// NewContextReader creates a reader, that returns any errors that ctx gives
|
|
|
|
func NewContextReader(ctx context.Context, r io.Reader) io.Reader {
|
|
|
|
return &contextReader{
|
|
|
|
ctx: ctx,
|
|
|
|
r: r,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type contextReader struct {
|
|
|
|
ctx context.Context
|
|
|
|
r io.Reader
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read bytes as per io.Reader interface
|
|
|
|
func (cr *contextReader) Read(p []byte) (n int, err error) {
|
|
|
|
err = cr.ctx.Err()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return cr.r.Read(p)
|
|
|
|
}
|