From f6a206941ca0fa6ce2607b6bb11adbcd859e1f6e Mon Sep 17 00:00:00 2001 From: caixw Date: Thu, 23 Feb 2023 14:38:09 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=B8=8D=E5=86=8D=E4=BE=9D?= =?UTF-8?q?=E8=B5=96=20exp/constraints?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- conv.go | 10 ++++------ go.mod | 2 -- go.sum | 2 -- types.go | 11 +++++++++++ 4 files changed, 15 insertions(+), 10 deletions(-) create mode 100644 types.go diff --git a/conv.go b/conv.go index 227657e..db588e3 100644 --- a/conv.go +++ b/conv.go @@ -8,8 +8,6 @@ import ( "reflect" "strconv" "strings" - - "golang.org/x/exp/constraints" ) // 抛出一个类型无法转换的错误 @@ -84,7 +82,7 @@ func MustBool(val any, def ...bool) bool { } // IntOf 转换成指定类型的符号整数 -func IntOf[T constraints.Signed](val any) (T, error) { +func IntOf[T Signed](val any) (T, error) { ret, err := toInt64(val) if err != nil { return 1, err @@ -95,7 +93,7 @@ func IntOf[T constraints.Signed](val any) (T, error) { // UintOf 转换成指定类型的无符号整数 // // 将一个有符号整数转换成无符号整数,负数将返回错误,正数和零正常转换 -func UintOf[T constraints.Unsigned](val any) (T, error) { +func UintOf[T Unsigned](val any) (T, error) { ret, err := toUint64(val) if err != nil { return 0, err @@ -492,7 +490,7 @@ func toUint64(val any) (uint64, error) { } // MustIntOf 将 val 转换成 T 类型或是在无法转换的情况下返回 def 参数 -func MustIntOf[T constraints.Signed](val any, def ...T) T { +func MustIntOf[T Signed](val any, def ...T) T { if ret, err := IntOf[T](val); err == nil { return ret } @@ -500,7 +498,7 @@ func MustIntOf[T constraints.Signed](val any, def ...T) T { } // MustUintOf 将 val 转换成 T 类型或是在无法转换的情况下返回 def 参数 -func MustUintOf[T constraints.Unsigned](val any, def ...T) T { +func MustUintOf[T Unsigned](val any, def ...T) T { if ret, err := UintOf[T](val); err == nil { return ret } diff --git a/go.mod b/go.mod index 0649151..1bd52c6 100644 --- a/go.mod +++ b/go.mod @@ -2,6 +2,4 @@ module github.com/issue9/conv require github.com/issue9/assert/v3 v3.0.4 -require golang.org/x/exp v0.0.0-20230212135524-a684f29349b6 - go 1.18 diff --git a/go.sum b/go.sum index 7399e3b..2ba59f3 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +1,2 @@ github.com/issue9/assert/v3 v3.0.4 h1:WsYZQ6PQmM/pGFrbkn5GIXjWeVZHv+wcl2829UTX1Qc= github.com/issue9/assert/v3 v3.0.4/go.mod h1:yft/uaskRpwQTyBT3n1zRl91SR1wNlO4fLZHzOa4bdM= -golang.org/x/exp v0.0.0-20230212135524-a684f29349b6 h1:Ic9KukPQ7PegFzHckNiMTQXGgEszA7mY2Fn4ZMtnMbw= -golang.org/x/exp v0.0.0-20230212135524-a684f29349b6/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc= diff --git a/types.go b/types.go new file mode 100644 index 0000000..63d4feb --- /dev/null +++ b/types.go @@ -0,0 +1,11 @@ +// SPDX-License-Identifier: MIT + +package conv + +type Signed interface { + ~int | ~int8 | ~int16 | ~int32 | ~int64 +} + +type Unsigned interface { + ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr +}