xio

package
v0.0.0-...-8299741 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 7, 2024 License: GPL-3.0 Imports: 3 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func BindCtxR

func BindCtxR(r Reader, ctx context.Context) io.Reader

BindCtxR binds Reader r and ctx into io.Reader which passes ctx to r on every Read.

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

func BindCtxW(w Writer, ctx context.Context) io.Writer

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.

func (*CountedReader) Read

func (cr *CountedReader) Read(ctx context.Context, p []byte) (int, error)

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.

func (*PipeReader) Read

func (r *PipeReader) Read(ctx context.Context, data []byte) (n int, err error)

Read implements xio.Reader interface: it reads data from the pipe, blocking until a writer arrives or the write end is closed. If the write end is closed with an error, that error is returned as err; otherwise err is EOF.

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.

func (*PipeWriter) Write

func (w *PipeWriter) Write(ctx context.Context, data []byte) (n int, err error)

Write implements xio.Writer interface: it writes data to the pipe, blocking until one or more readers have consumed all the data or the read end is closed. If the read end is closed with an error, that err is returned as err; otherwise err is io.ErrClosedPipe.

type ReadCloser

type ReadCloser interface {
	Reader
	io.Closer
}

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

type ReadWriteCloser interface {
	Reader
	Writer
	io.Closer
}

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

type ReadWriter interface {
	Reader
	Writer
}

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 Reader

type Reader interface {
	Read(ctx context.Context, dst []byte) (n int, err error)
}

Reader is like io.Reader but additionally takes context for Read.

func WithCtxR

func WithCtxR(r io.Reader) Reader

WithCtxR converts io.Reader r into Reader that accepts ctx.

It returns original IO object if r was created via BindCtx*, but in general returned Reader will handle context only on best-effort basis.

type WriteCloser

type WriteCloser interface {
	Writer
	io.Closer
}

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.

type Writer

type Writer interface {
	Write(ctx context.Context, src []byte) (n int, err error)
}

Writer is like io.Writer but additionally takes context for Write.

func WithCtxW

func WithCtxW(w io.Writer) Writer

WithCtxW converts io.Writer w into Writer that accepts ctx.

It returns original IO object if w was created via BindCtx*, but in general returned Writer will handle context only on best-effort basis.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL