Documentation
¶
Overview ¶
Package ringslice provides a generic ring buffer backed by a Go slice, with iter.Seq iteration and callback hooks for production use cases.
Index ¶
- type Ring
- func (r *Ring[T]) Add(val T)
- func (r *Ring[T]) All() iter.Seq[T]
- func (r *Ring[T]) AllDesc() iter.Seq[T]
- func (r *Ring[T]) Cap() int
- func (r *Ring[T]) Clear()
- func (r *Ring[T]) Flush()
- func (r *Ring[T]) Len() int
- func (r *Ring[T]) OnBeforeAdd(fn func(T) bool)
- func (r *Ring[T]) OnFlush(fn func([]T))
- func (r *Ring[T]) OnRotate(fn func([]T))
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Ring ¶
type Ring[T any] struct { // contains filtered or unexported fields }
Ring is a fixed-size ring buffer backed by a Go slice. When the buffer is full, new values overwrite the oldest.
func (*Ring[T]) AllDesc ¶
AllDesc returns an iterator that yields each element in reverse chronological order.
func (*Ring[T]) Clear ¶ added in v0.2.0
func (r *Ring[T]) Clear()
Clear resets the ring buffer, discarding all elements.
func (*Ring[T]) Flush ¶ added in v0.4.0
func (r *Ring[T]) Flush()
Flush resets the ring buffer, discarding all elements. If an OnFlush callback is registered, it is invoked before the buffer is cleared.
func (*Ring[T]) OnBeforeAdd ¶ added in v0.3.0
OnBeforeAdd registers a callback invoked before a value is added. Returning false from fn prevents the value from being written. fn must not call any methods on the same Ring instance.
func (*Ring[T]) OnFlush ¶ added in v0.4.0
func (r *Ring[T]) OnFlush(fn func([]T))
OnFlush registers a callback invoked by Flush() before the buffer is cleared. fn receives the underlying slice of the ring buffer, thus it must not mutate or retain the slice beyond the duration of the call. fn must not call any methods on the same Ring instance.
func (*Ring[T]) OnRotate ¶ added in v0.3.0
func (r *Ring[T]) OnRotate(fn func([]T))
OnRotate registers a callback invoked when the write index wraps back to zero. fn receives the underlying slice of the ring buffer, thus it must not mutate or retain the slice beyond the duration of the call. fn must not call any methods on the same Ring instance.