github.com/Schaudge/grailbase@v0.0.0-20240223061707-44c758a471c0/ioctx/std.go (about)

     1  package ioctx
     2  
     3  import (
     4  	"context"
     5  	"io"
     6  )
     7  
     8  type (
     9  	fromStdReader   struct{ io.Reader }
    10  	fromStdWriter   struct{ io.Writer }
    11  	fromStdCloser   struct{ io.Closer }
    12  	fromStdSeeker   struct{ io.Seeker }
    13  	fromStdReaderAt struct{ io.ReaderAt }
    14  	fromStdWriterAt struct{ io.WriterAt }
    15  
    16  	StdReader struct {
    17  		Ctx context.Context
    18  		Reader
    19  	}
    20  	StdWriter struct {
    21  		Ctx context.Context
    22  		Writer
    23  	}
    24  	StdCloser struct {
    25  		Ctx context.Context
    26  		Closer
    27  	}
    28  	StdSeeker struct {
    29  		Ctx context.Context
    30  		Seeker
    31  	}
    32  	StdReadCloser struct {
    33  		Ctx context.Context
    34  		ReadCloser
    35  	}
    36  	StdReaderAt struct {
    37  		Ctx context.Context
    38  		ReaderAt
    39  	}
    40  	StdWriterAt struct {
    41  		Ctx context.Context
    42  		WriterAt
    43  	}
    44  )
    45  
    46  // FromStdReader wraps io.Reader as Reader.
    47  func FromStdReader(r io.Reader) Reader { return fromStdReader{r} }
    48  
    49  func (r fromStdReader) Read(_ context.Context, dst []byte) (n int, err error) {
    50  	return r.Reader.Read(dst)
    51  }
    52  
    53  // FromStdWriter wraps io.Writer as Writer.
    54  func FromStdWriter(w io.Writer) Writer { return fromStdWriter{w} }
    55  
    56  func (w fromStdWriter) Write(_ context.Context, p []byte) (n int, err error) {
    57  	return w.Writer.Write(p)
    58  }
    59  
    60  // FromStdCloser wraps io.Closer as Closer.
    61  func FromStdCloser(c io.Closer) Closer { return fromStdCloser{c} }
    62  
    63  func (c fromStdCloser) Close(context.Context) error { return c.Closer.Close() }
    64  
    65  // FromStdSeeker wraps io.Seeker as Seeker.
    66  func FromStdSeeker(s io.Seeker) Seeker { return fromStdSeeker{s} }
    67  
    68  func (s fromStdSeeker) Seek(_ context.Context, offset int64, whence int) (int64, error) {
    69  	return s.Seeker.Seek(offset, whence)
    70  }
    71  
    72  // FromStdReadCloser wraps io.ReadCloser as ReadCloser.
    73  func FromStdReadCloser(rc io.ReadCloser) ReadCloser {
    74  	return struct {
    75  		Reader
    76  		Closer
    77  	}{FromStdReader(rc), FromStdCloser(rc)}
    78  }
    79  
    80  // FromStdReadSeeker wraps io.ReadSeeker as ReadSeeker.
    81  func FromStdReadSeeker(rs io.ReadSeeker) ReadSeeker {
    82  	return struct {
    83  		Reader
    84  		Seeker
    85  	}{FromStdReader(rs), FromStdSeeker(rs)}
    86  }
    87  
    88  // FromStdReaderAt wraps io.ReaderAt as ReaderAt.
    89  func FromStdReaderAt(r io.ReaderAt) ReaderAt { return fromStdReaderAt{r} }
    90  
    91  func (r fromStdReaderAt) ReadAt(_ context.Context, dst []byte, off int64) (n int, err error) {
    92  	return r.ReaderAt.ReadAt(dst, off)
    93  }
    94  
    95  // ToStdReader wraps Reader as io.Reader.
    96  func ToStdReader(ctx context.Context, r Reader) io.Reader { return StdReader{ctx, r} }
    97  
    98  func (r StdReader) Read(dst []byte) (n int, err error) {
    99  	return r.Reader.Read(r.Ctx, dst)
   100  }
   101  
   102  // ToStdWriter wraps Writer as io.Writer.
   103  func ToStdWriter(ctx context.Context, w Writer) io.Writer { return StdWriter{ctx, w} }
   104  
   105  func (w StdWriter) Write(p []byte) (n int, err error) {
   106  	return w.Writer.Write(w.Ctx, p)
   107  }
   108  
   109  // ToStdCloser wraps Closer as io.Closer.
   110  func ToStdCloser(ctx context.Context, c Closer) io.Closer { return StdCloser{ctx, c} }
   111  
   112  func (c StdCloser) Close() error {
   113  	return c.Closer.Close(c.Ctx)
   114  }
   115  
   116  // ToStdSeeker wraps Seeker as io.Seeker.
   117  func ToStdSeeker(ctx context.Context, s Seeker) io.Seeker { return StdSeeker{ctx, s} }
   118  
   119  func (r StdSeeker) Seek(offset int64, whence int) (int64, error) {
   120  	return r.Seeker.Seek(r.Ctx, offset, whence)
   121  }
   122  
   123  // ToStdReadCloser wraps ReadCloser as io.ReadCloser.
   124  func ToStdReadCloser(ctx context.Context, rc ReadCloser) io.ReadCloser {
   125  	return struct {
   126  		io.Reader
   127  		io.Closer
   128  	}{ToStdReader(ctx, rc), ToStdCloser(ctx, rc)}
   129  }
   130  
   131  // ToStdReadSeeker wraps ReadSeeker as io.ReadSeeker.
   132  func ToStdReadSeeker(ctx context.Context, rs ReadSeeker) io.ReadSeeker {
   133  	return struct {
   134  		io.Reader
   135  		io.Seeker
   136  	}{ToStdReader(ctx, rs), ToStdSeeker(ctx, rs)}
   137  }
   138  
   139  // ToStdReaderAt wraps ReaderAt as io.ReaderAt.
   140  func ToStdReaderAt(ctx context.Context, r ReaderAt) io.ReaderAt { return StdReaderAt{ctx, r} }
   141  
   142  func (r StdReaderAt) ReadAt(dst []byte, off int64) (n int, err error) {
   143  	return r.ReaderAt.ReadAt(r.Ctx, dst, off)
   144  }
   145  
   146  // ToStdWriterAt wraps WriterAt as io.WriterAt.
   147  func ToStdWriterAt(ctx context.Context, w WriterAt) io.WriterAt { return StdWriterAt{ctx, w} }
   148  
   149  func (w StdWriterAt) WriteAt(dst []byte, off int64) (n int, err error) {
   150  	return w.WriterAt.WriteAt(w.Ctx, dst, off)
   151  }