removed nil checks

This commit is contained in:
harshabose
2025-07-07 11:21:03 +05:30
parent f6eabae72a
commit 93e9f63548
3 changed files with 30 additions and 25 deletions

BIN
.DS_Store vendored Normal file

Binary file not shown.

View File

@@ -9,8 +9,8 @@ import (
type ChannelBuffer[T any] struct {
pool Pool[T]
bufferChannel chan *T
inputBuffer chan *T
bufferChannel chan T
inputBuffer chan T
closed bool
mux sync.RWMutex
ctx context.Context
@@ -19,8 +19,8 @@ type ChannelBuffer[T any] struct {
func CreateChannelBuffer[T any](ctx context.Context, size int, pool Pool[T]) *ChannelBuffer[T] {
buffer := &ChannelBuffer[T]{
pool: pool,
bufferChannel: make(chan *T, size),
inputBuffer: make(chan *T, size),
bufferChannel: make(chan T, size),
inputBuffer: make(chan T, size),
closed: false,
ctx: ctx,
}
@@ -28,7 +28,7 @@ func CreateChannelBuffer[T any](ctx context.Context, size int, pool Pool[T]) *Ch
return buffer
}
func (buffer *ChannelBuffer[T]) Push(ctx context.Context, element *T) error {
func (buffer *ChannelBuffer[T]) Push(ctx context.Context, element T) error {
buffer.mux.RLock()
defer buffer.mux.RUnlock()
@@ -44,38 +44,40 @@ func (buffer *ChannelBuffer[T]) Push(ctx context.Context, element *T) error {
}
}
func (buffer *ChannelBuffer[T]) Pop(ctx context.Context) (*T, error) {
func (buffer *ChannelBuffer[T]) Pop(ctx context.Context) (T, error) {
buffer.mux.RLock()
defer buffer.mux.RUnlock()
var zero T
if buffer.closed {
return nil, errors.New("buffer closed")
return zero, errors.New("buffer closed")
}
select {
case <-ctx.Done():
return nil, ctx.Err()
return zero, ctx.Err()
case data, ok := <-buffer.bufferChannel:
if !ok {
return nil, ErrorChannelBufferClose
}
if data == nil {
return nil, ErrorElementUnallocated
return zero, ErrorChannelBufferClose
}
// if data == nil {
// return zero, ErrorElementUnallocated
// }
return data, nil
}
}
func (buffer *ChannelBuffer[T]) Generate() *T {
func (buffer *ChannelBuffer[T]) Generate() T {
return buffer.pool.Get()
}
func (buffer *ChannelBuffer[T]) PutBack(element *T) {
func (buffer *ChannelBuffer[T]) PutBack(element T) {
if buffer.pool != nil {
buffer.pool.Put(element)
}
}
func (buffer *ChannelBuffer[T]) GetChannel() chan *T {
func (buffer *ChannelBuffer[T]) GetChannel() chan T {
return buffer.bufferChannel
}
@@ -91,7 +93,10 @@ loop:
case <-buffer.ctx.Done():
return
case element, ok := <-buffer.inputBuffer:
if !ok || element == nil {
// if !ok || element == nil {
// continue loop
// }
if !ok {
continue loop
}
select {

View File

@@ -3,22 +3,22 @@ package buffer
import "context"
type Pool[T any] interface {
Get() *T
Put(*T)
Get() T
Put(T)
Release()
}
type Buffer[T any] interface {
Push(context.Context, *T) error
Pop(ctx context.Context) (*T, error)
Push(context.Context, T) error
Pop(ctx context.Context) (T, error)
Size() int
}
type BufferWithGenerator[T any] interface {
Push(context.Context, *T) error
Pop(ctx context.Context) (*T, error)
Push(context.Context, T) error
Pop(ctx context.Context) (T, error)
Size() int
Generate() *T
PutBack(*T)
GetChannel() chan *T
Generate() T
PutBack(T)
GetChannel() chan T
}