Skip to content

Commit e02a862

Browse files
committed
feat: support uintptr as a valid conversion source and target type
1 parent 902e99b commit e02a862

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

conversion_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,13 @@ func TestConvert(t *testing.T) {
204204
"float64 to uint64": MapTest[float64, uint64]{Input: 42, ExpectedOutput: 42},
205205
"float64 to float32": MapTest[float64, float32]{Input: 42, ExpectedOutput: 42},
206206
"float64 to float64": MapTest[float64, float64]{Input: 42, ExpectedOutput: 42},
207+
208+
"uintptr to uint": MapTest[uintptr, uint]{Input: uintptr(42), ExpectedOutput: uint(42)},
209+
"uint to uintptr": MapTest[uint, uintptr]{Input: uint(42), ExpectedOutput: uintptr(42)},
210+
"int8 to uintptr": MapTest[int8, uintptr]{Input: int8(42), ExpectedOutput: uintptr(42)},
211+
"int16 to uintptr": MapTest[int16, uintptr]{Input: int16(42), ExpectedOutput: uintptr(42)},
212+
"int32 to uintptr": MapTest[int32, uintptr]{Input: int32(42), ExpectedOutput: uintptr(42)},
213+
"int64 to uintptr": MapTest[int64, uintptr]{Input: int64(42), ExpectedOutput: uintptr(42)},
207214
} {
208215
t.Run(name, func(t *testing.T) {
209216
c.Run(t)
@@ -310,6 +317,11 @@ func TestConvert(t *testing.T) {
310317
Input: float64(math.MaxUint64 * 1.01),
311318
ExpectedError: safecast.ErrExceedMaximumValue,
312319
},
320+
321+
"upper bound overflows for uintptr": MapTest[float64, uintptr]{
322+
Input: float64(math.MaxUint64 * 1.01), // uintptr is at least uint32, but can be uint64
323+
ExpectedError: safecast.ErrExceedMaximumValue,
324+
},
313325
} {
314326
t.Run(name, func(t *testing.T) {
315327
c.Run(t)
@@ -370,6 +382,11 @@ func TestConvert(t *testing.T) {
370382
Input: -42,
371383
ExpectedError: safecast.ErrExceedMinimumValue,
372384
},
385+
386+
"negative overflows uintptr": MapTest[int, uintptr]{
387+
Input: -42,
388+
ExpectedError: safecast.ErrExceedMinimumValue,
389+
},
373390
} {
374391
t.Run(name, func(t *testing.T) {
375392
c.Run(t)

types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66

77
// Number is a constraint for all integers and floats
88
type Number interface {
9-
~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~float32 | ~float64
9+
~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~float32 | ~float64 | ~uintptr
1010

1111
// TODO(ccoVeille): consider using complex, but not sure if there is a need
1212
}

0 commit comments

Comments
 (0)