buffer

package
v0.0.0-...-b8788ab Latest Latest
Warning

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

Go to latest
Published: Feb 10, 2026 License: Apache-2.0 Imports: 2 Imported by: 94

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidSize = errors.New("size must be positive")

ErrInvalidSize indicates size must be > 0

Functions

This section is empty.

Types

type Ring

type Ring[T any] struct {
	// contains filtered or unexported fields
}

Ring is a dynamically-sized ring buffer which can grow and shrink as-needed. The zero value has an initial size and normal size of 0 and is ready to use. Not thread safe.

func NewRing

func NewRing[T any](opts RingOptions) *Ring[T]

NewRing constructs a new Ring instance with provided parameters.

func (*Ring[T]) Cap

func (r *Ring[T]) Cap() int

Cap returns the capacity of the buffer.

func (*Ring[T]) Len

func (r *Ring[T]) Len() int

Len returns the number of items in the buffer.

func (*Ring[T]) ReadOne

func (r *Ring[T]) ReadOne() (data T, ok bool)

ReadOne reads (consumes) first item from the buffer if it is available, otherwise returns false. When the buffer has been totally consumed and has grown in size beyond its normal size, it shrinks down to its normal size again.

func (*Ring[T]) WriteOne

func (r *Ring[T]) WriteOne(data T)

WriteOne adds an item to the end of the buffer, growing it if it is full.

type RingGrowing deprecated

type RingGrowing = TypedRingGrowing[any]

RingGrowing is a growing ring buffer. Not thread safe.

Deprecated: Use TypedRingGrowing[any] instead.

func NewRingGrowing deprecated

func NewRingGrowing(initialSize int) *RingGrowing

NewRingGrowing constructs a new RingGrowing instance with provided parameters.

Deprecated: Use NewTypedRingGrowing[any] instead.

type RingGrowingOptions

type RingGrowingOptions struct {
	// InitialSize is the number of pre-allocated elements in the
	// initial underlying storage buffer.
	InitialSize int
}

RingGrowingOptions sets parameters for RingGrowing and TypedRingGrowing.

type RingOptions

type RingOptions struct {
	// InitialSize is the number of pre-allocated elements in the
	// initial underlying storage buffer.
	InitialSize int
	// NormalSize is the number of elements to allocate for new storage
	// buffers once the Ring is consumed and
	// can shrink again.
	NormalSize int
}

RingOptions sets parameters for Ring.

type TypedRingFixed

type TypedRingFixed[T any] struct {
	// contains filtered or unexported fields
}

TypedRingFixed is a fixed-size circular buffer for elements of type T. Writes overwrite older data, keeping only the last N elements. Not thread safe.

func NewTypedRingFixed

func NewTypedRingFixed[T any](size int) (*TypedRingFixed[T], error)

NewTypedRingFixed creates a circular buffer with the given capacity (must be > 0).

func (*TypedRingFixed[T]) Len

func (r *TypedRingFixed[T]) Len() int

Len returns how many elements are currently in the buffer.

func (*TypedRingFixed[T]) Reset

func (r *TypedRingFixed[T]) Reset()

Reset clears the buffer.

func (*TypedRingFixed[T]) Size

func (r *TypedRingFixed[T]) Size() int

Size returns the buffer capacity.

func (*TypedRingFixed[T]) Slice

func (r *TypedRingFixed[T]) Slice() []T

Slice returns buffer contents in write order. Don't modify the returned slice.

func (*TypedRingFixed[T]) TotalWritten

func (r *TypedRingFixed[T]) TotalWritten() int64

TotalWritten returns total elements ever written (including overwritten ones).

func (*TypedRingFixed[T]) Write

func (r *TypedRingFixed[T]) Write(p []T) (int, error)

Write writes p to the buffer, overwriting old data if needed.

type TypedRingGrowing

type TypedRingGrowing[T any] struct {
	// contains filtered or unexported fields
}

TypedRingGrowing is a growing ring buffer. The zero value has an initial size of 0 and is ready to use. Not thread safe.

func NewTypedRingGrowing

func NewTypedRingGrowing[T any](opts RingGrowingOptions) *TypedRingGrowing[T]

NewTypedRingGrowing constructs a new TypedRingGrowing instance with provided parameters.

func (*TypedRingGrowing[T]) Cap

func (r *TypedRingGrowing[T]) Cap() int

Cap returns the capacity of the buffer.

func (*TypedRingGrowing[T]) Len

func (r *TypedRingGrowing[T]) Len() int

Len returns the number of items in the buffer.

func (*TypedRingGrowing[T]) ReadOne

func (r *TypedRingGrowing[T]) ReadOne() (data T, ok bool)

ReadOne reads (consumes) first item from the buffer if it is available, otherwise returns false.

func (*TypedRingGrowing[T]) WriteOne

func (r *TypedRingGrowing[T]) WriteOne(data T)

WriteOne adds an item to the end of the buffer, growing it if it is full.

Jump to

Keyboard shortcuts

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