-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat (pool): further compatibility interface work (#33)
- Loading branch information
1 parent
858c066
commit 9a49f17
Showing
2 changed files
with
109 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,80 @@ | ||
package pool | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"sync" | ||
) | ||
|
||
type Pool[T any] interface { | ||
Get() T | ||
Put(T) | ||
} | ||
|
||
// ByteBuffer is satisfied by [*pool.Buffer] and [*bytes.Buffer]. | ||
// | ||
// Note, we can't include Reset and Grow as our implementations return an error while a [*bytes.Buffer] does not. | ||
type ByteBuffer interface { | ||
Len() int | ||
Cap() int | ||
Bytes() []byte | ||
WriteRune(rune) (int, error) | ||
WriteString(string) (int, error) | ||
|
||
io.WriterTo | ||
io.ByteWriter | ||
io.ReadWriter | ||
|
||
io.ReaderFrom | ||
io.ByteReader | ||
io.RuneReader | ||
} | ||
|
||
type WithPutError[T any] interface { | ||
Get() T | ||
Put(T) error | ||
} | ||
|
||
func (b BufferFactoryInterfaceCompat) Put(buf *Buffer) { | ||
_ = b.BufferFactory.Put(buf) | ||
} | ||
|
||
type BufferFactoryInterfaceCompat struct { | ||
BufferFactory | ||
} | ||
|
||
func (b BufferFactoryInterfaceCompat) Put(buf *Buffer) { | ||
_ = b.BufferFactory.Put(buf) | ||
type BufferFactoryByteBufferCompat struct { | ||
BufferFactory | ||
} | ||
|
||
func (bf BufferFactoryByteBufferCompat) Put(buf ByteBuffer) { | ||
if b, ok := buf.(*Buffer); ok { | ||
err := bf.BufferFactory.Put(b) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return | ||
} | ||
if b, ok := buf.(*bytes.Buffer); ok { | ||
newB := &Buffer{ | ||
o: &sync.Once{}, | ||
Buffer: b, | ||
} | ||
_ = bf.BufferFactory.Put(newB) | ||
return | ||
} | ||
// unfortunately this compatibility shim cannot be used with any other types implementing ByteBuffer | ||
// this is because we can't wrap them in a *Buffer | ||
panic("invalid type, need *pool.Buffer or *bytes.Buffer") | ||
} | ||
|
||
func (bf BufferFactoryByteBufferCompat) Get() ByteBuffer { | ||
b := bf.BufferFactory.Get() | ||
return ByteBuffer(b) | ||
} | ||
|
||
var ( | ||
_ ByteBuffer = (*Buffer)(nil) | ||
_ ByteBuffer = (*bytes.Buffer)(nil) | ||
_ Pool[ByteBuffer] = (*BufferFactoryByteBufferCompat)(nil) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters