github.com/jxskiss/gopkg/v2@v2.14.9-0.20240514120614-899f3e7952b4/utils/ptr/primitive.go (about) 1 package ptr 2 3 import ( 4 "reflect" 5 "strconv" 6 "time" 7 "unsafe" 8 9 "github.com/jxskiss/gopkg/v2/internal/constraints" 10 ) 11 12 func Bool(v bool) *bool { return &v } 13 func Time(v time.Time) *time.Time { return &v } 14 func Duration(v time.Duration) *time.Duration { return &v } 15 16 func String[T ~string | constraints.Integer](v T) *string { 17 if s, ok := any(v).(string); ok { 18 return &s 19 } 20 rv := reflect.ValueOf(v) 21 switch rv.Kind() { 22 case reflect.String: 23 s := rv.String() 24 return &s 25 case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int: 26 s := strconv.FormatInt(rv.Int(), 10) 27 return &s 28 case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint, reflect.Uintptr: 29 s := strconv.FormatUint(rv.Uint(), 10) 30 return &s 31 } 32 panic("bug: unreachable code") 33 } 34 35 func Int[T constraints.Integer](v T) *int { 36 x := int(v) 37 return &x 38 } 39 40 func Int8[T constraints.Integer](v T) *int8 { 41 x := int8(v) 42 return &x 43 } 44 45 func Int16[T constraints.Integer](v T) *int16 { 46 x := int16(v) 47 return &x 48 } 49 50 func Int32[T constraints.Integer](v T) *int32 { 51 x := int32(v) 52 return &x 53 } 54 55 func Int64[T constraints.Integer](v T) *int64 { 56 x := int64(v) 57 return &x 58 } 59 60 func Uint[T constraints.Integer](v T) *uint { 61 x := uint(v) 62 return &x 63 } 64 65 func Uint8[T constraints.Integer](v T) *uint8 { 66 x := uint8(v) 67 return &x 68 } 69 70 func Uint16[T constraints.Integer](v T) *uint16 { 71 x := uint16(v) 72 return &x 73 } 74 75 func Uint32[T constraints.Integer](v T) *uint32 { 76 x := uint32(v) 77 return &x 78 } 79 80 func Uint64[T constraints.Integer](v T) *uint64 { 81 x := uint64(v) 82 return &x 83 } 84 85 func Float32[T constraints.RealNumber](v T) *float32 { 86 x := float32(v) 87 return &x 88 } 89 90 func Float64[T constraints.RealNumber](v T) *float64 { 91 x := float64(v) 92 return &x 93 } 94 95 func DerefBool(v *bool) bool { return Deref(v) } 96 func DerefTime(v *time.Time) time.Time { return Deref(v) } 97 func DerefDuration(v *time.Duration) time.Duration { return Deref(v) } 98 99 func DerefString[T ~string](v *T) string { 100 return Deref((*string)(unsafe.Pointer(v))) 101 } 102 103 func DerefInt[T constraints.Integer](v *T) (ret int) { 104 if v != nil { 105 ret = int(*v) 106 } 107 return 108 } 109 110 func DerefInt8[T constraints.Integer](v *T) (ret int8) { 111 if v != nil { 112 ret = int8(*v) 113 } 114 return 115 } 116 117 func DerefInt16[T constraints.Integer](v *T) (ret int16) { 118 if v != nil { 119 ret = int16(*v) 120 } 121 return 122 } 123 124 func DerefInt32[T constraints.Integer](v *T) (ret int32) { 125 if v != nil { 126 ret = int32(*v) 127 } 128 return 129 } 130 131 func DerefInt64[T constraints.Integer](v *T) (ret int64) { 132 if v != nil { 133 ret = int64(*v) 134 } 135 return 136 } 137 138 func DerefUint[T constraints.Integer](v *T) (ret uint) { 139 if v != nil { 140 ret = uint(*v) 141 } 142 return 143 } 144 145 func DerefUint8[T constraints.Integer](v *T) (ret uint8) { 146 if v != nil { 147 ret = uint8(*v) 148 } 149 return 150 } 151 152 func DerefUint16[T constraints.Integer](v *T) (ret uint16) { 153 if v != nil { 154 ret = uint16(*v) 155 } 156 return 157 } 158 159 func DerefUint32[T constraints.Integer](v *T) (ret uint32) { 160 if v != nil { 161 ret = uint32(*v) 162 } 163 return 164 } 165 166 func DerefUint64[T constraints.Integer](v *T) (ret uint64) { 167 if v != nil { 168 ret = uint64(*v) 169 } 170 return 171 } 172 173 func DerefFloat32[T constraints.RealNumber](v *T) (ret float32) { 174 if v != nil { 175 ret = float32(*v) 176 } 177 return 178 } 179 180 func DerefFloat64[T constraints.RealNumber](v *T) (ret float64) { 181 if v != nil { 182 ret = float64(*v) 183 } 184 return 185 }