From d1da70bfa7c44086d918d294c849301b440c5a41 Mon Sep 17 00:00:00 2001 From: Lukas Nemec Date: Mon, 7 Nov 2022 11:30:32 +0100 Subject: [PATCH] Fix named types output. --- as.go | 3 +-- as_test.go | 10 +++++++++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/as.go b/as.go index 6d5d6ee..2f834e1 100644 --- a/as.go +++ b/as.go @@ -41,8 +41,7 @@ func T[To Number](v any) (To, error) { err error out To ) - - switch any(out).(type) { + switch any(indirect(out)).(type) { case int: var n int n, err = Int(v) diff --git a/as_test.go b/as_test.go index fb96f50..4304007 100644 --- a/as_test.go +++ b/as_test.go @@ -54,11 +54,19 @@ func TestTCustomType(t *testing.T) { assertNoError(t, as.T[int], aliasedInt(math.MinInt64)) } +func TestTIntoCustomType(t *testing.T) { + type aliasedInt int + type aliasedIntOut int + + assertNoError(t, as.T[aliasedIntOut], aliasedInt(math.MaxInt64)) + assertNoError(t, as.T[aliasedIntOut], aliasedInt(math.MinInt64)) +} + var out int // BenchmarkT-8 91144112 12.90 ns/op 8 B/op 0 allocs/op // After changes to indirect: -// BenchmarkT-8 40429498 29.37 ns/op 16 B/op 1 allocs/op +// BenchmarkT-8 24196993 49.55 ns/op 24 B/op 2 allocs/op func BenchmarkT(b *testing.B) { var t int for n := 0; n < b.N; n++ {