Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions pkg/xsync/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -343,19 +343,18 @@ func (s *SyncMap[K, V]) ToMap() map[K]V
ToMap returns a copy of the map as a regular map.

<a name="SyncPool"></a>
## type [SyncPool](<https://github.com/dashjay/xiter/blob/main/pkg/xsync/sync_pool.go#L5-L9>)
## type [SyncPool](<https://github.com/dashjay/xiter/blob/main/pkg/xsync/sync_pool.go#L5-L7>)



```go
type SyncPool[T any] struct {
New func() T
// contains filtered or unexported fields
}
```

<a name="NewSyncPool"></a>
### func [NewSyncPool](<https://github.com/dashjay/xiter/blob/main/pkg/xsync/sync_pool.go#L12>)
### func [NewSyncPool](<https://github.com/dashjay/xiter/blob/main/pkg/xsync/sync_pool.go#L10>)

```go
func NewSyncPool[T any](new func() T) *SyncPool[T]
Expand All @@ -364,7 +363,7 @@ func NewSyncPool[T any](new func() T) *SyncPool[T]
NewSyncPool creates a new SyncPool with specified init function

<a name="SyncPool[T].Get"></a>
### func \(\*SyncPool\[T\]\) [Get](<https://github.com/dashjay/xiter/blob/main/pkg/xsync/sync_pool.go#L30>)
### func \(\*SyncPool\[T\]\) [Get](<https://github.com/dashjay/xiter/blob/main/pkg/xsync/sync_pool.go#L20>)

```go
func (s *SyncPool[T]) Get() T
Expand All @@ -373,7 +372,7 @@ func (s *SyncPool[T]) Get() T
Get wraps sync.Pool.Get.

<a name="SyncPool[T].Put"></a>
### func \(\*SyncPool\[T\]\) [Put](<https://github.com/dashjay/xiter/blob/main/pkg/xsync/sync_pool.go#L36>)
### func \(\*SyncPool\[T\]\) [Put](<https://github.com/dashjay/xiter/blob/main/pkg/xsync/sync_pool.go#L25>)

```go
func (s *SyncPool[T]) Put(x T)
Expand Down
18 changes: 3 additions & 15 deletions pkg/xsync/sync_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,25 @@ package xsync
import "sync"

type SyncPool[T any] struct {
New func() T
pool sync.Pool
once sync.Once
}

// NewSyncPool creates a new SyncPool with specified init function
func NewSyncPool[T any](new func() T) *SyncPool[T] {
sp := &SyncPool[T]{
New: new,
pool: sync.Pool{New: func() interface{} {
return new()
}},
}
sp.init()
return sp
}

// init initializes the SyncPool
func (s *SyncPool[T]) init() {
s.once.Do(func() {
s.pool.New = func() interface{} {
return s.New()
}
})
}

// Get wraps sync.Pool.Get.
func (s *SyncPool[T]) Get() T {
s.init()
return s.pool.Get().(T)
}

// Put wraps sync.Pool.Put.
func (s *SyncPool[T]) Put(x T) {
s.init()
s.pool.Put(x)
}