-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathitem.go
More file actions
47 lines (38 loc) · 1.18 KB
/
item.go
File metadata and controls
47 lines (38 loc) · 1.18 KB
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
package pool
// ItemWrap wraps the item returned by the pool's factory.
type ItemWrap[T any] interface {
// Return returns the item back to the pool.
Return()
// MarkAsInvalid marks the item as invalid (eg. unusable, unstable or broken) so
// that after it gets returned to the pool, it is discarded. It will eventually
// get garbage collected.
MarkAsInvalid()
// Item represents the unwrapped item borrowed from the pool.
Item() T
}
// itemWrap wraps the item returned by the pool's factory.
type itemWrap[T any] struct {
item T
invalid bool
pool interface{ returnItem(*itemWrap[T]) }
}
// Item represents the unwrapped item borrowed from the pool.
func (iw *itemWrap[T]) Item() T {
return iw.item
}
// Return returns the item back to the pool.
func (iw *itemWrap[T]) Return() {
iw.pool.returnItem(iw)
}
// reset restores iw to the zero value.
func (iw *itemWrap[T]) reset() {
iw.item = *new(T)
iw.invalid = false
iw.pool = nil
}
// MarkAsInvalid marks the item as invalid (eg. unusable, unstable or broken) so
// that after it gets returned to the pool, it is discarded. It will eventually
// get garbage collected.
func (iw *itemWrap[T]) MarkAsInvalid() {
iw.invalid = true
}