views

package
v1.74.0 Latest Latest
Warning

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

Go to latest
Published: Sep 12, 2024 License: BSD-3-Clause Imports: 9 Imported by: 47

Documentation

Overview

Package views provides read-only accessors for commonly used value types.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AppendStrings added in v1.66.0

func AppendStrings[T fmt.Stringer](dst []string, v Slice[T]) []string

AppendStrings appends the string representation of each element in v to dst.

func ContainsPointers added in v1.72.0

func ContainsPointers[T any]() bool

ContainsPointers reports whether T contains any pointers, either explicitly or implicitly. It has special handling for some types that contain pointers that we know are free from memory aliasing/mutation concerns.

func SliceContains added in v1.30.0

func SliceContains[T comparable](v Slice[T], e T) bool

SliceContains reports whether v contains element e.

As it runs in O(n) time, use with care.

func SliceEqual added in v1.50.0

func SliceEqual[T comparable](a, b Slice[T]) bool

SliceEqual is like the standard library's slices.Equal, but for two views.

func SliceEqualAnyOrder added in v1.36.0

func SliceEqualAnyOrder[T comparable](a, b Slice[T]) bool

SliceEqualAnyOrder reports whether a and b contain the same elements, regardless of order. The underlying slices for a and b can be nil.

Types

type ByteSlice added in v1.50.0

type ByteSlice[T ~[]byte] struct {
	// contains filtered or unexported fields
}

ByteSlice is a read-only accessor for types that are backed by a []byte.

func ByteSliceOf added in v1.50.0

func ByteSliceOf[T ~[]byte](x T) ByteSlice[T]

ByteSliceOf returns a ByteSlice for the provided slice.

func (ByteSlice[T]) AppendTo added in v1.50.0

func (v ByteSlice[T]) AppendTo(dst T) T

AppendTo appends the underlying slice values to dst.

func (ByteSlice[T]) AsSlice added in v1.50.0

func (v ByteSlice[T]) AsSlice() T

AsSlice returns a copy of the underlying slice.

func (ByteSlice[T]) At added in v1.50.0

func (v ByteSlice[T]) At(i int) byte

At returns the byte at index `i` of the slice.

func (ByteSlice[T]) Equal added in v1.50.0

func (v ByteSlice[T]) Equal(b T) bool

Equal reports whether the underlying slice is equal to b.

func (ByteSlice[T]) EqualView added in v1.50.0

func (v ByteSlice[T]) EqualView(b ByteSlice[T]) bool

EqualView reports whether the underlying slice is equal to b.

func (ByteSlice[T]) IsNil added in v1.50.0

func (v ByteSlice[T]) IsNil() bool

IsNil reports whether the underlying slice is nil.

func (ByteSlice[T]) Len added in v1.50.0

func (v ByteSlice[T]) Len() int

Len returns the length of the slice.

func (ByteSlice[T]) MapKey added in v1.60.0

func (v ByteSlice[T]) MapKey() SliceMapKey[byte]

MapKey returns a unique key for a slice, based on its address and length.

func (ByteSlice[T]) MarshalJSON added in v1.50.0

func (v ByteSlice[T]) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (ByteSlice[T]) Mem added in v1.50.0

func (v ByteSlice[T]) Mem() mem.RO

Mem returns a read-only view of the underlying slice.

func (ByteSlice[T]) Slice added in v1.50.0

func (v ByteSlice[T]) Slice(i, j int) ByteSlice[T]

Slice returns v[i:j]

func (ByteSlice[T]) SliceFrom added in v1.50.0

func (v ByteSlice[T]) SliceFrom(i int) ByteSlice[T]

SliceFrom returns v[i:].

func (ByteSlice[T]) SliceTo added in v1.50.0

func (v ByteSlice[T]) SliceTo(i int) ByteSlice[T]

SliceTo returns v[:i]

func (*ByteSlice[T]) UnmarshalJSON added in v1.50.0

func (v *ByteSlice[T]) UnmarshalJSON(b []byte) error

UnmarshalJSON implements json.Unmarshaler.

type Cloner added in v1.72.0

