Skip to content

Commit

Permalink
fix: 重构
Browse files Browse the repository at this point in the history
  • Loading branch information
Drelf2018 committed Dec 2, 2023
1 parent cca5cac commit 28ad1f7
Show file tree
Hide file tree
Showing 7 changed files with 164 additions and 117 deletions.
40 changes: 35 additions & 5 deletions Chan/chan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,44 @@ import (
"testing"

"github.com/Drelf2018/TypeGo/Chan"
"github.com/Drelf2018/TypeGo/test"
)

type Student struct {
Name string
ID int64
}

func (s Student) String() string {
if s.Name == "" {
return "I don't have a NAME!"
}
return fmt.Sprintf("I am %v and my ID is %v.", s.Name, s.ID)
}

func (s Student) Introduce() {
fmt.Println(s.String())
}

type Class struct {
Students []Student
}

func (c *Class) Join(s Student) {
c.Students = append(c.Students, s)
}

func (c Class) Call(ch chan Student) {
for _, s := range c.Students {
ch <- s
}
close(ch)
}

func TestChan(t *testing.T) {
class := test.Class{}
class.Join(test.Student{Name: "张三", ID: 1})
class.Join(test.Student{Name: "李四", ID: 2})
class.Join(test.Student{Name: "王五", ID: 3})
class := Class{}
class.Join(Student{Name: "张三", ID: 1})
class.Join(Student{Name: "李四", ID: 2})
class.Join(Student{Name: "王五", ID: 3})

for s := range Chan.New(class.Call) {
s.Introduce()
Expand Down
62 changes: 47 additions & 15 deletions Pool/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,56 @@
package Pool_test

import (
"errors"
"testing"

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

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(&test.Student{})
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)

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

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

s2 := pool.Get("李四")
t.Logf("s2: %v\n", s2)
pool.Put(c2, c3, nil)
t.Logf("pool.Len(): %v pool.Cap(): %v\n", pool.Len(), pool.Cap())

s3 := pool.Get("王五")
t.Logf("s3: %v\n", s3)
_ = pool.Get()
t.Logf("pool.Len(): %v pool.Cap(): %v\n", pool.Len(), pool.Cap())
}
```

Expand All @@ -41,11 +71,13 @@ go test github.com/Drelf2018/TypeGo/Pool -v

```
=== RUN TestPool
pool_test.go:14: s1: I am 张三 and my ID is 1.
pool_test.go:17: s1: I am undefined and my ID is 0.
pool_test.go:20: s2: I am 李四 and my ID is 0.
pool_test.go:23: s3: I am 王五 and my ID is 1.
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.025s
ok github.com/Drelf2018/TypeGo/Pool 0.026s
```
68 changes: 34 additions & 34 deletions Pool/pool.go
Original file line number Diff line number Diff line change
@@ -1,55 +1,55 @@
package Pool

import (
"reflect"
"sync"
)

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

type Pool[T Var] struct {
type Pool[T any, PT Var[T]] struct {
len int
cap int
sync.Pool
}

func (p *Pool[T]) Get(args ...any) T {
t := p.Pool.Get().(T)
t.Set(args...)
return t
func (p *Pool[T, PT]) Get(args ...any) (pt PT) {
pt = p.Pool.Get().(PT)
pt.OnGet(args...)
p.len++
return
}

func (p *Pool[T]) Put(ts ...T) {
for _, t := range ts {
t.Reset()
p.Pool.Put(t)
func (p *Pool[T, PT]) Put(pt ...PT) {
for _, v := range pt {
if v == nil {
continue
}
v.OnPut()
p.Pool.Put(v)
p.len--
}
}

func zero(typ reflect.Type) any {
if typ.Kind() == reflect.Ptr {
return reflect.New(typ.Elem()).Interface()
}
return reflect.Zero(typ).Interface()
func (p *Pool[T, PT]) Len() int {
return p.len
}

func (p *Pool[T, PT]) Cap() int {
return p.cap
}

func New[T Var](t T) (p Pool[T]) {
typ := reflect.TypeOf(t)
func New[T any, PT Var[T]](_ ...PT) *Pool[T, PT] {
p := new(Pool[T, PT])
p.New = func() any {
i := zero(typ).(T)
i.New()
return i
var v PT = new(T)
v.OnNew()
p.cap++
return v
}
return
return p
}

// None is a null struct which implement Var(interface).
//
// You can composite it in your own struct so that you don't need to implement Var.
type None struct{}

func (n *None) New() {}
func (n *None) Set(...any) {}
func (*None) Reset() {}
50 changes: 40 additions & 10 deletions Pool/pool_test.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,54 @@
package Pool_test

import (
"errors"
"testing"

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

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(&test.Student{})
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)

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

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

s2 := pool.Get("李四")
t.Logf("s2: %v\n", s2)
pool.Put(c2, c3, nil)
t.Logf("pool.Len(): %v pool.Cap(): %v\n", pool.Len(), pool.Cap())

s3 := pool.Get("王五")
t.Logf("s3: %v\n", s3)
_ = pool.Get()
t.Logf("pool.Len(): %v pool.Cap(): %v\n", pool.Len(), pool.Cap())
}
8 changes: 8 additions & 0 deletions Reflect/reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,14 @@ func Slice(elem reflect.Type) []uintptr {
}
}

func SlicePtr(elem reflect.Type) []uintptr {
return []uintptr{
Addr(elem),
Addr(reflect.SliceOf(elem)),
Addr(reflect.SliceOf(reflect.PtrTo(elem))),
}
}

type Option[V any] func(*Reflect[V])

func WithAlias[V any](f func(elem reflect.Type) []uintptr) Option[V] {
Expand Down
16 changes: 0 additions & 16 deletions test/class.go

This file was deleted.

37 changes: 0 additions & 37 deletions test/student.go

This file was deleted.

0 comments on commit 28ad1f7

Please sign in to comment.