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