1
1
mirror of https://github.com/wader/fq.git synced 2024-11-25 23:13:19 +03:00
fq/internal/iox/iox.go

69 lines
1.0 KiB
Go
Raw Normal View History

package iox
2020-06-08 03:29:51 +03:00
import (
"context"
2020-06-08 03:29:51 +03:00
"errors"
"io"
)
func SeekerEnd(s io.Seeker) (int64, error) {
cPos, err := s.Seek(0, io.SeekCurrent)
if err != nil {
return 0, err
}
epos, err := s.Seek(0, io.SeekEnd)
if err != nil {
return 0, err
}
if _, err := s.Seek(cPos, io.SeekStart); err != nil {
return 0, err
}
return epos, nil
}
2021-09-20 18:05:24 +03:00
type ReadErrSeeker struct{ io.Reader }
2020-06-08 03:29:51 +03:00
2021-09-20 18:05:24 +03:00
func (r *ReadErrSeeker) Seek(offset int64, whence int) (int64, error) {
return 0, errors.New("seek")
}
type CtxWriter struct {
io.Writer
Ctx context.Context
}
func (o CtxWriter) Write(p []byte) (n int, err error) {
if o.Ctx != nil {
if err := o.Ctx.Err(); err != nil {
return 0, err
}
}
return o.Writer.Write(p)
}
type DiscardCtxWriter struct {
Ctx context.Context
}
func (o DiscardCtxWriter) Write(p []byte) (n int, err error) {
if o.Ctx != nil {
if err := o.Ctx.Err(); err != nil {
return 0, err
}
}
return n, nil
}
func Unwrap(r any) any {
for {
u, ok := r.(interface {
Unwrap() any
})
if !ok {
return r
}
r = u.Unwrap()
}
}