github.com/haraldrudell/parl@v0.4.176/closer.go (about) 1 /* 2 © 2022–present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/) 3 ISC License 4 */ 5 6 package parl 7 8 import ( 9 "io" 10 11 "github.com/haraldrudell/parl/perrors" 12 ) 13 14 // Closer is a deferrable function that closes a channel. 15 // - if errp is non-nil, panic values updates it using errors.AppendError. 16 // - Closer is thread-safe, panic-free and deferrable 17 func Closer[T any](ch chan T, errp *error) { 18 defer PanicToErr(errp) 19 20 close(ch) 21 } 22 23 // CloserSend is a deferrable function that closes a send-channel. 24 // - if errp is non-nil, panic values updates it using errors.AppendError. 25 // - CloserSend is thread-safe, panic-free and deferrable 26 func CloserSend[T any](ch chan<- T, errp *error) { 27 defer PanicToErr(errp) 28 29 close(ch) 30 } 31 32 // Close closes an io.Closer object. 33 // - if errp is non-nil, panic values updates it using errors.AppendError. 34 // - Close is thread-safe, panic-free and deferrable 35 // - type Closer interface { Close() error } 36 func Close(closable io.Closer, errp *error) { 37 var err error 38 defer RecoverErr(func() DA { return A() }, errp) 39 40 if err = closable.Close(); perrors.IsPF(&err, "%w", err) { 41 *errp = perrors.AppendError(*errp, err) 42 } 43 }