type Cloner[T any] interface {
	// Clone returns a deep-clone of the receiver.
	// It returns nil, when the receiver is nil.
	Clone() T
}

Cloner is any type that has a Clone function returning a deep-clone of the receiver.

type Map added in v1.26.0

type Map[K comparable, V any] struct {
	// contains filtered or unexported fields
}

Map provides a read-only view of a map. It is the caller's responsibility to make sure V is immutable.

func MapOf added in v1.26.0

func MapOf[K comparable, V any](m map[K]V) Map[K, V]

MapOf returns a view over m. It is the caller's responsibility to make sure V is immutable.

func (Map[K, V]) AsMap added in v1.46.0

func (m Map[K, V]) AsMap() map[K]V

AsMap returns a shallow-clone of the underlying map. If V is a pointer type, it is the caller's responsibility to make sure the values are immutable.

func (Map[K, V]) Contains added in v1.70.0

func (m Map[K, V]) Contains(k K) bool

Contains reports whether k has an entry in the map.

func (Map[K, V]) Get added in v1.26.0

func (m Map[K, V]) Get(k K) V

Get returns the element with key k.

func (Map[K, V]) GetOk added in v1.26.0

func (m Map[K, V]) GetOk(k K) (V, bool)

GetOk returns the element with key k and a bool representing whether the key is in map.

func (Map[K, V]) Has added in v1.26.0

func (m Map[K, V]) Has(k K) bool

Has reports whether k has an entry in the map. Deprecated: use Contains instead.

func (Map[K, V]) IsNil added in v1.26.0

func (m Map[K, V]) IsNil() bool

IsNil reports whether the underlying map is nil.

func (Map[K, V]) Len added in v1.26.0

func (m Map[K, V]) Len() int

Len returns the number of elements in the map.

func (Map[K, V]) MarshalJSON added in v1.46.0

func (m Map[K, V]) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (Map[K, V]) Range added in v1.26.0

func (m Map[K, V]) Range(f MapRangeFn[K, V])

Range calls f for every k,v pair in the underlying map. It stops iteration immediately if f returns false.

func (*Map[K, V]) UnmarshalJSON added in v1.46.0

func (m *Map[K, V]) UnmarshalJSON(b []byte) error

UnmarshalJSON implements json.Unmarshaler. It should only be called on an uninitialized Map.

type MapFn added in v1.26.0

type MapFn[K comparable, T any, V any] struct {
	// contains filtered or unexported fields
}

MapFn is like Map but with a func to convert values from T to V. It is used to provide map of slices and views.

func MapFnOf added in v1.26.0

func MapFnOf[K comparable, T any, V any](m map[K]T, f func(T) V) MapFn[K, T, V]

MapFnOf returns a MapFn for m.

func (MapFn[K, T, V]) Contains added in v1.70.0

func (m MapFn[K, T, V]) Contains(k K) bool

Contains reports whether k has an entry in the map.

func (MapFn[K, T, V]) Get added in v1.26.0

func (m MapFn[K, T, V]) Get(k K) V

Get returns the element with key k.

func (MapFn[K, T, V]) GetOk added in v1.26.0

func (m MapFn[K, T, V]) GetOk(k K) (V, bool)

GetOk returns the element with key k and a bool representing whether the key is in map.

func (MapFn[K, T, V]) Has added in v1.26.0

func (m MapFn[K, T, V]) Has(k K) bool

Has reports whether k has an entry in the map. Deprecated: use Contains instead.

func (MapFn[K, T, V]) IsNil added in v1.26.0

func (m MapFn[K, T, V]) IsNil() bool

IsNil reports whether the underlying map is nil.

func (MapFn[K, T, V]) Len added in v1.26.0

func (m MapFn[K, T, V]) Len() int

Len returns the number of elements in the map.

func (MapFn[K, T, V]) Range added in v1.26.0

func (m MapFn[K, T, V]) Range(f MapRangeFn[K, V])

Range calls f for every k,v pair in the underlying map. It stops iteration immediately if f returns false.

type MapRangeFn added in v1.26.0

type MapRangeFn[K comparable, V any] func(k K, v V) (cont bool)

MapRangeFn is the func called from a Map.Range call. Implementations should return false to stop range.

type MapSlice added in v1.70.0

type MapSlice[K comparable, V any] struct {
	// contains filtered or unexported fields
}

MapSlice is a view over a map whose values are slices.

func MapSliceOf added in v1.70.0

func MapSliceOf[K comparable, V any](m map[K][]V) MapSlice[K, V]

MapSliceOf returns a MapSlice for the provided map. It is the caller's responsibility to make sure V is immutable.

func (MapSlice[K, V]) AsMap added in v1.70.0

func (m MapSlice[K, V]) AsMap() map[K][]V

AsMap returns a shallow-clone of the underlying map.

If V is a pointer type, it is the caller's responsibility to make sure the values are immutable. The map and slices are cloned, but the values are not.

func (MapSlice[K, V]) Contains added in v1.70.0

func (m MapSlice[K, V]) Contains(k K) bool

Contains reports whether k has an entry in the map.

func (MapSlice[K, V]) Get added in v1.70.0

func (m MapSlice[K, V]) Get(k K) Slice[V]

Get returns the element with key k.

func (MapSlice[K, V]) GetOk added in v1.70.0

func (m MapSlice[K, V]) GetOk(k K) (Slice[V], bool)

GetOk returns the element with key k and a bool representing whether the key is in map.

func (MapSlice[K, V]) IsNil added in v1.70.0

func (m MapSlice[K, V]) IsNil() bool

IsNil reports whether the underlying map is nil.

func (MapSlice[K, V]) Len added in v1.70.0

func (m MapSlice[K, V]) Len() int

Len returns the number of elements in the map.

func (MapSlice[K, V]) MarshalJSON added in v1.70.0

func (m MapSlice[K, V]) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (MapSlice[K, V]) Range added in v1.70.0

func (m MapSlice[K, V]) Range(f MapRangeFn[K, Slice[V]])

Range calls f for every k,v pair in the underlying map. It stops iteration immediately if f returns false.

func (*MapSlice[K, V]) UnmarshalJSON added in v1.70.0

func (m *MapSlice[K, V]) UnmarshalJSON(b []byte) error

UnmarshalJSON implements json.Unmarshaler. It should only be called on an uninitialized Map.

type Slice added in v1.24.0

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

Slice is a read-only accessor for a slice.

func SliceOf added in v1.24.0

func SliceOf[T any](x []T) Slice[T]

SliceOf returns a Slice for the provided slice for immutable values. It is the caller's responsibility to make sure V is immutable.

func (Slice[T]) All added in v1.74.0

func (v Slice[T]) All() iter.Seq2[int, T]

All returns an iterator over v.

func (Slice[T]) AppendTo added in v1.24.0

func (v Slice[T]) AppendTo(dst []T) []T

AppendTo appends the underlying slice values to dst.

func (Slice[T]) AsSlice added in v1.24.0

func (v Slice[T]) AsSlice() []T

AsSlice returns a copy of underlying slice.

func (Slice[T]) At added in v1.24.0

func (v Slice[T]) At(i int) T

At returns the element at index `i` of the slice.

func (Slice[T]) ContainsFunc added in v1.30.0

func (v Slice[T]) ContainsFunc(f func(T) bool) bool

ContainsFunc reports whether any element in v satisfies f(e).

As it runs in O(n) time, use with care.

func (Slice[T]) IndexFunc added in v1.30.0

func (v Slice[T]) IndexFunc(f func(T) bool) int

IndexFunc returns the first index of an element in v satisfying f(e), or -1 if none do.

As it runs in O(n) time, use with care.

func (Slice[T]) IsNil added in v1.24.0

func (v Slice[T]) IsNil() bool

IsNil reports whether the underlying slice is nil.

func (Slice[T]) Len added in v1.24.0

func (v Slice[T]) Len() int

Len returns the length of the slice.

func (Slice[T]) MapKey added in v1.60.0

func (v Slice[T]) MapKey() SliceMapKey[T]

MapKey returns a unique key for a slice, based on its address and length.

func (Slice[T]) MarshalJSON added in v1.24.0

