You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+15-1Lines changed: 15 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,7 +11,7 @@ It provides overflow-safe arithmetic (`+`, `-`, `*`, `/`) and safe type conversi
11
11
12
12
***Comprehensive generics**: works with all standard integer types.
13
13
***Checked arithmetic**: [`Add`](https://pkg.go.dev/go.dw1.io/safemath#Add), [`Sub`](https://pkg.go.dev/go.dw1.io/safemath#Sub), [`Mul`](https://pkg.go.dev/go.dw1.io/safemath#Mul), [`Div`](https://pkg.go.dev/go.dw1.io/safemath#Div) functions return an error instead of allowing silent, dangerous wrapping.
14
-
***Safe conversions**: [`Convert[To, From](v)`](https://pkg.go.dev/go.dw1.io/safemath#Convert) makes sure no data is lost during type conversion (e.g., checking bounds when casting larger types to smaller ones or signed to unsigned).
14
+
***Safe conversions**: [`Convert[To, From](v)`](https://pkg.go.dev/go.dw1.io/safemath#Convert) makes sure no data is lost during type conversion (e.g., checking bounds when casting larger types to smaller ones or signed to unsigned).[`ConvertAny`](https://pkg.go.dev/go.dw1.io/safemath#ConvertAny) extends the checks to `any` values, rejecting non-integer inputs.
15
15
***Panic APIs**: [`Must*`](https://pkg.go.dev/go.dw1.io/safemath#MustAdd) variants are available for situations where panicking on failure is preferred.
16
16
***Adversarial safety**: robustly handles dangerous edge cases like $$MinInt / -1$$ and avoids hardware exceptions.
17
17
@@ -56,6 +56,20 @@ func main() {
56
56
}
57
57
```
58
58
59
+
### Converting from interface{}
60
+
61
+
```go
62
+
val:=any(int64(42))
63
+
out, err:= safemath.ConvertAny[uint16](val)
64
+
if err != nil {
65
+
fmt.Println(err)
66
+
}
67
+
fmt.Println(out)
68
+
69
+
// Will panic (ErrInvalidType) because input is not an integer
0 commit comments