ring API

ring

package

API reference for the ring package.

S
struct

ByteBuffer

ByteBuffer is a ring buffer specialized for bytes.

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

pkg/ring/bytes.go:6-10
type ByteBuffer struct

Methods

empty
Method

Returns

bool
func (*ByteBuffer) empty() bool
{ return b.r == b.w }
Cap
Method

Returns

int
func (*ByteBuffer) Cap() int
{
	if b == nil {
		return 0
	}
	return len(b.buf) - 1
}
Len
Method

Returns

int
func (*ByteBuffer) Len() int
{
	if b == nil {
		return 0
	}
	if b.w >= b.r {
		return b.w - b.r
	}
	return len(b.buf) - b.r + b.w
}
Space
Method

Returns

int
func (*ByteBuffer) Space() int
{ return b.Cap() - b.Len() }
Reset
Method
func (*ByteBuffer) Reset()
{ b.r, b.w = 0, 0 }
Write
Method

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

Parameters

p []byte

Returns

int
func (*ByteBuffer) Write(p []byte) int
{
	if b == nil || len(p) == 0 {
		return 0
	}
	toWrite := min(b.Space(), len(p))
	if toWrite == 0 {
		return 0
	}

	// Copy in up to two chunks.
	first := min(len(b.buf)-b.w, toWrite)
	copy(b.buf[b.w:b.w+first], p[:first])
	b.w = (b.w + first) % len(b.buf)

	second := toWrite - first
	if second > 0 {
		copy(b.buf[b.w:b.w+second], p[first:first+second])
		b.w += second
	}
	return toWrite
}
Read
Method

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

Parameters

p []byte

Returns

int
func (*ByteBuffer) Read(p []byte) int
{
	if b == nil || len(p) == 0 {
		return 0
	}
	toRead := min(b.Len(), len(p))
	if toRead == 0 {
		return 0
	}

	first := min(len(b.buf)-b.r, toRead)
	copy(p[:first], b.buf[b.r:b.r+first])
	b.r = (b.r + first) % len(b.buf)

	second := toRead - first
	if second > 0 {
		copy(p[first:first+second], b.buf[b.r:b.r+second])
		b.r += second
	}
	return toRead
}

Fields

Name Type Description
buf []byte
r int
w int
F
function

NewBytes

Parameters

capacity
int

Returns

pkg/ring/bytes.go:12-17
func NewBytes(capacity int) *ByteBuffer

{
	if capacity < 1 {
		panic("ring: capacity must be >= 1")
	}
	return &ByteBuffer{buf: make([]byte, capacity+1)}
}
S
struct

Buffer

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

pkg/ring/ring.go:9-13
type Buffer struct

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.

Fields

Name Type Description
buf []T
r int
w int
F
function

New

New creates a new ring buffer with the given capacity.

Parameters

capacity
int

Returns

*Buffer[T]
pkg/ring/ring.go:16-21
func New[T any](capacity int) *Buffer[T]

{
	if capacity < 1 {
		panic("ring: capacity must be >= 1")
	}
	return &Buffer[T]{buf: make([]T, capacity+1)}
}
F
function

TestBufferPushPopWrap

Parameters

pkg/ring/ring_test.go:8-39
func TestBufferPushPopWrap(t *testing.T)

{
	b := New[int](3)
	if b.Cap() != 3 {
		t.Fatalf("Cap=%d", b.Cap())
	}

	if !b.Push(1) || !b.Push(2) || !b.Push(3) {
		t.Fatal("expected push to succeed")
	}
	if b.Push(4) {
		t.Fatal("expected push to fail when full")
	}

	v, ok := b.Pop()
	if !ok || v != 1 {
		t.Fatalf("Pop=%v,%v", v, ok)
	}
	if !b.Push(4) {
		t.Fatal("expected push to succeed after pop")
	}

	// Now should read 2,3,4
	for _, want := range []int{2, 3, 4} {
		got, ok := b.Pop()
		if !ok || got != want {
			t.Fatalf("got %v ok=%v want %v", got, ok, want)
		}
	}
	if _, ok := b.Pop(); ok {
		t.Fatal("expected empty")
	}
}
F
function

TestByteBufferReadWrite

Parameters

pkg/ring/ring_test.go:41-70
func TestByteBufferReadWrite(t *testing.T)

{
	b := NewBytes(5)
	in := []byte("hello")
	if n := b.Write(in); n != 5 {
		t.Fatalf("Write=%d", n)
	}
	if n := b.Write([]byte("!")); n != 0 {
		t.Fatalf("Write when full=%d", n)
	}

	out := make([]byte, 3)
	if n := b.Read(out); n != 3 {
		t.Fatalf("Read=%d", n)
	}
	if !bytes.Equal(out, []byte("hel")) {
		t.Fatalf("out=%q", out)
	}

	if n := b.Write([]byte("!!")); n != 2 {
		t.Fatalf("Write=%d", n)
	}

	out2 := make([]byte, 4)
	if n := b.Read(out2); n != 4 {
		t.Fatalf("Read=%d", n)
	}
	if !bytes.Equal(out2, []byte("lo!!")) {
		t.Fatalf("out2=%q", out2)
	}
}