ring

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2026 License: MIT Imports: 0 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Buffer

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

Buffer is a non-thread-safe ring (circular) buffer.

Notes: - Capacity is the maximum number of elements it can hold. - Internally it keeps one slot empty to distinguish full vs empty. - This is intended as a low-level primitive; add locking externally if needed.

func New

func New[T any](capacity int) *Buffer[T]

New creates a new ring buffer with the given capacity.

func (*Buffer[T]) Cap

func (b *Buffer[T]) Cap() int

Cap returns the buffer capacity.

func (*Buffer[T]) Drain added in v0.5.0

func (b *Buffer[T]) Drain() []T

Drain returns all elements as a slice and resets the buffer.

func (*Buffer[T]) Len

func (b *Buffer[T]) Len() int

Len returns the number of elements currently stored.

func (*Buffer[T]) Peek added in v0.5.0

func (b *Buffer[T]) Peek() (v T, ok bool)

Peek returns the next element without removing it. ok is false if the buffer is empty.

func (*Buffer[T]) Pop

func (b *Buffer[T]) Pop() (v T, ok bool)

Pop removes and returns one element. ok is false if the buffer is empty.

func (*Buffer[T]) Push

func (b *Buffer[T]) Push(v T) bool

Push appends one element. Returns false if the buffer is full.

func (*Buffer[T]) Reset

func (b *Buffer[T]) Reset()

Reset clears the buffer.

func (*Buffer[T]) Space

func (b *Buffer[T]) Space() int

Space returns the remaining free slots.

type ByteBuffer

type ByteBuffer struct {
	// contains filtered or unexported fields
}

ByteBuffer is a ring buffer specialized for bytes.

Semantics match Buffer[T]: it keeps one slot empty internally.

func NewBytes

func NewBytes(capacity int) *ByteBuffer

NewBytes creates a new ByteBuffer with the given capacity.

func (*ByteBuffer) Cap

func (b *ByteBuffer) Cap() int

func (*ByteBuffer) Len

func (b *ByteBuffer) Len() int

func (*ByteBuffer) Read

func (b *ByteBuffer) Read(p []byte) int

Read reads up to len(p) bytes and returns the number of bytes read.

func (*ByteBuffer) Reset

func (b *ByteBuffer) Reset()

func (*ByteBuffer) Space

func (b *ByteBuffer) Space() int

func (*ByteBuffer) Write

func (b *ByteBuffer) Write(p []byte) int

Write writes as many bytes as possible and returns the number of bytes written.

Jump to

Keyboard shortcuts

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