github.com/haraldrudell/parl@v0.4.176/pio/copy-context.go (about)

     1  /*
     2  © 2023–present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/)
     3  ISC License
     4  */
     5  
     6  // Package pio provides a context-cancelable stream copier, a closable buffer, line-based reader
     7  // and other io functions
     8  package pio
     9  
    10  import (
    11  	"context"
    12  	"io"
    13  )
    14  
    15  // CopyContext is like [io.Copy] but is cancelable via context
    16  //   - CopyContext closes both reader and writer if their runtime type is closable
    17  //   - context cancel is detected on any [io.File.Read] or [io.File.Write] invocation and
    18  //     carried out by parallel [io.File.Close] if either reader or writer is closable
    19  //   - may launch 1 thread while copying
    20  //   - err may be [context.Canceled]
    21  func CopyContext(dst io.Writer, src io.Reader, buf []byte, ctx context.Context) (written int64, err error) {
    22  	return NewContextCopier(buf).Copy(dst, src, ctx)
    23  }
    24  
    25  var _ = io.Copy