Documentation ¶
Overview ¶
Package xio provides addons to standard package io.
- Reader, Writer, ReadWriter, etc are io analogs that add support for contexts.
- BindCtx*(X, ctx) converts xio.X into io.X that implicitly passes ctx to xio.X and can be used in legacy code.
- WithCtx*(X) converts io.X back into xio.X that accepts context. It is the opposite operation for BindCtx, but for arbitrary io.X returned xio.X handles context only on best-effort basis. In particular IO cancellation is not reliably handled for os.File .
- Pipe amends io.Pipe and creates synchronous in-memory pipe that supports IO cancellation.
Miscellaneous utilities:
- CountReader provides InputOffset for a Reader.
Index ¶
- func BindCtxR(r Reader, ctx context.Context) io.Reader
- func BindCtxRC(r ReadCloser, ctx context.Context) io.ReadCloser
- func BindCtxRW(rw ReadWriter, ctx context.Context) io.ReadWriter
- func BindCtxRWC(rw ReadWriteCloser, ctx context.Context) io.ReadWriteCloser
- func BindCtxW(w Writer, ctx context.Context) io.Writer
- func BindCtxWC(w WriteCloser, ctx context.Context) io.WriteCloser
- func Pipe() (*PipeReader, *PipeWriter)
- type CountedReader
- type PipeReader
- type PipeWriter
- type ReadCloser
- type ReadWriteCloser
- type ReadWriter
- type Reader
- type WriteCloser
- type Writer
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BindCtxRC ¶
func BindCtxRC(r ReadCloser, ctx context.Context) io.ReadCloser
BindCtxRC binds ReadCloser r and ctx into io.ReadCloser which passes ctx to r on every Read.
func BindCtxRW ¶
func BindCtxRW(rw ReadWriter, ctx context.Context) io.ReadWriter
BindCtxRW binds ReadWriter rw and ctx into io.ReadWriter which passes ctx to rw on every Read and Write.
func BindCtxRWC ¶
func BindCtxRWC(rw ReadWriteCloser, ctx context.Context) io.ReadWriteCloser
BindCtxRWC binds ReadWriteCloser rw and ctx into io.ReadWriteCloser which passes ctx to rw on every Read and Write.
func BindCtxW ¶
BindCtxW binds Writer w and ctx into io.Writer which passes ctx to w on every Write.
func BindCtxWC ¶
func BindCtxWC(w WriteCloser, ctx context.Context) io.WriteCloser
BindCtxWC binds WriteCloser w and ctx into io.WriteCloser which passes ctx to w on every Write.
func Pipe ¶
func Pipe() (*PipeReader, *PipeWriter)
Pipe creates a synchronous in-memory pipe. It can be used to connect code expecting a xio.Reader with code expecting a xio.Writer.
Reads and Writes on the pipe are matched one to one except when multiple Reads are needed to consume a single Write. That is, each Write to the PipeWriter blocks until it has satisfied one or more Reads from the PipeReader that fully consume the written data. The data is copied directly from the Write to the corresponding Read (or Reads); there is no internal buffering.
It is safe to call Read and Write in parallel with each other or with Close. Parallel calls to Read and parallel calls to Write are also safe: the individual calls will be gated sequentially.
Pipe is similar to io.Pipe but additionally provides cancellation support for Read and Write.
Types ¶
type CountedReader ¶
type CountedReader struct {
// contains filtered or unexported fields
}
CountedReader is a Reader that count total bytes read.
func CountReader ¶
func CountReader(r Reader) *CountedReader
CountReader wraps r with CountedReader.
func (*CountedReader) InputOffset ¶
func (cr *CountedReader) InputOffset() int64
InputOffset returns the number of bytes read.
type PipeReader ¶
type PipeReader struct {
// contains filtered or unexported fields
}
A PipeReader is the read half of a pipe.
It is similar to io.PipeReader, but additionally provides cancellation support for Read.
func (*PipeReader) Close ¶
func (r *PipeReader) Close() error
Close closes the reader; subsequent writes to the write half of the pipe will return the error io.ErrClosedPipe.
func (*PipeReader) CloseWithError ¶
func (r *PipeReader) CloseWithError(err error) error
CloseWithError closes the reader; subsequent writes to the write half of the pipe will return the error err.
CloseWithError never overwrites the previous error if it exists and always returns nil.
type PipeWriter ¶
type PipeWriter struct {
// contains filtered or unexported fields
}
A PipeWriter is the write half of a pipe.
It is similar to io.PipeWriter, but additionally provides cancellation support for Write.
func (*PipeWriter) Close ¶
func (w *PipeWriter) Close() error
Close closes the writer; subsequent reads from the read half of the pipe will return no bytes and EOF.
func (*PipeWriter) CloseWithError ¶
func (w *PipeWriter) CloseWithError(err error) error
CloseWithError closes the writer; subsequent reads from the read half of the pipe will return no bytes and the error err, or EOF if err is nil.
CloseWithError never overwrites the previous error if it exists and always returns nil.
type ReadCloser ¶
ReadCloser combines Reader and io.Closer.
func WithCtxRC ¶
func WithCtxRC(r io.ReadCloser) ReadCloser
WithCtxRC converts io.ReadCloser r into ReadCloser that accepts ctx.
It returns original IO object if r was created via BindCtx*, but in general returned ReadCloser will handle context only on best-effort basis.
type ReadWriteCloser ¶
ReadWriteCloser combines Reader, Writer and io.Closer.
func WithCtxRWC ¶
func WithCtxRWC(rw io.ReadWriteCloser) ReadWriteCloser
WithCtxRWC converts io.ReadWriteCloser rw into ReadWriteCloser that accepts ctx.
It returns original IO object if rw was created via BindCtx*, but in general returned ReadWriteCloser will handle context only on best-effort basis.
type ReadWriter ¶
ReadWriter combines Reader and Writer.
func WithCtxRW ¶
func WithCtxRW(rw io.ReadWriter) ReadWriter
WithCtxRW converts io.ReadWriter rw into ReadWriter that accepts ctx.
It returns original IO object if rw was created via BindCtx*, but in general returned ReadWriter will handle context only on best-effort basis.
type WriteCloser ¶
WriteCloser combines Writer and io.Closer.
func WithCtxWC ¶
func WithCtxWC(w io.WriteCloser) WriteCloser
WithCtxWC converts io.WriteCloser w into WriteCloser that accepts ctx.
It returns original IO object if w was created via BindCtx*, but in general returned WriteCloser will handle context only on best-effort basis.