File: uint.go

package info (click to toggle)
golang-github-geertjohan-go.incremental 1.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 176 kB
  • sloc: makefile: 4
file content (30 lines) | stat: -rw-r--r-- 663 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package incremental

import (
	"sync"
)

type Uint struct {
	increment uint
	lock      sync.Mutex
}

// Next returns with an integer that is exactly one higher as the previous call to Next() for this Uint
func (i *Uint) Next() uint {
	i.lock.Lock()
	defer i.lock.Unlock()
	i.increment++
	return i.increment
}

// Last returns the number (uint) that was returned by the most recent call to this instance's Next()
func (i *Uint) Last() uint {
	return i.increment
}

// Set changes the increment to given value, the succeeding call to Next() will return the given value+1
func (i *Uint) Set(value uint) {
	i.lock.Lock()
	defer i.lock.Unlock()
	i.increment = value
}