package z
import (
"context"
"sync"
"github.com/cespare/xxhash/v2"
)
type Key interface {
uint64 | string | []byte | byte | int | int32 | uint32 | int64
}
func KeyToHash [K Key ](key K ) (uint64 , uint64 ) {
keyAsAny := any (key )
switch k := keyAsAny .(type ) {
case uint64 :
return k , 0
case string :
return MemHashString (k ), xxhash .Sum64String (k )
case []byte :
return MemHash (k ), xxhash .Sum64 (k )
case byte :
return uint64 (k ), 0
case int :
return uint64 (k ), 0
case int32 :
return uint64 (k ), 0
case uint32 :
return uint64 (k ), 0
case int64 :
return uint64 (k ), 0
default :
panic ("Key type not supported" )
}
}
var (
dummyCloserChan <-chan struct {}
tmpDir string
)
type Closer struct {
waiting sync .WaitGroup
ctx context .Context
cancel context .CancelFunc
}
func SetTmpDir (dir string ) {
tmpDir = dir
}
func NewCloser (initial int ) *Closer {
ret := &Closer {}
ret .ctx , ret .cancel = context .WithCancel (context .Background ())
ret .waiting .Add (initial )
return ret
}
func (lc *Closer ) AddRunning (delta int ) {
lc .waiting .Add (delta )
}
func (lc *Closer ) Ctx () context .Context {
if lc == nil {
return context .Background ()
}
return lc .ctx
}
func (lc *Closer ) Signal () {
lc .cancel ()
}
func (lc *Closer ) HasBeenClosed () <-chan struct {} {
if lc == nil {
return dummyCloserChan
}
return lc .ctx .Done ()
}
func (lc *Closer ) Done () {
if lc == nil {
return
}
lc .waiting .Done ()
}
func (lc *Closer ) Wait () {
lc .waiting .Wait ()
}
func (lc *Closer ) SignalAndWait () {
lc .Signal ()
lc .Wait ()
}
func ZeroOut (dst []byte , start , end int ) {
if start < 0 || start >= len (dst ) {
return
}
if end >= len (dst ) {
end = len (dst )
}
if end -start <= 0 {
return
}
Memclr (dst [start :end ])
}
The pages are generated with Golds v0.8.4 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .