-
Notifications
You must be signed in to change notification settings - Fork 1
/
pid.go
55 lines (44 loc) · 1.06 KB
/
pid.go
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
45
46
47
48
49
50
51
52
53
54
55
/*
© 2022–present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/)
ISC License
*/
package parl
import (
"strconv"
"github.com/haraldrudell/parl/ints"
"github.com/haraldrudell/parl/perrors"
"golang.org/x/exp/constraints"
)
// Pid is a unique named type for process identifiers
// - Pid implements fmt.Stringer
// - Pid is ordered
// - Pid has IsValid method
type Pid uint32
// NewPid returns a typed value process identifier
func NewPid[T constraints.Integer](pid T) (typedPid Pid, err error) {
var u32 uint32
if u32, err = ints.ConvertU32(pid, perrors.PackFunc()); err != nil {
return
}
typedPid = Pid(u32)
return
}
func NewPid1[T constraints.Integer](pid T) (typedPid Pid) {
var err error
if typedPid, err = NewPid(pid); err != nil {
panic(err)
}
return
}
func (pid Pid) IsValid() (isValid bool) {
return pid != 0
}
func (pid Pid) Int() (pidInt int) {
return int(pid)
}
func (pid Pid) Uint32() (pidUint32 uint32) {
return uint32(pid)
}
func (pid Pid) String() (s string) {
return strconv.Itoa(int(pid))
}