-
Notifications
You must be signed in to change notification settings - Fork 0
/
disposable.go
56 lines (45 loc) · 1.01 KB
/
disposable.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
44
45
46
47
48
49
50
51
52
53
54
55
56
package lang
import "io"
// Disposable 接口用于释放对象持有的资源
type Disposable interface {
Dispose() error
}
type ReleasePool interface {
Release() error
Push(target Disposable)
}
func Dispose(t Disposable) error {
if t == nil {
return nil
}
return t.Dispose()
}
func Close(t io.Closer) error {
if t == nil {
return nil
}
return t.Close()
}
func Release(pool ReleasePool) error {
if pool == nil {
return nil
}
return pool.Release()
}
////////////////////////////////////////////////////////////////////////////////
// DisposableFunc Disposable 接口的函数形式
type DisposableFunc func() error
type disposableFuncAdapter struct {
fn DisposableFunc
}
func (inst *disposableFuncAdapter) Dispose() error {
fn := inst.fn
if fn == nil {
return nil
}
return fn()
}
// DisposableForFunc 函数创建一个 DisposableFunc 的包装
func DisposableForFunc(fn DisposableFunc) Disposable {
return &disposableFuncAdapter{fn: fn}
}