Initial QSfera import
This commit is contained in:
+4
@@ -0,0 +1,4 @@
|
||||
|
||||
# Constraints
|
||||
|
||||
This package is for Go 1.18 retrocompatiblity purpose.
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
// Copyright 2021 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// Package constraints defines a set of useful constraints to be used
|
||||
// with type parameters.
|
||||
package constraints
|
||||
|
||||
// Signed is a constraint that permits any signed integer type.
|
||||
// If future releases of Go add new predeclared signed integer types,
|
||||
// this constraint will be modified to include them.
|
||||
type Signed interface {
|
||||
~int | ~int8 | ~int16 | ~int32 | ~int64
|
||||
}
|
||||
|
||||
// Unsigned is a constraint that permits any unsigned integer type.
|
||||
// If future releases of Go add new predeclared unsigned integer types,
|
||||
// this constraint will be modified to include them.
|
||||
type Unsigned interface {
|
||||
~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
|
||||
}
|
||||
|
||||
// Integer is a constraint that permits any integer type.
|
||||
// If future releases of Go add new predeclared integer types,
|
||||
// this constraint will be modified to include them.
|
||||
type Integer interface {
|
||||
Signed | Unsigned
|
||||
}
|
||||
|
||||
// Float is a constraint that permits any floating-point type.
|
||||
// If future releases of Go add new predeclared floating-point types,
|
||||
// this constraint will be modified to include them.
|
||||
type Float interface {
|
||||
~float32 | ~float64
|
||||
}
|
||||
|
||||
// Complex is a constraint that permits any complex numeric type.
|
||||
// If future releases of Go add new predeclared complex numeric types,
|
||||
// this constraint will be modified to include them.
|
||||
type Complex interface {
|
||||
~complex64 | ~complex128
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
//go:build !go1.21
|
||||
|
||||
package constraints
|
||||
|
||||
// Ordered is a constraint that permits any ordered type: any type
|
||||
// that supports the operators < <= >= >.
|
||||
// If future releases of Go add new ordered types,
|
||||
// this constraint will be modified to include them.
|
||||
type Ordered interface {
|
||||
Integer | Float | ~string
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
//go:build go1.21
|
||||
|
||||
package constraints
|
||||
|
||||
import (
|
||||
"cmp"
|
||||
)
|
||||
|
||||
// Ordered is a constraint that permits any ordered type: any type
|
||||
// that supports the operators < <= >= >.
|
||||
// If future releases of Go add new ordered types,
|
||||
// this constraint will be modified to include them.
|
||||
type Ordered = cmp.Ordered
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
//go:build !go1.22
|
||||
|
||||
package xrand
|
||||
|
||||
import "math/rand"
|
||||
|
||||
// Shuffle returns a slice of shuffled values. Uses the Fisher-Yates shuffle algorithm.
|
||||
func Shuffle(n int, swap func(i, j int)) {
|
||||
rand.Shuffle(n, swap)
|
||||
}
|
||||
|
||||
// IntN returns, as an int, a pseudo-random number in the half-open interval [0,n)
|
||||
// from the default Source.
|
||||
// It panics if n <= 0.
|
||||
func IntN(n int) int {
|
||||
// bearer:disable go_gosec_crypto_weak_random
|
||||
return rand.Intn(n)
|
||||
}
|
||||
|
||||
// Int64 returns a non-negative pseudo-random 63-bit integer as an int64
|
||||
// from the default Source.
|
||||
func Int64() int64 {
|
||||
// bearer:disable go_gosec_crypto_weak_random
|
||||
n := rand.Int63()
|
||||
|
||||
// bearer:disable go_gosec_crypto_weak_random
|
||||
if rand.Intn(2) == 0 {
|
||||
return -n
|
||||
}
|
||||
|
||||
return n
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
//go:build go1.22
|
||||
|
||||
package xrand
|
||||
|
||||
import "math/rand/v2"
|
||||
|
||||
// Shuffle returns a slice of shuffled values. Uses the Fisher-Yates shuffle algorithm.
|
||||
func Shuffle(n int, swap func(i, j int)) {
|
||||
rand.Shuffle(n, swap)
|
||||
}
|
||||
|
||||
// IntN returns, as an int, a pseudo-random number in the half-open interval [0,n)
|
||||
// from the default Source.
|
||||
// It panics if n <= 0.
|
||||
func IntN(n int) int {
|
||||
return rand.IntN(n)
|
||||
}
|
||||
|
||||
// Int64 returns a non-negative pseudo-random 63-bit integer as an int64
|
||||
// from the default Source.
|
||||
func Int64() int64 {
|
||||
return rand.Int64()
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
|
||||
# xtime
|
||||
|
||||
Lightweight mock for time package.
|
||||
|
||||
A dedicated package such as [jonboulle/clockwork](https://github.com/jonboulle/clockwork/) would be better, but I would rather limit dependencies for this package. `clockwork` does not support Go 1.18 anymore.
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
//nolint:revive
|
||||
package xtime
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
func NewFakeClock() *FakeClock {
|
||||
return NewFakeClockAt(time.Now())
|
||||
}
|
||||
|
||||
func NewFakeClockAt(t time.Time) *FakeClock {
|
||||
return &FakeClock{
|
||||
time: t,
|
||||
}
|
||||
}
|
||||
|
||||
type FakeClock struct {
|
||||
_ noCopy
|
||||
|
||||
// Not protected by a mutex. If a warning is thrown in your tests,
|
||||
// just disable parallel tests.
|
||||
time time.Time
|
||||
}
|
||||
|
||||
func (c *FakeClock) Now() time.Time {
|
||||
return c.time
|
||||
}
|
||||
|
||||
func (c *FakeClock) Since(t time.Time) time.Duration {
|
||||
return c.time.Sub(t)
|
||||
}
|
||||
|
||||
func (c *FakeClock) Until(t time.Time) time.Duration {
|
||||
return t.Sub(c.time)
|
||||
}
|
||||
|
||||
func (c *FakeClock) Sleep(d time.Duration) {
|
||||
c.time = c.time.Add(d)
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package xtime
|
||||
|
||||
// noCopy may be added to structs which must not be copied
|
||||
// after the first use.
|
||||
//
|
||||
// See https://golang.org/issues/8005#issuecomment-190753527
|
||||
// for details.
|
||||
//
|
||||
// Note that it must not be embedded, due to the Lock and Unlock methods.
|
||||
type noCopy struct{}
|
||||
|
||||
// Lock is a no-op used by -copylocks checker from `go vet`.
|
||||
func (*noCopy) Lock() {}
|
||||
func (*noCopy) Unlock() {}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
//nolint:revive
|
||||
package xtime
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
func NewRealClock() *RealClock {
|
||||
return &RealClock{}
|
||||
}
|
||||
|
||||
type RealClock struct {
|
||||
_ noCopy
|
||||
}
|
||||
|
||||
func (c *RealClock) Now() time.Time {
|
||||
return time.Now()
|
||||
}
|
||||
|
||||
func (c *RealClock) Since(t time.Time) time.Duration {
|
||||
return time.Since(t)
|
||||
}
|
||||
|
||||
func (c *RealClock) Until(t time.Time) time.Duration {
|
||||
return time.Until(t)
|
||||
}
|
||||
|
||||
func (c *RealClock) Sleep(d time.Duration) {
|
||||
time.Sleep(d)
|
||||
}
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
//nolint:revive
|
||||
package xtime
|
||||
|
||||
import "time"
|
||||
|
||||
var clock Clock = &RealClock{}
|
||||
|
||||
func SetClock(c Clock) {
|
||||
clock = c
|
||||
}
|
||||
|
||||
func Now() time.Time {
|
||||
return clock.Now()
|
||||
}
|
||||
|
||||
func Since(t time.Time) time.Duration {
|
||||
return clock.Since(t)
|
||||
}
|
||||
|
||||
func Until(t time.Time) time.Duration {
|
||||
return clock.Until(t)
|
||||
}
|
||||
|
||||
func Sleep(d time.Duration) {
|
||||
clock.Sleep(d)
|
||||
}
|
||||
|
||||
type Clock interface {
|
||||
Now() time.Time
|
||||
Since(t time.Time) time.Duration
|
||||
Until(t time.Time) time.Duration
|
||||
Sleep(d time.Duration)
|
||||
}
|
||||
Reference in New Issue
Block a user