Pool

package
v0.7.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 23, 2024 License: MIT Imports: 1 Imported by: 0

README

Pool

带类型的变量池

使用
package Pool_test

import (
	"errors"
	"testing"

	"github.com/Drelf2018/TypeGo/Pool"
)

type Class struct {
	Students map[int]string
}

func (c *Class) OnNew() {
	c.Students = make(map[int]string)
}

func (c *Class) OnPut() {
	for k := range c.Students {
		delete(c.Students, k)
	}
}

func (c *Class) OnGet(stu ...any) {
	l := len(stu)
	if l&1 == 1 {
		panic(errors.New("odd params"))
	}
	for i := 0; i < l; i += 2 {
		c.Students[stu[i].(int)] = stu[i+1].(string)
	}
}

func TestPool(t *testing.T) {
	pool := Pool.New(new(Class))

	c1 := pool.Get(1, "Alice")
	t.Logf("c1: %v\n", c1)

	pool.Put(c1)
	t.Logf("c1: %v\n", c1)

	c2 := pool.Get(2, "Bob", 3, "Carol")
	t.Logf("c1: %v c2: %v\n", c1, c2)

	c3 := pool.Get(4, "Dave")
	t.Logf("c3: %v\n", c3)

	pool.Put(c2, c3, nil)
	t.Logf("pool.Len(): %v pool.Cap(): %v\n", pool.Len(), pool.Cap())

	_ = pool.Get()
	t.Logf("pool.Len(): %v pool.Cap(): %v\n", pool.Len(), pool.Cap())
}
命令
go test github.com/Drelf2018/TypeGo/Pool -v
控制台
=== RUN   TestPool
    pool_test.go:38: c1: &{map[1:Alice]}
    pool_test.go:41: c1: &{map[]}
    pool_test.go:44: c1: &{map[2:Bob 3:Carol]} c2: &{map[2:Bob 3:Carol]}
    pool_test.go:47: c3: &{map[4:Dave]}
    pool_test.go:50: pool.Len(): 0 pool.Cap(): 2
    pool_test.go:53: pool.Len(): 1 pool.Cap(): 2
--- PASS: TestPool (0.00s)
PASS
ok      github.com/Drelf2018/TypeGo/Pool        0.026s

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Pool added in v0.5.0

type Pool[T any, PT Var[T]] struct {
	sync.Pool
	// contains filtered or unexported fields
}

func New

func New[T any, PT Var[T]](_ ...PT) *Pool[T, PT]

func (*Pool[T, PT]) Cap added in v0.6.0

func (p *Pool[T, PT]) Cap() int

func (*Pool[T, PT]) Get added in v0.5.0

func (p *Pool[T, PT]) Get(args ...any) (pt PT)

func (*Pool[T, PT]) Len added in v0.6.0

func (p *Pool[T, PT]) Len() int

func (*Pool[T, PT]) Put added in v0.5.0

func (p *Pool[T, PT]) Put(pt ...PT)

type Var added in v0.1.1

type Var[T any] interface {
	OnNew()
	OnGet(...any)
	OnPut()
	~*T
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL