Documentation ¶
Overview ¶
package "sak" (Swiss Army knife) provides some basic util functions
Index ¶
- func Abs[T SignedNumber](a T) T
- func MapCopy[K comparable, T any](m map[K]T) map[K]T
- func MapKeysToSlice[K comparable, T any](m map[K]T) []K
- func MapValuesToSlice[K comparable, T any](m map[K]T) []T
- func Max[T Number](a, b T) T
- func MaxN[T Number](vals ...T) (max T)
- func Min[T Number](a, b T) T
- func MinN[T Number](vals ...T) (min T)
- func Must[T any](item T, err error) T
- func Noescape(p unsafe.Pointer) unsafe.Pointer
- func Ptr[T any](v T) *T
- func ToPtrSlice[T any](structs []T) []*T
- func ToStructSlice[T any](ptrs []*T) []T
- type Element
- type Float
- type List
- func (l *List[T]) Back() *Element[T]
- func (l *List[T]) Front() *Element[T]
- func (l *List[T]) Init() *List[T]
- func (l *List[T]) InsertAfter(v T, mark *Element[T]) *Element[T]
- func (l *List[T]) InsertBefore(v T, mark *Element[T]) *Element[T]
- func (l *List[T]) Len() int
- func (l *List[T]) MoveAfter(e, mark *Element[T])
- func (l *List[T]) MoveBefore(e, mark *Element[T])
- func (l *List[T]) MoveToBack(e *Element[T])
- func (l *List[T]) MoveToFront(e *Element[T])
- func (l *List[T]) PushBack(v T) *Element[T]
- func (l *List[T]) PushBackList(other *List[T])
- func (l *List[T]) PushFront(v T) *Element[T]
- func (l *List[T]) PushFrontList(other *List[T])
- func (l *List[T]) Remove(e *Element[T]) T
- type Number
- type Pool
- type RunStatus
- type Signed
- type SignedNumber
- type Unsigned
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func MapCopy ¶
func MapCopy[K comparable, T any](m map[K]T) map[K]T
A utility function that copies a map[K]T. Useful when you need to iterate over items in a map that is synchronized buy a Mutex. Used internally but exposed for your consumption.
func MapKeysToSlice ¶
func MapKeysToSlice[K comparable, T any](m map[K]T) []K
A utility function that extracts all keys from a map[K]T. Useful when you need to iterate over keys in a map that is synchronized buy a Mutex. Used internally but exposed for your consumption.
func MapValuesToSlice ¶
func MapValuesToSlice[K comparable, T any](m map[K]T) []T
A utility function that extracts all values from a map[K]T. Useful when you need to iterate over items in a map that is synchronized buy a Mutex. Used internally but exposed for your consumption.
func Max ¶
func Max[T Number](a, b T) T
A generic version of math.Max.
Example ¶
package main import ( "fmt" "github.com/aws/go-kafka-event-source/streams/sak" ) func main() { a := uint16(1) b := uint16(2) max := sak.Max(a, b) fmt.Println(max) }
Output: 2
func MaxN ¶
func MaxN[T Number](vals ...T) (max T)
A generic version of math.Max with the added bonus of accepting more than 2 arguments.
Example ¶
package main import ( "fmt" "github.com/aws/go-kafka-event-source/streams/sak" ) func main() { vals := []int{1, 9, 4, 10, -1, 25} max := sak.MaxN(vals...) fmt.Println(max) }
Output: 25
func Min ¶
func Min[T Number](a, b T) T
A generic version of math.Min.
Example ¶
package main import ( "fmt" "github.com/aws/go-kafka-event-source/streams/sak" ) func main() { a := uint16(1) b := uint16(2) min := sak.Min(a, b) fmt.Println(min) }
Output: 1
func MinN ¶
func MinN[T Number](vals ...T) (min T)
A generic version of math.Min with the added bonus of accepting more than 2 arguments.
Example ¶
package main import ( "fmt" "github.com/aws/go-kafka-event-source/streams/sak" ) func main() { vals := []int{1, 9, 4, 10, -1, 25} min := sak.MinN(vals...) fmt.Println(min) }
Output: -1
func Must ¶
A convenience method for panicking on errors. Useful for simplifying code when calling methods that should never error, or when thre is no way to recover from the error.
Example ¶
package main import ( "fmt" "github.com/aws/go-kafka-event-source/streams/sak" ) type Encoder[T any] func(T) ([]byte, error) func encodeString(s string) ([]byte, error) { return []byte(s), nil } func main() { var encode Encoder[string] = encodeString b := sak.Must(encode("Hello World!")) fmt.Println(len(b)) }
Output: 12
func Noescape ¶
Noescape hides a pointer from escape analysis. noescape is the identity function but escape analysis doesn't think the output depends on the input. USE CAREFULLY!
func Ptr ¶
func Ptr[T any](v T) *T
Simple utilty for swapping struct T to a ptr T Wether or not this creates a heap escape is up to the compiler. This method simply return &v
func ToPtrSlice ¶
func ToPtrSlice[T any](structs []T) []*T
A utility function that converts a slice of T to a slice of *T. Useful when you don't want consumer of your package to be able to mutate an argument passed to you, but you need to mutate it internally (accept a slice of structs, then swap them to pointers). This methos forces a heap escape via
ptr := new(T)
Used internally but exposed for your consumption.
func ToStructSlice ¶
func ToStructSlice[T any](ptrs []*T) []T
The inverse of ToPtrSlice. Useful when you're doing some type gymnastics. Not used internally but, seems only correct to supply the inverse.
Types ¶
type Element ¶
type Element[T any] struct { // The value stored with this element. Value T // contains filtered or unexported fields }
Element is an element of a linked list.
type List ¶
type List[T any] struct { // contains filtered or unexported fields }
List represents a doubly linked list. The zero value for List is an empty list ready to use.
func (*List[T]) InsertAfter ¶
InsertAfter inserts a new element e with value v immediately after mark and returns e. If mark is not an element of l, the list is not modified. The mark must not be nil.
func (*List[T]) InsertBefore ¶
InsertBefore inserts a new element e with value v immediately before mark and returns e. If mark is not an element of l, the list is not modified. The mark must not be nil.
func (*List[T]) MoveAfter ¶
MoveAfter moves element e to its new position after mark. If e or mark is not an element of l, or e == mark, the list is not modified. The element and mark must not be nil.
func (*List[T]) MoveBefore ¶
MoveBefore moves element e to its new position before mark. If e or mark is not an element of l, or e == mark, the list is not modified. The element and mark must not be nil.
func (*List[T]) MoveToBack ¶
MoveToBack moves element e to the back of list l. If e is not an element of l, the list is not modified. The element must not be nil.
func (*List[T]) MoveToFront ¶
MoveToFront moves element e to the front of list l. If e is not an element of l, the list is not modified. The element must not be nil.
func (*List[T]) PushBack ¶
PushBack inserts a new element e with value v at the back of list l and returns e.
func (*List[T]) PushBackList ¶
PushBackList inserts a copy of another list at the back of list l. The lists l and other may be the same. They must not be nil.
func (*List[T]) PushFront ¶
PushFront inserts a new element e with value v at the front of list l and returns e.
func (*List[T]) PushFrontList ¶
PushFrontList inserts a copy of another list at the front of list l. The lists l and other may be the same. They must not be nil.
type Pool ¶
type Pool[T any] struct { // contains filtered or unexported fields }
Pool is a generic alternative to sync.Pool, but more akin to "Free List". It does not function in the same way however. It is simply a capped list of objects controlled by a mutex. In many situations, this will sak.Pool will outperform sync.Pool, however the memory management is very rudimentary and does not provide the same benefits. There is however, much less overhead than a standard sync.Pool.
Example ¶
package main import ( "fmt" "github.com/aws/go-kafka-event-source/streams/sak" ) func newIntArray() []int { return make([]int, 0) } func releaseIntArray(a []int) []int { return a[0:0] } func main() { intArrayPool := sak.NewPool(100, newIntArray, releaseIntArray) a := intArrayPool.Borrow() for i := 0; i < 10; i++ { a = append(a, i) } fmt.Println(len(a)) intArrayPool.Release(a) b := intArrayPool.Borrow() fmt.Println(len(b)) }
Output: 10 0
func NewPool ¶
NewPool creates a new free list. size is the maximum size of the returned free list. `factory` is the function which allocates new objects when necessary. `resetter` is optional, but when provided, is invoked on `Release`, before returning the object to the pool.
type RunStatus ¶
type RunStatus struct {
// contains filtered or unexported fields
}
RunStatus encapsulates a cancellable Context for the purposes of determining whether a sub-process is running or to instruct it halt.
Example ¶
package main import ( "context" "fmt" "github.com/aws/go-kafka-event-source/streams/sak" ) func main() { parent := sak.NewRunStatus(context.Background()).WithValue("name", "parent") child1 := parent.Fork().WithValue("name", "child1") // will override "name" of parent child2 := parent.Fork() // will inherit "name" from parent child1.Halt() // child1 halts but parent continues to run fmt.Println(parent.Running()) fmt.Println(child1.Running()) fmt.Println(child2.Running()) parent.Halt() // all RunStatus are halted fmt.Println(parent.Running()) fmt.Println(child2.Running()) fmt.Println(parent.Ctx().Value("name")) fmt.Println(child1.Ctx().Value("name")) fmt.Println(child2.Ctx().Value("name")) }
Output: true false true false false parent child1 parent
func NewRunStatus ¶
Creates a RunStatus. If `parent` == nil, context.Background() is used.
func (RunStatus) Done ¶
func (rs RunStatus) Done() <-chan struct{}
Returns the RunStatus.Ctx().Done()
func (RunStatus) Fork ¶
Creates a new child RunStatus, using the current RunStatus.Ctx() as a parent. The newly created RunStatus get's a new context.CancelFunc. Calling Halt on the returned RunStatus will not Halt the parent. The equivalent of calling:
NewRunStatus(rs.Ctx())
func (RunStatus) Running ¶
Returns true if the underlying Context has neither timed out or has been canceled.