]> 127.0.0.1 Git - part/.git/commitdiff
1 v0.28.20231214150658
authorqydysky <qydysky@foxmail.com>
Thu, 14 Dec 2023 15:03:52 +0000 (23:03 +0800)
committerqydysky <qydysky@foxmail.com>
Thu, 14 Dec 2023 15:03:52 +0000 (23:03 +0800)
.github/workflows/test.yml
slice/Blocks.go
slice/Blocks_test.go

index a9f0cd67a8b601917d977e192eb441f8d602992e..431d9d8d0f4a200d6254cc468e5314758989b4ea 100644 (file)
@@ -40,6 +40,7 @@ jobs:
         go test -count 1 -timeout 10s -v -race github.com/qydysky/part/rpc
         go test -count 1 -timeout 5s -v -race github.com/qydysky/part/component
         go test -count 1 -timeout 15s -v -race github.com/qydysky/part/ctx
+        go test -count 1 -timeout 5s -v -race github.com/qydysky/part/slice
 
     - name: Set Release Name
       run: |
index b3a6752ee88971bc4c0f145d058f1b1476edb470..39c907bc66a7357ee5906eaddc1e66eedf3b87e2 100644 (file)
@@ -4,16 +4,33 @@ import (
        "errors"
 )
 
-type Blocks[T any] struct {
+type BlocksI[T any] interface {
+       // // eg
+       //
+       //      if tmpbuf, putBack, e := buf.Get(); e == nil {
+       //              clear(tmpbuf)
+       //              // do something with tmpbuf
+       //              putBack()
+       //      }
+       Get() ([]T, func(), error)
+}
+
+type blocks[T any] struct {
+       _    noCopy
        free chan int
        size int
        buf  []T
 }
 
+type noCopy struct{}
+
+func (*noCopy) Lock()   {}
+func (*noCopy) Unlock() {}
+
 var ErrOverflow = errors.New("ErrOverflow")
 
-func NewBlocks[T any](blockSize int, blockNum int) *Blocks[T] {
-       p := &Blocks[T]{
+func NewBlocks[T any](blockSize int, blockNum int) BlocksI[T] {
+       p := &blocks[T]{
                size: blockSize,
                free: make(chan int, blockNum+1),
                buf:  make([]T, blockSize*blockNum),
@@ -24,14 +41,7 @@ func NewBlocks[T any](blockSize int, blockNum int) *Blocks[T] {
        return p
 }
 
-// // eg
-//
-//     if tmpbuf, putBack, e := buf.Get(); e == nil {
-//             clear(tmpbuf)
-//             // do something with tmpbuf
-//             putBack()
-//     }
-func (t *Blocks[T]) Get() ([]T, func(), error) {
+func (t *blocks[T]) Get() ([]T, func(), error) {
        select {
        case offset := <-t.free:
                offset *= t.size
index b6eae3936f5d5e1eafde6aef2769dc3a364c794e..303e159d916c9e8ce253a89cbe33adcdb7fb683b 100644 (file)
@@ -1,12 +1,27 @@
 package part
 
-import "testing"
+import (
+       "testing"
+)
 
 func TestMain(t *testing.T) {
-       buf := NewBlocks[byte](1024, 10)
+       buf := NewBlocks[byte](1024, 1)
        if tmpbuf, putBack, e := buf.Get(); e == nil {
                clear(tmpbuf)
                // do something with tmpbuf
                putBack()
+       } else {
+               t.Fail()
+       }
+       if tmpbuf, putBack, e := buf.Get(); e == nil {
+               clear(tmpbuf)
+               if tmpbuf, putBack, e := buf.Get(); e != ErrOverflow {
+                       clear(tmpbuf)
+                       t.Fail()
+                       // do something with tmpbuf
+                       putBack()
+               }
+               // do something with tmpbuf
+               putBack()
        }
 }