mirror of
https://github.com/PretendoNetwork/nex-go.git
synced 2025-04-02 11:02:14 -04:00
25 lines
535 B
Go
25 lines
535 B
Go
package nex
|
|
|
|
import (
|
|
"golang.org/x/exp/constraints"
|
|
)
|
|
|
|
type numeric interface {
|
|
constraints.Integer | constraints.Float | constraints.Complex
|
|
}
|
|
|
|
// Counter represents an incremental counter of a specific numeric type
|
|
type Counter[T numeric] struct {
|
|
Value T
|
|
}
|
|
|
|
// Next increments the counter by 1 and returns the new value
|
|
func (c *Counter[T]) Next() T {
|
|
c.Value++
|
|
return c.Value
|
|
}
|
|
|
|
// NewCounter returns a new Counter, with a starting number
|
|
func NewCounter[T numeric](start T) *Counter[T] {
|
|
return &Counter[T]{Value: start}
|
|
}
|