github.com/haraldrudell/parl@v0.4.176/pio/write-closer-to-chan.go (about) 1 /* 2 © 2021–present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/) 3 ISC License 4 */ 5 6 package pio 7 8 import ( 9 "io" 10 "io/fs" 11 12 "github.com/haraldrudell/parl" 13 "github.com/haraldrudell/parl/perrors" 14 ) 15 16 type WriteCloserToChan struct { 17 ch parl.NBChan[[]byte] 18 } 19 20 func NewWriteCloserToChan() (writeCloser io.WriteCloser) { 21 return &WriteCloserToChan{} 22 } 23 24 func InitWriteCloserToChan(wcp *WriteCloserToChan) {} 25 26 func (wc *WriteCloserToChan) Write(p []byte) (n int, err error) { 27 if wc.ch.DidClose() { 28 err = perrors.ErrorfPF(fs.ErrClosed.Error()) 29 return 30 } 31 wc.ch.Send(p) 32 n = len(p) 33 return 34 } 35 func (wc *WriteCloserToChan) Close() (err error) { 36 wc.ch.Close() 37 return 38 } 39 40 func (wc *WriteCloserToChan) Ch() (readCh <-chan []byte) { 41 return wc.ch.Ch() 42 }