iterator

package
v0.9.4-rc.1 Latest Latest
Warning

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

Go to latest
Published: Mar 17, 2023 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Overview

Package iterator describes a generic Iterator interface.

Index

Constants

This section is empty.

Variables

View Source
var ErrIteratorDone = errors.New("iterator is read to the end or closed")

ErrIteratorDone is returned when the iterator is read to the end or closed.

Functions

func Values added in v0.9.0

func Values[K, V any](iter Interface[K, V]) ([]V, error)

Values consumes all values from iterator until it is done. ErrIteratorDone error is returned as nil; any other error is returned as-is.

Iterator is always closed at the end.

Types

type Interface

type Interface[K, V any] interface {
	// Next returns the next key/value pair, where the key is a slice index, map key, document number, etc,
	// and the value is the slice or map value, next document, etc.
	//
	// Returned error could be (possibly wrapped) ErrIteratorDone or some fatal error
	// like (possibly wrapped) context.Canceled.
	// In any case, even if iterator was read to the end, and Next returned ErrIteratorDone,
	// or Next returned fatal error,
	// Close method still should be called.
	//
	// Next should not be called concurrently.
	Next() (K, V, error)

	// Close indicates that the iterator will no longer be used.
	// After Close is called, future calls to Next must return ErrIteratorDone,
	// even if previous call returned a different error.
	//
	// Close must be called.
	// If it wasn't, the iterator might leak resources or panic later.
	//
	// Close must be concurrency-safe and may be called multiple times.
	// All calls after the first should have no observable effect.
	Close()
}

Interface is an iterator interface.

func WithClose added in v0.9.1

func WithClose[K, V any](iter Interface[K, V], close func()) Interface[K, V]

WithClose wraps an iterator with a custom close function.

That function should call Close() method of the wrapped iterator.

Jump to

Keyboard shortcuts

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