github.com/mutagen-io/mutagen@v0.18.0-rc1/pkg/multiplexing/carrier.go (about) 1 package multiplexing 2 3 import ( 4 "bufio" 5 "io" 6 ) 7 8 // Carrier is the interface that the streams used for multiplexing must 9 // implement. It imposes the additional constraint that the Close method must 10 // unblock any pending read, discard, or write operations. This interface can be 11 // implemented by custom code, but it can also be implemented efficiently using 12 // the NewCarrierFromStream function. 13 type Carrier interface { 14 io.Reader 15 io.ByteReader 16 // Discard attempts to discard the next n bytes from the stream, returning 17 // the number of bytes discarded and any error that occurred. The returned 18 // error must be non-nil if and only if discarded != n. 19 Discard(n int) (discarded int, err error) 20 io.Writer 21 io.Closer 22 } 23 24 // bufioCarrier is a Carrier implementation that can be used to adapt an 25 // underlying io.ReadWriteCloser to a Carrier. 26 type bufioCarrier struct { 27 *bufio.Reader 28 io.Writer 29 io.Closer 30 } 31 32 // NewCarrierFromStream constructs a new Carrier by wrapping an underlying 33 // io.ReadWriteCloser. The underlying stream must have the property that its 34 // Close method unblocks any pending Read or Write calls. 35 func NewCarrierFromStream(stream io.ReadWriteCloser) Carrier { 36 return &bufioCarrier{ 37 bufio.NewReader(stream), 38 stream, 39 stream, 40 } 41 }