go123: lab.nexedi.com/kirr/go123/xio Index | Files

package xio

import "lab.nexedi.com/kirr/go123/xio"

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

Package Files

pipe.go xio.go

func BindCtxR Uses

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 Uses

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 Uses

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 Uses

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 Uses

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 Uses

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 Uses

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.

type CountedReader Uses

type CountedReader struct {
    // contains filtered or unexported fields
}

CountedReader is a Reader that count total bytes read.

func CountReader Uses

func CountReader(r Reader) *CountedReader

CountReader wraps r with CountedReader.

func (*CountedReader) InputOffset Uses

func (cr *CountedReader) InputOffset() int64

InputOffset returns the number of bytes read.

func (*CountedReader) Read Uses

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

type PipeReader Uses

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 Uses

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 Uses

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 Uses

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 Uses

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 Uses

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 Uses

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 Uses

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 Uses

type ReadCloser interface {
    Reader
    io.Closer
}

ReadCloser combines Reader and io.Closer.

func WithCtxRC Uses

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 Uses

type ReadWriteCloser interface {
    Reader
    Writer
    io.Closer
}

ReadWriteCloser combines Reader, Writer and io.Closer.

func WithCtxRWC Uses

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 Uses

type ReadWriter interface {
    Reader
    Writer
}

ReadWriter combines Reader and Writer.

func WithCtxRW Uses

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 Uses

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 Uses

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 Uses

type WriteCloser interface {
    Writer
    io.Closer
}

WriteCloser combines Writer and io.Closer.

func WithCtxWC Uses

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 Uses

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 Uses

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.

Package xio imports 3 packages (graph) and is imported by 2 packages. Updated 2020-06-03. Refresh now. Tools for package owners.