-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcasters_bool_num.go
88 lines (79 loc) · 2.11 KB
/
casters_bool_num.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package cast
func registerBoolNumCast() {
registerBoolInts()
registerBoolUints()
registerBoolFloats()
registerBoolComplexes()
}
// casters: [bool <-> ints]
// depends on: [ints <-> ints]
func registerBoolInts() {
MustRegister(func(in int64) (out bool, err error) {
return in != 0, nil
})
MustRegisterProxy[int32, bool, int64]()
MustRegisterProxy[int16, bool, int64]()
MustRegisterProxy[int8, bool, int64]()
MustRegisterProxy[int, bool, int64]()
MustRegister(func(in bool) (out int64, err error) {
if in {
return 1, nil
}
return 0, nil
})
MustRegisterProxy[bool, int32, int64]()
MustRegisterProxy[bool, int16, int64]()
MustRegisterProxy[bool, int8, int64]()
MustRegisterProxy[bool, int, int64]()
}
// casters: [bool <-> uints]
// depends on: [uints <-> uints]
func registerBoolUints() {
MustRegister(func(in uint64) (out bool, err error) {
return in != 0, nil
})
MustRegisterProxy[uint32, bool, uint64]()
MustRegisterProxy[uint16, bool, uint64]()
MustRegisterProxy[uint8, bool, uint64]()
MustRegisterProxy[uint, bool, uint64]()
MustRegister(func(in bool) (out uint64, err error) {
if in {
return 1, nil
}
return 0, nil
})
MustRegisterProxy[bool, uint32, uint64]()
MustRegisterProxy[bool, uint16, uint64]()
MustRegisterProxy[bool, uint8, uint64]()
MustRegisterProxy[bool, uint, uint64]()
}
// casters: [bool <-> floats]
// depends on: [floats <-> floats]
func registerBoolFloats() {
MustRegister(func(in float64) (out bool, err error) {
return in != 0, nil
})
MustRegisterProxy[float32, bool, float64]()
MustRegister(func(in bool) (out float64, err error) {
if in {
return 1, nil
}
return 0, nil
})
MustRegisterProxy[bool, float32, float64]()
}
// casters: [bool <-> complexes]
// depends on: [complexes <-> complexes]
func registerBoolComplexes() {
MustRegister(func(in complex128) (out bool, err error) {
return real(in) != 0, nil
})
MustRegisterProxy[complex64, bool, complex128]()
MustRegister(func(in bool) (out complex128, err error) {
if in {
return complex(1, 0), nil
}
return complex(0, 0), nil
})
MustRegisterProxy[bool, complex64, complex128]()
}