-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathref.go
More file actions
44 lines (38 loc) · 738 Bytes
/
ref.go
File metadata and controls
44 lines (38 loc) · 738 Bytes
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package phx
import (
"fmt"
"strconv"
"sync/atomic"
)
// Ref is a unique reference integer that is atomically incremented and will wrap at 64 bits + 1
type Ref uint64
func ParseRef(ref any) (Ref, error) {
if ref == nil {
return Ref(0), nil
}
switch v := ref.(type) {
case string:
if ref == "" {
return 0, nil
}
refUint, err := strconv.ParseUint(v, 10, 64)
if err != nil {
return 0, err
}
return Ref(refUint), nil
case uint64:
return Ref(v), nil
}
return 0, fmt.Errorf("cannot convert %#v to Ref", ref)
}
type atomicRef struct {
ref *uint64
}
func newAtomicRef() *atomicRef {
return &atomicRef{
ref: new(uint64),
}
}
func (ic *atomicRef) nextRef() Ref {
return Ref(atomic.AddUint64(ic.ref, 1))
}