func (v Slice[T]) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (Slice[T]) Slice added in v1.44.0

func (v Slice[T]) Slice(i, j int) Slice[T]

Slice returns v[i:j]

func (Slice[T]) SliceFrom added in v1.44.0

func (v Slice[T]) SliceFrom(i int) Slice[T]

SliceFrom returns v[i:].

func (Slice[T]) SliceTo added in v1.44.0

func (v Slice[T]) SliceTo(i int) Slice[T]

SliceTo returns v[:i]

func (*Slice[T]) UnmarshalJSON added in v1.24.0

func (v *Slice[T]) UnmarshalJSON(b []byte) error

UnmarshalJSON implements json.Unmarshaler.

type SliceMapKey added in v1.60.0

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

SliceMapKey represents a comparable unique key for a slice, based on its address and length. It can be used to key maps by slices but should only be used when the underlying slice is immutable.

Empty and nil slices have different keys.

type SliceView added in v1.26.0

type SliceView[T ViewCloner[T, V], V StructView[T]] struct {
	// contains filtered or unexported fields
}

SliceView wraps []T to provide accessors which return an immutable view V of T. It is used to provide the equivalent of SliceOf([]V) without having to allocate []V from []T.

func SliceOfViews added in v1.26.0

func SliceOfViews[T ViewCloner[T, V], V StructView[T]](x []T) SliceView[T, V]

SliceOfViews returns a ViewSlice for x.

func (SliceView[T, V]) AppendTo added in v1.26.0

func (v SliceView[T, V]) AppendTo(dst []V) []V

AppendTo appends the underlying slice values to dst.

func (SliceView[T, V]) AsSlice added in v1.26.0

func (v SliceView[T, V]) AsSlice() []V

AsSlice returns a copy of underlying slice.

func (SliceView[T, V]) At added in v1.26.0

func (v SliceView[T, V]) At(i int) V

At returns a View of the element at index `i` of the slice.

func (SliceView[T, V]) IsNil added in v1.26.0

func (v SliceView[T, V]) IsNil() bool

IsNil reports whether the underlying slice is nil.

func (SliceView[T, V]) Len added in v1.26.0

func (v SliceView[T, V]) Len() int

Len returns the length of the slice.

func (SliceView[T, V]) MapKey added in v1.60.0

func (v SliceView[T, V]) MapKey() SliceMapKey[T]

MapKey returns a unique key for a slice, based on its address and length.

func (SliceView[T, V]) MarshalJSON added in v1.26.0

func (v SliceView[T, V]) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (SliceView[T, V]) Slice added in v1.44.0

func (v SliceView[T, V]) Slice(i, j int) SliceView[T, V]

Slice returns v[i:j]

func (SliceView[T, V]) SliceFrom added in v1.44.0

func (v SliceView[T, V]) SliceFrom(i int) SliceView[T, V]

SliceFrom returns v[i:].

func (SliceView[T, V]) SliceTo added in v1.44.0

func (v SliceView[T, V]) SliceTo(i int) SliceView[T, V]

SliceTo returns v[:i]

func (*SliceView[T, V]) UnmarshalJSON added in v1.26.0

func (v *SliceView[T, V]) UnmarshalJSON(b []byte) error

UnmarshalJSON implements json.Unmarshaler.

type StructView added in v1.26.0

type StructView[T any] interface {
	// Valid reports whether the underlying Viewable is nil.
	Valid() bool
	// AsStruct returns a deep-copy of the underlying value.
	// It returns nil, if Valid() is false.
	AsStruct() T
}

StructView represents the corresponding StructView of a Viewable. The concrete types are typically generated by tailscale.com/cmd/viewer.

type ViewCloner added in v1.26.0

type ViewCloner[T any, V StructView[T]] interface {
	// View returns a read-only view of Viewable.
	// If Viewable is nil, View().Valid() reports false.
	View() V
	// Clone returns a deep-clone of Viewable.
	// It returns nil, when Viewable is nil.
	Clone() T
}

ViewCloner is any type that has had View and Clone funcs generated using tailscale.com/cmd/viewer.

Jump to

Keyboard shortcuts

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